infnapitoggle / handle_models.py
charliebaby2023's picture
Update handle_models.py
3e60998 verified
raw
history blame
2.57 kB
from config import howManyModelsToUse,num_models,max_images,inference_timeout,MAX_SEED,thePrompt,preSetPrompt,negPreSetPrompt
def load_fn(models):
global models_load
models_load = {}
for model in models:
if model not in models_load.keys():
try:
m = gr_Interface_load(f'models/{model}', hf_token=HF_TOKEN)
except Exception as error:
print(error)
m = gr.Interface(lambda: None, ['text'], ['image'])
models_load.update({model: m})
async def infer(model_str, prompt, nprompt="", height=0, width=0, steps=0, cfg=0, seed=-1, timeout=inference_timeout):
kwargs = {}
if height > 0: kwargs["height"] = height
if width > 0: kwargs["width"] = width
if steps > 0: kwargs["num_inference_steps"] = steps
if cfg > 0: cfg = kwargs["guidance_scale"] = cfg
if seed == -1:
theSeed = randomize_seed()
else:
theSeed = seed
kwargs["seed"] = theSeed
task = asyncio.create_task(asyncio.to_thread(models_load[model_str].fn, prompt=prompt, negative_prompt=nprompt, **kwargs, token=HF_TOKEN))
await asyncio.sleep(0)
try:
result = await asyncio.wait_for(task, timeout=timeout)
except asyncio.TimeoutError as e:
print(e)
print(f"infer: Task timed out: {model_str}")
if not task.done(): task.cancel()
result = None
raise Exception(f"Task timed out: {model_str}") from e
except Exception as e:
print(e)
print(f"infer: exception: {model_str}")
if not task.done(): task.cancel()
result = None
raise Exception() from e
if task.done() and result is not None and not isinstance(result, tuple):
with lock:
png_path = model_str.replace("/", "_") + " - " + get_current_time() + "_" + str(theSeed) + ".png"
image = save_image(result, png_path, model_str, prompt, nprompt, height, width, steps, cfg, theSeed)
return image
return None
def gen_fn(model_str, prompt, nprompt="", height=0, width=0, steps=0, cfg=0, seed=-1):
try:
loop = asyncio.new_event_loop()
result = loop.run_until_complete(infer(model_str, prompt, nprompt,
height, width, steps, cfg, seed, inference_timeout))
except (Exception, asyncio.CancelledError) as e:
print(e)
print(f"gen_fn: Task aborted: {model_str}")
result = None
raise gr.Error(f"Task aborted: {model_str}, Error: {e}")
finally:
loop.close()
return result