Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -33,6 +33,8 @@ num_models = 6
|
|
33 |
default_models = models[:num_models]
|
34 |
inference_timeout = 600
|
35 |
|
|
|
|
|
36 |
def extend_choices(choices):
|
37 |
return choices[:num_models] + (num_models - len(choices[:num_models])) * ['NA']
|
38 |
|
@@ -40,12 +42,44 @@ def extend_choices(choices):
|
|
40 |
def update_imgbox(choices):
|
41 |
choices_plus = extend_choices(choices[:num_models])
|
42 |
return [gr.Image(None, label=m, visible=(m!='NA')) for m in choices_plus]
|
43 |
-
|
44 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
if model_str == 'NA':
|
46 |
return None
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
|
51 |
|
@@ -54,6 +88,7 @@ with gr.Blocks() as demo:
|
|
54 |
txt_input = gr.Textbox(label='Your prompt:', lines=4)
|
55 |
gen_button = gr.Button('Generate up to 6 images in up to 3 minutes total')
|
56 |
#stop_button = gr.Button('Stop', variant = 'secondary', interactive = False)
|
|
|
57 |
gen_button.click(lambda s: gr.update(interactive = True), None)
|
58 |
gr.HTML(
|
59 |
"""
|
@@ -72,8 +107,8 @@ with gr.Blocks() as demo:
|
|
72 |
current_models = [gr.Textbox(m, visible = False) for m in default_models]
|
73 |
|
74 |
for m, o in zip(current_models, output):
|
75 |
-
gen_event = gr.on(triggers=[gen_button.click, txt_input.submit], fn=
|
76 |
-
inputs=[m, txt_input], outputs=[o], concurrency_limit=None, queue=False)
|
77 |
#stop_button.click(lambda s: gr.update(interactive = False), None, stop_button, cancels = [gen_event])
|
78 |
with gr.Accordion('Model selection'):
|
79 |
model_choice = gr.CheckboxGroup(models, label = f'Choose up to {int(num_models)} different models from the {len(models)} available!', value=default_models, interactive=True)
|
|
|
33 |
default_models = models[:num_models]
|
34 |
inference_timeout = 600
|
35 |
|
36 |
+
MAX_SEED=3999999999
|
37 |
+
|
38 |
def extend_choices(choices):
|
39 |
return choices[:num_models] + (num_models - len(choices[:num_models])) * ['NA']
|
40 |
|
|
|
42 |
def update_imgbox(choices):
|
43 |
choices_plus = extend_choices(choices[:num_models])
|
44 |
return [gr.Image(None, label=m, visible=(m!='NA')) for m in choices_plus]
|
45 |
+
|
46 |
+
async def infer(model_str, prompt, seed=1, timeout=inference_timeout):
|
47 |
+
from pathlib import Path
|
48 |
+
kwargs = {}
|
49 |
+
noise = ""
|
50 |
+
kwargs["seed"] = seed
|
51 |
+
task = asyncio.create_task(asyncio.to_thread(models_load[model_str].fn,
|
52 |
+
prompt=f'{prompt} {noise}', **kwargs, token=HF_TOKEN))
|
53 |
+
await asyncio.sleep(0)
|
54 |
+
try:
|
55 |
+
result = await asyncio.wait_for(task, timeout=timeout)
|
56 |
+
except (Exception, asyncio.TimeoutError) as e:
|
57 |
+
print(e)
|
58 |
+
print(f"Task timed out: {model_str}")
|
59 |
+
if not task.done(): task.cancel()
|
60 |
+
result = None
|
61 |
+
if task.done() and result is not None:
|
62 |
+
with lock:
|
63 |
+
png_path = "image.png"
|
64 |
+
result.save(png_path)
|
65 |
+
image = str(Path(png_path).resolve())
|
66 |
+
return image
|
67 |
+
return None
|
68 |
+
|
69 |
+
|
70 |
+
def gen_fnseed(model_str, prompt, seed=1):
|
71 |
if model_str == 'NA':
|
72 |
return None
|
73 |
+
try:
|
74 |
+
loop = asyncio.new_event_loop()
|
75 |
+
result = loop.run_until_complete(infer(model_str, prompt, seed, inference_timeout))
|
76 |
+
except (Exception, asyncio.CancelledError) as e:
|
77 |
+
print(e)
|
78 |
+
print(f"Task aborted: {model_str}")
|
79 |
+
result = None
|
80 |
+
finally:
|
81 |
+
loop.close()
|
82 |
+
return result
|
83 |
|
84 |
|
85 |
|
|
|
88 |
txt_input = gr.Textbox(label='Your prompt:', lines=4)
|
89 |
gen_button = gr.Button('Generate up to 6 images in up to 3 minutes total')
|
90 |
#stop_button = gr.Button('Stop', variant = 'secondary', interactive = False)
|
91 |
+
seed = gr.Slider(label="Use a seed to replicate the same image later", info="Max 3999999999", minimum=0, maximum=MAX_SEED, step=1, value=1)
|
92 |
gen_button.click(lambda s: gr.update(interactive = True), None)
|
93 |
gr.HTML(
|
94 |
"""
|
|
|
107 |
current_models = [gr.Textbox(m, visible = False) for m in default_models]
|
108 |
|
109 |
for m, o in zip(current_models, output):
|
110 |
+
gen_event = gr.on(triggers=[gen_button.click, txt_input.submit], fn=gen_fnseed,
|
111 |
+
inputs=[m, txt_input, seed], outputs=[o], concurrency_limit=None, queue=False)
|
112 |
#stop_button.click(lambda s: gr.update(interactive = False), None, stop_button, cancels = [gen_event])
|
113 |
with gr.Accordion('Model selection'):
|
114 |
model_choice = gr.CheckboxGroup(models, label = f'Choose up to {int(num_models)} different models from the {len(models)} available!', value=default_models, interactive=True)
|