- Updated UI_2_generate_image()to reference new option in the Image Generation dropdown menu.

- Using Local SD-WebUI API option will send a post request to stable-diffusion-webui running with the --api switch on http://127.0.0.1:7860/sdapi/v1/txt2img

Signed-off-by: Robert Vining
This commit is contained in:
Robert Vining
2022-11-01 23:24:19 +10:00
parent 107bbfa291
commit 0bb6e0afae
2 changed files with 62 additions and 25 deletions

View File

@@ -9097,20 +9097,24 @@ def UI_2_generate_image(data):
#If we don't have a GPU, use horde if we're allowed to
start_time = time.time()
if ((not koboldai_vars.hascuda or not os.path.exists("models/stable-diffusion-v1-4")) and koboldai_vars.img_gen_priority != 0) or koboldai_vars.img_gen_priority == 3:
# Check if stable-diffusion-webui API option selected and use that if found.
if koboldai_vars.img_gen_priority == 4:
b64_data = text2img_api(", ".join(keys), art_guide = art_guide)
else:
import psutil
#We aren't being forced to use horde, so now let's figure out if we should use local
if torch.cuda.get_device_properties(0).total_memory - torch.cuda.memory_reserved(0) >= 6000000000:
#He have enough vram, just do it locally
b64_data = text2img_local(", ".join(keys), art_guide = art_guide)
elif torch.cuda.get_device_properties(0).total_memory > 6000000000 and koboldai_vars.img_gen_priority <= 1:
#We could do it locally by swapping the model out
print("Could do local or online")
b64_data = text2img_api(", ".join(keys), art_guide = art_guide)
elif koboldai_vars.img_gen_priority != 0:
b64_data = text2img_api(", ".join(keys), art_guide = art_guide)
if ((not koboldai_vars.hascuda or not os.path.exists("models/stable-diffusion-v1-4")) and koboldai_vars.img_gen_priority != 0) or koboldai_vars.img_gen_priority == 3:
b64_data = text2img_horde(", ".join(keys), art_guide = art_guide)
else:
import psutil
#We aren't being forced to use horde, so now let's figure out if we should use local
if torch.cuda.get_device_properties(0).total_memory - torch.cuda.memory_reserved(0) >= 6000000000:
#He have enough vram, just do it locally
b64_data = text2img_local(", ".join(keys), art_guide = art_guide)
elif torch.cuda.get_device_properties(0).total_memory > 6000000000 and koboldai_vars.img_gen_priority <= 1:
#We could do it locally by swapping the model out
print("Could do local or online")
b64_data = text2img_horde(", ".join(keys), art_guide = art_guide)
elif koboldai_vars.img_gen_priority != 0:
b64_data = text2img_horde(", ".join(keys), art_guide = art_guide)
logger.debug("Time to Generate Image {}".format(time.time()-start_time))
koboldai_vars.picture = b64_data
koboldai_vars.picture_prompt = ", ".join(keys)
@@ -9195,20 +9199,54 @@ def text2img_horde(prompt,
koboldai_vars.generating_image = False
logger.error(submit_req.text)
#@logger.catch
@logger.catch
def text2img_api(prompt,
art_guide = 'fantasy illustration, artstation, by Hugin Miyama by Taiki Kawakami, cinematic lighting',
#art_guide = 'fantasy illustration, artstation, by Hugin Miyama by Taiki Kawakami, cinematic lighting',
art_guide = 'fantasy illustration, artstation, by jason felix by steve argyle by tyler jacobson by peter mohrbacher, cinematic lighting',
filename = "story_art.png"):
logger.debug("Generating Image using external API")
logger.debug("Generating Image using Local SD-WebUI API")
koboldai_vars.generating_image = True
#Add items that you want the AI to avoid in your image.
negprompt = 'lowres, bad anatomy, bad hands out of frame, two heads, totem pole, several faces, extra fingers, mutated hands, (poorly drawn hands:1.21), (poorly drawn face:1.21), (mutation:1.331), (deformed:1.331), (ugly:1.21), blurry, (bad anatomy:1.21), (bad proportions:1.331), (extra limbs:1.21), glitchy, ((clip through table)), adherent bodies, slimy bodies, (badly visible legs), captions, words'
#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.
#"enable_hr": false,
#"denoising_strength": 0,
#"firstphase_width": 0,
#"firstphase_height": 0,
#"prompt": "",
#"styles": [
# "string"
#],
#"seed": -1,
#"subseed": -1,
#"subseed_strength": 0,
#"seed_resize_from_h": -1,
#"seed_resize_from_w": -1,
#"batch_size": 1,
#"n_iter": 1,
#"steps": 50,
#"cfg_scale": 7,
#"width": 512,
#"height": 512,
#"restore_faces": false,
#"tiling": false,
#"negative_prompt": "string",
#"eta": 0,
#"s_churn": 0,
#"s_tmax": 0,
#"s_tmin": 0,
#"s_noise": 1,
#"override_settings": {},
#"sampler_index": "Euler"
final_imgen_params = {
"prompt": "{}, {}".format(prompt, art_guide),
"n": 1,
"width": 512,
"height": 512,
"steps": 50,
"cfg_scale": 14,
"negative_prompt": " lowres, bad anatomy, bad hands out of frame, two heads, totem pole, several faces, extra fingers, mutated hands, (poorly drawn hands:1.21), (poorly drawn face:1.21), (mutation:1.331), (deformed:1.331), (ugly:1.21), blurry, (bad anatomy:1.21), (bad proportions:1.331), (extra limbs:1.21), glitchy, ((clip through table)), adherent bodies, slimy bodies, (badly visible legs), captions, words",
"steps": 40,
"cfg_scale": 10,
"negative_prompt": "{}".format(negprompt),
"sampler_index": "Euler a"
}
final_submit_dict = {
@@ -9229,13 +9267,12 @@ def text2img_api(prompt,
base64_bytes = b64img.encode('utf-8')
img_bytes = base64.b64decode(base64_bytes)
img = Image.open(BytesIO(img_bytes))
#dtnow = datetime.datetime.now()
#dt_string = dtnow().strftime("%H%M%S%d%m%Y")
dt_string = datetime.datetime.now().strftime("%H%M%S%d%m%Y")
final_filename = "stories/art/{}_{}".format(dt_string,filename)
pnginfo = PngImagePlugin.PngInfo()
pnginfo.add_text("parameters", str(results['info']))
img.save(final_filename, pnginfo=pnginfo)
#img.save(final_filename)
logger.debug("Saved Image")
koboldai_vars.generating_image = False
return(b64img)

View File

@@ -551,7 +551,7 @@ gensettingstf = [
"sub_path": "Images",
"classname": "user",
"name": "img_gen_priority",
'children': [{'text': 'Use Local Only', 'value': 0}, {'text':'Prefer Local','value':1}, {'text':'Prefer Horde', 'value':2}, {'text':'Use Horde Only', 'value':3}]
'children': [{'text': 'Use Local Only', 'value': 0}, {'text':'Prefer Local','value':1}, {'text':'Prefer Horde', 'value':2}, {'text':'Use Horde Only', 'value':3}, {'text':'Use Local SD-WebUI API', 'value':4}]
},
{
"UI_V2_Only": True,