Skip to content

Commit

Permalink
convert the output of ControlNet api to a standard response the plugi…
Browse files Browse the repository at this point in the history
…n can work with
  • Loading branch information
AbdullahAlfaraj committed Feb 21, 2023
1 parent 047fc5c commit 656664d
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 52 deletions.
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1966,6 +1966,9 @@ async function getSettings() {

//save the control_net_image
payload['control_net_image'] = g_generation_session.controlNetImage
payload['enable_control_net'] =
document.getElementById('chEnableControlNet').checked
payload['control_net_weight'] = html_manip.getControlNetWeight()
payload = {
...payload,
// prompt: prompt,
Expand Down Expand Up @@ -2138,7 +2141,13 @@ async function generateTxt2Img(settings) {
backend_type === backendTypeEnum['Auto1111'] ||
backend_type === backendTypeEnum['Auto1111HordeExtension']
) {
json = await sdapi.requestTxt2Img(settings)
if (settings['enable_control_net']) {
//use control net

json = await sdapi.requestControlNetTxt2Img(settings)
} else {
json = await sdapi.requestTxt2Img(settings)
}
}
} catch (e) {
console.warn(e)
Expand Down
102 changes: 56 additions & 46 deletions sdapi_py_re.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,19 +603,21 @@ function mapPluginSettingsToControlNet(plugin_settings) {
controlnet_module: 'depth',
controlnet_model: 'control_sd15_depth [fef5e48e]',

controlnet_weight: 1,
controlnet_weight: parseInt(ps['control_net_weight']),
controlnet_resize_mode: 'Scale to Fit (Inner Fit)',
// controlnet_lowvram: true,
controlnet_processor_res: 512,
controlnet_threshold_a: 64,
controlnet_threshold_b: 64,
seed: ps['seed'],
subseed: -1,
subseed_strength: -1,
// subseed_strength: -1,
// subseed_strength: 0,
controlnet_guidance: 1,
sampler_index: ps['sampler_index'],
batch_size: ps['batch_size'],
batch_size: parseInt(ps['batch_size']),
n_iter: 1,
steps: ps['steps'],
steps: parseInt(ps['steps']),
cfg_scale: ps['cfg_scale'],
width: ps['width'],
height: ps['height'],
Expand All @@ -633,42 +635,43 @@ async function requestControlNetTxt2Img(plugin_settings) {

const full_url = `${g_sd_url}/controlnet/txt2img`
const control_net_settings = mapPluginSettingsToControlNet(plugin_settings)
payload = {
prompt: 'cute cat',
negative_prompt: 'ugly',
controlnet_input_image: [getDummyBase64_2()],
// controlnet_mask: (List[str] = Body(
// [],
// (title = 'ControlNet Input Mask')
// )),
controlnet_module: 'depth',
controlnet_model: 'control_sd15_depth [fef5e48e]',
controlnet_weight: 1.0,
controlnet_resize_mode: 'Scale to Fit (Inner Fit)',
controlnet_lowvram: false,
controlnet_processor_res: 512,
controlnet_threshold_a: 64,
controlnet_threshold_b: 64,
seed: -1,
subseed: -1,
subseed_strength: -1,
sampler_index: 'Euler a',
batch_size: 4,
n_iter: 1,
steps: 20,
cfg_scale: 7,
width: 512,
height: 512,
restore_faces: false,
// override_settings: (Dict[(str, Any)] = Body(
// None,
// (title = 'Override Settings')
// )),
// override_settings_restore_afterwards: (bool = Body(
// True,
// (title = 'Restore Override Settings Afterwards')
// )),
}
// payload = {
// prompt: 'cute cat',
// negative_prompt: 'ugly',
// controlnet_input_image: [getDummyBase64_2()],
// // controlnet_mask: (List[str] = Body(
// // [],
// // (title = 'ControlNet Input Mask')
// // )),
// controlnet_module: 'depth',
// controlnet_model: 'control_sd15_depth [fef5e48e]',
// controlnet_weight: 1.0,
// controlnet_resize_mode: 'Scale to Fit (Inner Fit)',
// controlnet_lowvram: false,
// controlnet_processor_res: 512,
// controlnet_threshold_a: 64,
// controlnet_threshold_b: 64,
// seed: -1,
// subseed: -1,
// subseed_strength: -1,
// sampler_index: 'Euler a',
// batch_size: 4,
// n_iter: 1,
// steps: 20,
// cfg_scale: 7,
// width: 512,
// height: 512,
// restore_faces: false,
// // override_settings: (Dict[(str, Any)] = Body(
// // None,
// // (title = 'Override Settings')
// // )),
// // override_settings_restore_afterwards: (bool = Body(
// // True,
// // (title = 'Restore Override Settings Afterwards')
// // )),
// }

let request = await fetch(full_url, {
method: 'POST',
headers: {
Expand All @@ -680,19 +683,26 @@ async function requestControlNetTxt2Img(plugin_settings) {
})

let json = await request.json()

console.log('json:', json)
//get all images except last because it's the mask
for (const image of json['images'].slice(0, -1)) {
await io.IO.base64ToLayer(image)
}

//update the mask in controlNet tab
const numOfImages = json['images'].length
const base64_mask = json['images'][numOfImages - 1]

html_manip.setControlMaskSrc(base64ToBase64Url(base64_mask))

return json
const standard_response = await py_re.convertToStandardResponse(
json['images'],
plugin_settings['uniqueDocumentId']
)
console.log('standard_response:', standard_response)

// //get all images except last because it's the mask
// for (const image of json['images'].slice(0, -1)) {
// await io.IO.base64ToLayer(image)
// }

return standard_response
}

module.exports = {
Expand Down
5 changes: 4 additions & 1 deletion server/python_server/control_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ def __init__(self, prompt):
"controlnet_module": 'depth',
"ControlNet Weight": 1,
"controlnet_model": 'control_sd15_depth [fef5e48e]',
"controlnet_guidance": 1
}

def sendRequest(self):
# print(self.simple_txt2img)
r = requests.post(self.url, json=self.body)
print(r)
return r.json()

js = controlnetRequest("clothed busty bird").sendRequest()
Expand All @@ -59,4 +61,5 @@ def sendRequest(self):



len(js['images'])
len(js['images'])
print(js)
8 changes: 8 additions & 0 deletions utility/html_manip.js
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,13 @@ document
document.getElementById('lControlNetWeight').textContent =
Number(sd_value).toFixed(2)
})
function getControlNetWeight() {
const slider_value = document.getElementById('slControlNetWeight').value

// debugger
const sd_value = general.mapRange(slider_value, 0, 100, 0, 2) // convert slider value to SD ready value
return sd_value
}
module.exports = {
getPrompt,
autoFillInPrompt,
Expand Down Expand Up @@ -865,4 +872,5 @@ module.exports = {
isSquareThumbnail,
setControlImageSrc,
setControlMaskSrc,
getControlNetWeight,
}
38 changes: 38 additions & 0 deletions utility/sdapi/python_replacement.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,44 @@ async function getAuto1111Metadata(base64_image) {
console.warn(e)
}
}
async function convertToStandardResponse(images, uuid) {
//standardized the response between modes and backends
const uniqueDocumentId = uuid // maybe use the generation_session uuid

const image_paths = []

const metadata = []
const images_info = []

for (i of images) {
let auto_metadata_json = {}
try {
const auto_metadata_str = await getAuto1111Metadata(i)
auto_metadata_json = convertMetadataToJson(auto_metadata_str)
console.warn('auto_metadata_json.Seed:', auto_metadata_json?.Seed)
} catch (e) {
console.warn(e)
auto_metadata_json = {} // set the metadata to empty if there an error while getting the metadata
}

const image_name = general.newOutputImageName()
const image_path = `${uniqueDocumentId}/${image_name}`

images_info.push({
base64: i,
path: image_path,
auto_metadata: auto_metadata_json,
})
// console.log("metadata_json: ", metadata_json)
}
const dir_name = 'temp_dir_name'
return {
payload: payload,
dir_name: dir_name,
images_info: images_info,
metadata: metadata,
}
}
function replacePromptsWithShortcuts(
prompt,
negative_prompt,
Expand Down Expand Up @@ -556,4 +593,5 @@ module.exports = {
openUrlRequest,
replacePromptsWithShortcuts,
extraSingleImageRequest,
convertToStandardResponse,
}
7 changes: 3 additions & 4 deletions utility/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,9 @@ class GenerationSession {
await layer_util.deleteLayers([g_inpaint_mask_layer])
await createTempInpaintMaskLayer()
}
//delete controlNet image
this.controlNetImage = null

html_manip.setControlImageSrc('https://source.unsplash.com/random')
//delete controlNet image, Note: don't delete control net, let the user disable controlNet if doesn't want to use it
// this.controlNetImage = null
// html_manip.setControlImageSrc('https://source.unsplash.com/random')
} catch (e) {
console.warn(e)
}
Expand Down

0 comments on commit 656664d

Please sign in to comment.