Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -49,13 +49,50 @@ def gen_fn(model_str, prompt):
|
|
49 |
noise = str('') #str(randint(0, 99999999999))
|
50 |
return models_load[model_str](f'{prompt} {noise}')
|
51 |
|
52 |
-
def gen_fnseed(model_str, prompt, useseed, **kwargs):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
kwargs = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
if model_str == 'NA':
|
55 |
return None
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
def gen_fnsix(model_str, prompt):
|
61 |
if model_str == 'NA':
|
@@ -107,7 +144,7 @@ with gr.Blocks() as demo:
|
|
107 |
with gr.Tab('Seed it!'):
|
108 |
model_choiceseed = gr.Dropdown(models, label = f'Choose a model from the {len(models)} available! Try clearing the box and typing on it to filter them!', value = models[0], filterable = True)
|
109 |
txt_inputseed = gr.Textbox(label = 'Your prompt:')
|
110 |
-
|
111 |
|
112 |
max_imagesseed = 1
|
113 |
num_imagesseed = gr.Slider(1, max_imagesone, value = max_imagesone, step = 1, label = 'One, because more would make it produce identical images with the seed', visible = False)
|
@@ -126,7 +163,7 @@ with gr.Blocks() as demo:
|
|
126 |
|
127 |
gen_eventseed = gr.on(triggers=[gen_buttonseed.click, txt_inputseed.submit],
|
128 |
fn=lambda i, n, m, t, n1: gen_fnseed(m, t, n1) if (i < n) else None,
|
129 |
-
inputs=[img_is, num_imagesseed, model_choiceseed, txt_inputseed,
|
130 |
concurrency_limit=None, queue=False) # Be sure to delete ", queue=False" when activating the stop button
|
131 |
|
132 |
#stop_button.click(lambda s: gr.update(interactive = False), None, stop_button, cancels = [gen_event])
|
|
|
49 |
noise = str('') #str(randint(0, 99999999999))
|
50 |
return models_load[model_str](f'{prompt} {noise}')
|
51 |
|
52 |
+
#def gen_fnseed(model_str, prompt, useseed, **kwargs):
|
53 |
+
# kwargs = {}
|
54 |
+
# if model_str == 'NA':
|
55 |
+
# return None
|
56 |
+
# noise = str('') #str(randint(0, 99999999999))
|
57 |
+
# kwargs["seed"] = useseed
|
58 |
+
# return models_load[model_str](f'{prompt} {noise}', **kwargs)
|
59 |
+
|
60 |
+
async def infer(model_str, prompt, seed=1, timeout=inference_timeout):
|
61 |
+
from pathlib import Path
|
62 |
kwargs = {}
|
63 |
+
noise = ""
|
64 |
+
kwargs["seed"] = seed
|
65 |
+
task = asyncio.create_task(asyncio.to_thread(models_load[model_str].fn,
|
66 |
+
prompt=f'{prompt} {noise}', **kwargs, token=HF_TOKEN))
|
67 |
+
await asyncio.sleep(0)
|
68 |
+
try:
|
69 |
+
result = await asyncio.wait_for(task, timeout=timeout)
|
70 |
+
except (Exception, asyncio.TimeoutError) as e:
|
71 |
+
print(e)
|
72 |
+
print(f"Task timed out: {model_str}")
|
73 |
+
if not task.done(): task.cancel()
|
74 |
+
result = None
|
75 |
+
if task.done() and result is not None:
|
76 |
+
with lock:
|
77 |
+
png_path = "image.png"
|
78 |
+
result.save(png_path)
|
79 |
+
image = str(Path(png_path).resolve())
|
80 |
+
return image
|
81 |
+
return None
|
82 |
+
|
83 |
+
def gen_fnseed(model_str, prompt, seed=1):
|
84 |
if model_str == 'NA':
|
85 |
return None
|
86 |
+
try:
|
87 |
+
loop = asyncio.new_event_loop()
|
88 |
+
result = loop.run_until_complete(infer(model_str, prompt, seed, inference_timeout))
|
89 |
+
except (Exception, asyncio.CancelledError) as e:
|
90 |
+
print(e)
|
91 |
+
print(f"Task aborted: {model_str}")
|
92 |
+
result = None
|
93 |
+
finally:
|
94 |
+
loop.close()
|
95 |
+
return result
|
96 |
|
97 |
def gen_fnsix(model_str, prompt):
|
98 |
if model_str == 'NA':
|
|
|
144 |
with gr.Tab('Seed it!'):
|
145 |
model_choiceseed = gr.Dropdown(models, label = f'Choose a model from the {len(models)} available! Try clearing the box and typing on it to filter them!', value = models[0], filterable = True)
|
146 |
txt_inputseed = gr.Textbox(label = 'Your prompt:')
|
147 |
+
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)
|
148 |
|
149 |
max_imagesseed = 1
|
150 |
num_imagesseed = gr.Slider(1, max_imagesone, value = max_imagesone, step = 1, label = 'One, because more would make it produce identical images with the seed', visible = False)
|
|
|
163 |
|
164 |
gen_eventseed = gr.on(triggers=[gen_buttonseed.click, txt_inputseed.submit],
|
165 |
fn=lambda i, n, m, t, n1: gen_fnseed(m, t, n1) if (i < n) else None,
|
166 |
+
inputs=[img_is, num_imagesseed, model_choiceseed, txt_inputseed, seed], outputs=[o],
|
167 |
concurrency_limit=None, queue=False) # Be sure to delete ", queue=False" when activating the stop button
|
168 |
|
169 |
#stop_button.click(lambda s: gr.update(interactive = False), None, stop_button, cancels = [gen_event])
|