Updated text2img_api and UI_2_generate_image

- Refactored new configuration vars to be used directly instead of making copies in the function.
- Rewrote post assembly for text2img_api so that it passes values correctly.

Signed-off-by: viningr
This commit is contained in:
Robert Vining
2022-11-15 18:47:19 +10:00
parent d0529957f6
commit 0eef307172
3 changed files with 17 additions and 28 deletions

View File

@@ -9112,9 +9112,7 @@ def UI_2_generate_image(data):
eventlet.sleep(0) eventlet.sleep(0)
art_guide = '{}'.format(koboldai_vars.img_gen_art_guide) art_guide = '{}'.format(koboldai_vars.img_gen_art_guide)
steps = '{}'.format(koboldai_vars.img_gen_steps) print("Generating image using data:{} and art guide:{}".format(data,art_guide))
cfg_scale = '{}'.format(koboldai_vars.img_gen_cfg_scale)
#get latest action #get latest action
if len(koboldai_vars.actions) > 0: if len(koboldai_vars.actions) > 0:
action = koboldai_vars.actions[-1] action = koboldai_vars.actions[-1]
@@ -9193,11 +9191,11 @@ def UI_2_generate_image(data):
@logger.catch @logger.catch
def text2img_local(prompt, art_guide="", filename="new.png"): def text2img_local(prompt,
art_guide="",
filename="new.png"):
start_time = time.time() start_time = time.time()
logger.debug("Generating Image") logger.debug("Generating Image")
steps = '{}'.format(koboldai_vars.img_gen_steps)
cfg_scale = '{}'.format(koboldai_vars.img_gen_cfg_scale)
koboldai_vars.aibusy = True koboldai_vars.aibusy = True
koboldai_vars.generating_image = True koboldai_vars.generating_image = True
from diffusers import StableDiffusionPipeline from diffusers import StableDiffusionPipeline
@@ -9214,7 +9212,7 @@ def text2img_local(prompt, art_guide="", filename="new.png"):
from torch import autocast from torch import autocast
with autocast("cuda"): with autocast("cuda"):
return pipe(prompt, num_inference_steps=num_inference_steps).images[0] return pipe(prompt, num_inference_steps=num_inference_steps).images[0]
image = tpool.execute(get_image, pipe, prompt, num_inference_steps=steps) image = tpool.execute(get_image, pipe, prompt, num_inference_steps=koboldai_vars.img_gen_steps)
buffered = BytesIO() buffered = BytesIO()
image.save(buffered, format="JPEG") image.save(buffered, format="JPEG")
img_str = base64.b64encode(buffered.getvalue()).decode('ascii') img_str = base64.b64encode(buffered.getvalue()).decode('ascii')
@@ -9235,11 +9233,8 @@ def text2img_local(prompt, art_guide="", filename="new.png"):
@logger.catch @logger.catch
def text2img_horde(prompt, def text2img_horde(prompt,
#art_guide = '{}'.format(koboldai_vars.img_gen_art_guide),
art_guide = "", art_guide = "",
filename = "story_art.png"): filename = "story_art.png"):
steps = '{}'.format(koboldai_vars.img_gen_steps)
cfg_scale = '{}'.format(koboldai_vars.img_gen_cfg_scale)
logger.debug("Generating Image using Horde") logger.debug("Generating Image using Horde")
koboldai_vars.generating_image = True koboldai_vars.generating_image = True
@@ -9255,8 +9250,8 @@ def text2img_horde(prompt,
"nsfw": True, "nsfw": True,
"sampler_name": "k_euler_a", "sampler_name": "k_euler_a",
"karras": True, "karras": True,
"cfg_scale": cfg_scale, "cfg_scale": koboldai_vars.img_gen_cfg_scale,
"steps":steps, "steps":koboldai_vars.img_gen_steps,
"width":512, "width":512,
"height":512} "height":512}
} }
@@ -9286,11 +9281,8 @@ def text2img_horde(prompt,
@logger.catch @logger.catch
def text2img_api(prompt, def text2img_api(prompt,
#art_guide = '{}'.format(koboldai_vars.img_gen_art_guide),
art_guide = "", art_guide = "",
filename = "story_art.png"): filename = "story_art.png"):
steps = '{}'.format(koboldai_vars.img_gen_steps)
cfg_scale = '{}'.format(koboldai_vars.img_gen_cfg_scale)
logger.debug("Generating Image using Local SD-WebUI API") logger.debug("Generating Image using Local SD-WebUI API")
koboldai_vars.generating_image = True koboldai_vars.generating_image = True
#The following list are valid properties with their defaults, to add/modify in final_imgen_params. Will refactor configuring values into UI element in future. #The following list are valid properties with their defaults, to add/modify in final_imgen_params. Will refactor configuring values into UI element in future.
@@ -9324,22 +9316,19 @@ def text2img_api(prompt,
#"override_settings": {}, #"override_settings": {},
#"sampler_index": "Euler" #"sampler_index": "Euler"
final_imgen_params = { final_imgen_params = {
"n": 1, "prompt": "{}, {}".format(prompt, art_guide),
"n_iter": 1,
"width": 512, "width": 512,
"height": 512, "height": 512,
"steps": steps, "steps": koboldai_vars.img_gen_steps,
"cfg_scale": cfg_scale, "cfg_scale": koboldai_vars.img_gen_cfg_scale,
"negative_prompt": "{}".format(koboldai_vars.img_gen_negative_prompt), "negative_prompt": "{}".format(koboldai_vars.img_gen_negative_prompt),
"sampler_index": "Euler a" "sampler_index": "Euler a"
} }
final_submit_dict = {
"prompt": "{}, {}".format(prompt, art_guide),
"params": final_imgen_params,
}
apiaddress = '{}/sdapi/v1/txt2img'.format(koboldai_vars.img_gen_api_url) apiaddress = '{}/sdapi/v1/txt2img'.format(koboldai_vars.img_gen_api_url)
payload_json = json.dumps(final_submit_dict) payload_json = json.dumps(final_imgen_params)
logger.debug(final_submit_dict) logger.debug(final_imgen_params)
#print("payload_json contains " + payload_json)
submit_req = requests.post(url=f'{apiaddress}', data=payload_json).json() submit_req = requests.post(url=f'{apiaddress}', data=payload_json).json()
if submit_req: if submit_req:
results = submit_req results = submit_req

View File

@@ -601,12 +601,12 @@ gensettingstf = [
{ {
"UI_V2_Only": True, "UI_V2_Only": True,
"uitype": "slider", "uitype": "slider",
"unit": "int", "unit": "float",
"label": "Cfg Scale", "label": "Cfg Scale",
"id": "img_gen_cfg_scale", "id": "img_gen_cfg_scale",
"min": 1, "min": 1,
"max": 30, "max": 30,
"step": 1, "step": 0.5,
"default": "7", "default": "7",
"tooltip": "Set how strictly the AI will follow prompts, 5-15 are good values.\nDefault:7", "tooltip": "Set how strictly the AI will follow prompts, 5-15 are good values.\nDefault:7",
"menu_path": "Interface", "menu_path": "Interface",

View File

@@ -997,7 +997,7 @@ class user_settings(settings):
self.img_gen_art_guide = "fantasy illustration, artstation, by jason felix by steve argyle by tyler jacobson by peter mohrbacher, cinematic lighting" self.img_gen_art_guide = "fantasy illustration, artstation, by jason felix by steve argyle by tyler jacobson by peter mohrbacher, cinematic lighting"
self.img_gen_negative_prompt = "lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry, artist name" self.img_gen_negative_prompt = "lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry, artist name"
self.img_gen_steps = 30 self.img_gen_steps = 30
self.img_gen_cfg_scale = 7 self.img_gen_cfg_scale = 7.0
self.cluster_requested_models = [] # The models which we allow to generate during cluster mode self.cluster_requested_models = [] # The models which we allow to generate during cluster mode