Skip to content

Commit

Permalink
add support to prompt shortcut in txt2img mode
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdullahAlfaraj committed Feb 3, 2023
1 parent 706437b commit 70f6b6a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 16 deletions.
3 changes: 2 additions & 1 deletion index.js
Expand Up @@ -2,6 +2,7 @@
// helloHelper2 = require('./helper.js')
// for organizational proposes
// let g_sdapi_path = 'sdapi'
let g_version = 'v1.1.2'
let g_sdapi_path = 'sdapi_py_re'
let g_sd_url = 'http://127.0.0.1:7860'
const helper = require('./helper')
Expand All @@ -25,7 +26,7 @@ const sd_config = require('./utility/sdapi/config')
const session = require('./utility/session')
const ui = require('./utility/ui')
const script_horde = require('./utility/sd_scripts/horde')

const prompt_shortcut = require('./utility/sdapi/prompt_shortcut')
const formats = require('uxp').storage.formats
const storage = require('uxp').storage
const fs = storage.localFileSystem
Expand Down
8 changes: 4 additions & 4 deletions manifest.json
Expand Up @@ -63,7 +63,7 @@
{
"width": 32,
"height": 32,
"path": "icons/icon_D.png",
"path": "icon/icon_D.png",
"scale": [
1,
2
Expand All @@ -79,7 +79,7 @@
{
"width": 32,
"height": 32,
"path": "icons/icon_N.png",
"path": "icon/icon_N.png",
"scale": [
1,
2
Expand All @@ -99,7 +99,7 @@
{
"width": 32,
"height": 32,
"path": "icons/icon_D.png",
"path": "icon/icon_D.png",
"scale": [
1,
2
Expand All @@ -115,7 +115,7 @@
{
"width": 32,
"height": 32,
"path": "icons/icon_N.png",
"path": "icon/icon_N.png",
"scale": [
1,
2
Expand Down
2 changes: 1 addition & 1 deletion sdapi_py_re.js
Expand Up @@ -225,7 +225,7 @@ async function requestInterrupt(model_title) {
async function getVersionRequest() {
// version = "v0.0.0"
console.log('requestGetSamplers: ')
const current_version = 'v1.1.1'
const current_version = g_version
// try {
// const full_url = 'http://127.0.0.1:8000/version'
// let request = await fetch(full_url)
Expand Down
38 changes: 38 additions & 0 deletions utility/sdapi/prompt_shortcut.js
@@ -0,0 +1,38 @@
function find_words_inside_braces(string) {
const re = /\{(.*?)\}/g
const keywords = string.match(re)
console.log('keywords: ', keywords)
return keywords
}

function replaceShortcut(text, prompt_shortcut_json) {
const original_keywords = find_words_inside_braces(text)
const strip_keywords = original_keywords.map((s) => {
let content = s.slice(1, -1) //remove '{' and '}'
content = content.trim() //remove any space in the beginning and end of content
return content
})

// original_substrings = list(map(lambda s: '{'+s+'}',raw_keywords))

// print("strip_keywords: ", strip_keywords)
// print("original_substrings: ",original_substrings)
// # print ("text:",text)

let i = 0
for (const word of strip_keywords) {
// # word = word.strip()
// print("word: ",word)
if (word.length > 0 && prompt_shortcut_json.hasOwnProperty(word)) {
const prompt = prompt_shortcut_json[word]
console.log('prompt: ', prompt)
text = text.replace(original_keywords[i], prompt)
}
}
console.log('final text: ', text)
return text
}
module.exports = {
find_words_inside_braces,
replaceShortcut,
}
50 changes: 40 additions & 10 deletions utility/sdapi/python_replacement.js
Expand Up @@ -10,14 +10,19 @@ function newOutputImageName() {
async function txt2ImgRequest(payload) {
console.log('payload:', payload)

// if(payload['use_prompt_shortcut']){

// const prompt_shortcut_dict = prompt_shortcut.load()
// prompt_shortcut_dict.update(payload["prompt_shortcut_ui_dict"])
// payload['prompt'] = prompt_shortcut.replaceShortcut(payload['prompt'],prompt_shortcut_dict)
// # edit negative prompt, replaceShortcut(negative_prompt)
// payload['negative_prompt'] = prompt_shortcut.replaceShortcut(payload['negative_prompt'],prompt_shortcut_dict)
// }
if (payload['use_prompt_shortcut']) {
// const prompt_shortcut_dict = prompt_shortcut.load()
// prompt_shortcut_dict.update(payload["prompt_shortcut_ui_dict"])
payload['prompt'] = prompt_shortcut.replaceShortcut(
payload['prompt'],
payload['prompt_shortcut_ui_dict']
)
// # edit negative prompt, replaceShortcut(negative_prompt)
payload['negative_prompt'] = prompt_shortcut.replaceShortcut(
payload['negative_prompt'],
payload['prompt_shortcut_ui_dict']
)
}
const endpoint = 'sdapi/v1/txt2img'
try {
console.log('txt2ImgRequest(): about to send a fetch request')
Expand Down Expand Up @@ -189,6 +194,27 @@ async function getOutputImagesEntries(doc_entry) {
// .forEach((e) => console.log(e.name))
return output_images_entries
}

async function getMetaDataForOutputEntry(doc_entry, output_entry) {
const json_file_name = `${output_entry.name.split('.')[0]}.json`

try {
const json_entry = await doc_entry.getEntry(json_file_name)
if (json_entry) {
// await json_entry.read()

const json = JSON.parse(
await json_entry.read({
format: storage.formats.utf8,
})
)
return json
}
} catch (e) {
console.warn(e)
}
return {}
}
async function loadHistory(payload) {
// {'image_paths','metadata_setting'}
const history = {}
Expand All @@ -202,9 +228,13 @@ async function loadHistory(payload) {
history['image_paths'] = []
history['metadata_jsons'] = []
history['base64_images'] = []
for (output_entry of output_images_entries) {
for (const output_entry of output_images_entries) {
history['image_paths'].push(output_entry.name)
history['metadata_jsons'].push({})
const metadata_json = await getMetaDataForOutputEntry(
doc_entry,
output_entry
)
history['metadata_jsons'].push(metadata_json)

const arrayBuffer = await output_entry.read({ format: formats.binary })
const base64_image = _arrayBufferToBase64(arrayBuffer) //convert the buffer to base64
Expand Down

0 comments on commit 70f6b6a

Please sign in to comment.