From 2e23a3ad31fb3b542f097f6fe8fdf1e5c0287f0b Mon Sep 17 00:00:00 2001 From: AIrjen Date: Tue, 20 Jun 2023 09:27:38 +0200 Subject: [PATCH] Added API stability reruns and proper error message also removed "None" upscaler --- call_extras.py | 20 +++++++++++++++++++- call_img2img.py | 21 +++++++++++++++++---- call_txt2img.py | 23 +++++++++++++++++++++-- model_lists.py | 1 + 4 files changed, 58 insertions(+), 7 deletions(-) diff --git a/call_extras.py b/call_extras.py index 7b7b489..62260c1 100644 --- a/call_extras.py +++ b/call_extras.py @@ -4,6 +4,7 @@ import io import base64 import uuid from PIL import Image, PngImagePlugin +import time @@ -46,8 +47,25 @@ def call_extras(imagelocation,originalimage, originalpnginfo ="", apiurl="http:/ "models": "None" # the remove backgrounds plugin is automatically turned on, need to turn it off } + response = [] + + # If we don't get an image back, we want to retry a few times. Max 3 times + for i in range(4): + response = requests.post(url=f'{url}/sdapi/v1/extra-single-image', json=payload) - response = requests.post(url=f'{url}/sdapi/v1/extra-single-image', json=payload) + r = response.json() + if("image" in r): + break # this means if we have the images object, then we "break" out of the for loop. + else: + if(i == 3): + print("If this keeps happening: Is WebUI started with --api enabled?") + print("") + raise ValueError("API has not been responding after several retries. Stopped processing.") + print("") + print("We haven't received an image from the API. Maybe something went wrong. Will retry after waiting a bit.") + + + time.sleep(10 * (i+1) ) # incremental waiting time image = Image.open(io.BytesIO(base64.b64decode(response.json().get("image")))) diff --git a/call_img2img.py b/call_img2img.py index dd3e902..f598d52 100644 --- a/call_img2img.py +++ b/call_img2img.py @@ -6,6 +6,7 @@ import uuid from PIL import Image, PngImagePlugin from modules import shared from model_lists import * +import time def call_img2img(imagelocation,originalimage, originalpnginfo ="", apiurl="http://127.0.0.1:7860",filename="", prompt = "", negativeprompt = "", img2imgsamplingsteps = "20", img2imgcfg = "7", img2imgsamplingmethod = "DPM++ SDE Karras", img2imgupscaler = "R-ESRGAN 4x+", img2imgmodel = "currently selected model", denoising_strength = "0.3", scale = "2", padding = "64",upscalescript="SD upscale",usdutilewidth = "512", usdutileheight = "0", usdumaskblur = "8", usduredraw ="Linear", usduSeamsfix = "None", usdusdenoise = "0.35", usduswidth = "64", usduspadding ="32", usdusmaskblur = "8",controlnetenabled=False, controlnetmodel="",controlnetblockymode=False): @@ -171,13 +172,25 @@ def call_img2img(imagelocation,originalimage, originalpnginfo ="", apiurl="http: # target_size_type = 2 # custom_scale = 2 + r = [] + # If we don't get an image back, we want to retry a few times. Max 3 times + for i in range(4): + response = requests.post(url=f'{url}/sdapi/v1/img2img', json=payload) - response = requests.post(url=f'{url}/sdapi/v1/img2img', json=payload) - - - r = response.json() + r = response.json() + if('images' in r): + break # this means if we have the images object, then we "break" out of the for loop. + else: + if(i == 3): + print("If this keeps happening: Is WebUI started with --api enabled?") + print("") + raise ValueError("API has not been responding after several retries. Stopped processing.") + print("") + print("We haven't received an image from the API. Maybe something went wrong. Will retry after waiting a bit.") + + time.sleep(10 * (i+1) ) # incremental waiting time for i in r['images']: image = Image.open(io.BytesIO(base64.b64decode(i.split(",",1)[0]))) diff --git a/call_txt2img.py b/call_txt2img.py index b120322..afebd2e 100644 --- a/call_txt2img.py +++ b/call_txt2img.py @@ -6,6 +6,7 @@ import uuid import sys, os from PIL import Image, PngImagePlugin from model_lists import * +import time def call_txt2img(passingprompt,ratio,upscale,debugmode,filename="",model = "currently selected model",samplingsteps = "40",cfg= "7",hiressteps ="0",denoisestrength="0.6",samplingmethod="DPM++ SDE Karras", upscaler="R-ESRGAN 4x+",hiresscale="2",apiurl="http://127.0.0.1:7860", qualitygate=False,quality="7.6",runs="5",negativeprompt=""): @@ -130,10 +131,28 @@ def call_txt2img(passingprompt,ratio,upscale,debugmode,filename="",model = "curr filenamefull = filename + addrun outputTXT2IMGFull = '{}{}{}'.format(outputTXT2IMGfolder,filenamefull,outputTXT2IMGpng) + r = [] + + # If we don't get an image back, we want to retry a few times. Max 3 times + for i in range(4): + response = requests.post(url=f'{url}/sdapi/v1/txt2img', json=payload) + + r = response.json() + if('images' in r): + break # this means if we have the images object, then we "break" out of the for loop. + else: + if(i == 3): + print("If this keeps happening: Is WebUI started with --api enabled?") + print("") + raise ValueError("API has not been responding after several retries. Stopped processing.") + print("") + print("We haven't received an image from the API. Maybe something went wrong. Will retry after waiting a bit.") + + + time.sleep(10 * (i+1) ) # incremental waiting time + - response = requests.post(url=f'{url}/sdapi/v1/txt2img', json=payload) - r = response.json() for i in r['images']: image = Image.open(io.BytesIO(base64.b64decode(i.split(",",1)[0]))) diff --git a/model_lists.py b/model_lists.py index 006ddff..9c82493 100644 --- a/model_lists.py +++ b/model_lists.py @@ -25,6 +25,7 @@ def get_samplers(): def get_upscalers_for_img2img(): upscalerlistfromwWebUI = [x.name for x in shared.sd_upscalers] + upscalerlistfromwWebUI.remove("None") return upscalerlistfromwWebUI def get_samplers_for_img2img():