Initial implementation of prompt matrix - creates multiple task entries for the different permutations

This commit is contained in:
cmdr2 2022-10-08 15:56:56 +05:30
parent 7ae70d5a4d
commit 6b7d4877e6
2 changed files with 76 additions and 12 deletions

View File

@ -15,7 +15,7 @@
<div id="container">
<div id="top-nav">
<div id="logo">
<h1>Stable Diffusion UI <small>v2.21 <span id="updateBranchLabel"></span></small></h1>
<h1>Stable Diffusion UI <small>v2.22 <span id="updateBranchLabel"></span></small></h1>
</div>
<ul id="top-nav-items">
<li class="dropdown">
@ -226,7 +226,7 @@
</div>
</body>
<script src="media/main.js?v=32"></script>
<script src="media/main.js?v=33"></script>
<script>
async function init() {
await loadModifiers()

View File

@ -596,16 +596,8 @@ function makeImage() {
return
}
let prompts = promptField.value
prompts = prompts.split('\n')
prompts.forEach(prompt => {
prompt = prompt.trim()
if (prompt === '') {
return
}
createTask(prompt)
})
let prompts = getPrompts()
prompts.forEach(createTask)
initialText.style.display = 'none'
}
@ -747,6 +739,78 @@ function createTask(prompt) {
taskQueue.unshift(task)
}
function getPrompts() {
let prompts = promptField.value
prompts = prompts.split('\n')
let promptsToMake = []
prompts.forEach(prompt => {
prompt = prompt.trim()
if (prompt === '') {
return
}
let promptMatrix = prompt.split('|')
prompt = promptMatrix.shift().trim()
promptsToMake.push(prompt)
promptMatrix = promptMatrix.map(p => p.trim())
promptMatrix = promptMatrix.filter(p => p !== '')
if (promptMatrix.length > 0) {
let promptPermutations = permutePrompts(prompt, promptMatrix)
promptsToMake = promptsToMake.concat(promptPermutations)
}
})
return promptsToMake
}
function permutePrompts(promptBase, promptMatrix) {
let prompts = []
let permutations = permute(promptMatrix)
permutations.forEach(perm => {
let prompt = promptBase
if (perm.length > 0) {
let promptAddition = perm.join(', ')
if (promptAddition.trim() === '') {
return
}
prompt += ', ' + promptAddition
}
prompts.push(prompt)
})
return prompts
}
function permute(arr) {
let permutations = []
let n = arr.length
let n_permutations = Math.pow(2, n)
for (let i = 0; i < n_permutations; i++) {
let perm = []
let mask = Number(i).toString(2).padStart(n, '0')
for (let idx = 0; idx < mask.length; idx++) {
if (mask[idx] === '1' && arr[idx].trim() !== '') {
perm.push(arr[idx])
}
}
if (perm.length > 0) {
permutations.push(perm)
}
}
return permutations
}
// create a file name with embedded prompt and metadata
// for easier cateloging and comparison
function createFileName(seed, outputFormat) {