Spaces:
Running
Running
Upload 2 files
Browse files- app.py +63 -17
- externalmod.py +1 -1
app.py
CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
|
|
2 |
from random import randint
|
3 |
from all_models import models
|
4 |
from externalmod import gr_Interface_load
|
|
|
5 |
|
6 |
|
7 |
def load_fn(models):
|
@@ -13,6 +14,7 @@ def load_fn(models):
|
|
13 |
try:
|
14 |
m = gr_Interface_load(f'models/{model}')
|
15 |
except Exception as error:
|
|
|
16 |
m = gr.Interface(lambda txt: None, ['text'], ['image'])
|
17 |
models_load.update({model: m})
|
18 |
|
@@ -24,25 +26,70 @@ num_models = 6
|
|
24 |
default_models = models[:num_models]
|
25 |
|
26 |
|
27 |
-
|
28 |
def extend_choices(choices):
|
29 |
-
return choices[
|
30 |
|
31 |
|
32 |
def update_imgbox(choices):
|
33 |
-
choices_plus = extend_choices(choices[
|
34 |
return [gr.Image(None, label = m, visible = (m != 'NA')) for m in choices_plus]
|
35 |
|
36 |
-
|
37 |
-
def
|
38 |
-
|
39 |
-
|
40 |
-
noise = str('') #str(randint(0, 99999999999))
|
41 |
-
return models_load[model_str](f'{prompt} {noise}')
|
42 |
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
with gr.Tab('The Dream'):
|
47 |
txt_input = gr.Textbox(label = 'Your prompt:', lines=4) #.style(container=False,min_width=1200)
|
48 |
gen_button = gr.Button('Generate up to 6 images in up to 3 minutes total')
|
@@ -61,19 +108,18 @@ with gr.Blocks() as demo:
|
|
61 |
"""
|
62 |
)
|
63 |
with gr.Row():
|
64 |
-
output = [gr.Image(label = m,
|
|
|
65 |
current_models = [gr.Textbox(m, visible = False) for m in default_models]
|
66 |
|
67 |
for m, o in zip(current_models, output):
|
68 |
gen_event = gen_button.click(gen_fn, [m, txt_input], o)
|
|
|
69 |
stop_button.click(lambda s: gr.update(interactive = False), None, stop_button, cancels = [gen_event])
|
70 |
with gr.Accordion('Model selection'):
|
71 |
-
#model_choice = gr.CheckboxGroup(models, label = f'Choose up to {num_models} different models from the 866 available!', value = default_models, multiselect = True, max_choices = num_models, interactive = True, filterable = False)
|
72 |
-
#model_choice.change(update_imgbox, model_choice, output)
|
73 |
-
#model_choice.change(extend_choices, model_choice, current_models)
|
74 |
model_choice = gr.CheckboxGroup(models, label = f'Choose up to {num_models} different models from the 866 available!', value = default_models, interactive = True)
|
75 |
-
#model_choice.change(lambda x: x[0:num_models], [model_choice], [model_choice])
|
76 |
model_choice.change(update_imgbox, model_choice, output)
|
|
|
77 |
model_choice.change(extend_choices, model_choice, current_models)
|
78 |
with gr.Row():
|
79 |
gr.HTML(
|
@@ -84,5 +130,5 @@ with gr.Blocks() as demo:
|
|
84 |
"""
|
85 |
)
|
86 |
|
87 |
-
demo.queue()
|
88 |
-
demo.launch()
|
|
|
2 |
from random import randint
|
3 |
from all_models import models
|
4 |
from externalmod import gr_Interface_load
|
5 |
+
import asyncio
|
6 |
|
7 |
|
8 |
def load_fn(models):
|
|
|
14 |
try:
|
15 |
m = gr_Interface_load(f'models/{model}')
|
16 |
except Exception as error:
|
17 |
+
print(error)
|
18 |
m = gr.Interface(lambda txt: None, ['text'], ['image'])
|
19 |
models_load.update({model: m})
|
20 |
|
|
|
26 |
default_models = models[:num_models]
|
27 |
|
28 |
|
|
|
29 |
def extend_choices(choices):
|
30 |
+
return choices[:num_models] + (num_models - len(choices[:num_models])) * ['NA']
|
31 |
|
32 |
|
33 |
def update_imgbox(choices):
|
34 |
+
choices_plus = extend_choices(choices[:num_models])
|
35 |
return [gr.Image(None, label = m, visible = (m != 'NA')) for m in choices_plus]
|
36 |
|
37 |
+
|
38 |
+
def update_imgbox_gallery(choices):
|
39 |
+
choices_plus = extend_choices(choices[:num_models])
|
40 |
+
return [gr.Gallery(None, label = m, visible = (m != 'NA')) for m in choices_plus]
|
|
|
|
|
41 |
|
42 |
|
43 |
+
async def infer(model_str, prompt, timeout):
|
44 |
+
noise = ""
|
45 |
+
rand = randint(1, 500)
|
46 |
+
for i in range(rand):
|
47 |
+
noise += " "
|
48 |
+
task = asyncio.create_task(asyncio.to_thread(models_load[model_str], f'{prompt} {noise}'))
|
49 |
+
await asyncio.sleep(0)
|
50 |
+
try:
|
51 |
+
result = await asyncio.wait_for(task, timeout=timeout)
|
52 |
+
except Exception as e:
|
53 |
+
print(e)
|
54 |
+
task.cancel()
|
55 |
+
result = None
|
56 |
+
return result
|
57 |
|
58 |
+
|
59 |
+
def gen_fn(model_str, prompt):
|
60 |
+
if model_str == 'NA':
|
61 |
+
return None
|
62 |
+
try:
|
63 |
+
loop = asyncio.new_event_loop()
|
64 |
+
result = loop.run_until_complete(infer(model_str, prompt, 60))
|
65 |
+
except Exception as e:
|
66 |
+
print(e)
|
67 |
+
result = None
|
68 |
+
finally:
|
69 |
+
loop.close()
|
70 |
+
return result
|
71 |
+
|
72 |
+
|
73 |
+
def gen_fn_gallery(model_str, prompt, gallery):
|
74 |
+
if gallery is None: gallery = []
|
75 |
+
if model_str == 'NA':
|
76 |
+
yield gallery
|
77 |
+
try:
|
78 |
+
loop = asyncio.new_event_loop()
|
79 |
+
result = loop.run_until_complete(infer(model_str, prompt, 60))
|
80 |
+
if result: gallery.append(result)
|
81 |
+
except Exception as e:
|
82 |
+
print(e)
|
83 |
+
finally:
|
84 |
+
loop.close()
|
85 |
+
yield gallery
|
86 |
+
|
87 |
+
|
88 |
+
CSS="""
|
89 |
+
.output { width=480px; height=480px !important; }
|
90 |
+
"""
|
91 |
+
|
92 |
+
with gr.Blocks(css=CSS) as demo:
|
93 |
with gr.Tab('The Dream'):
|
94 |
txt_input = gr.Textbox(label = 'Your prompt:', lines=4) #.style(container=False,min_width=1200)
|
95 |
gen_button = gr.Button('Generate up to 6 images in up to 3 minutes total')
|
|
|
108 |
"""
|
109 |
)
|
110 |
with gr.Row():
|
111 |
+
output = [gr.Image(label = m, show_download_button=True, elem_classes="output", show_share_button=True) for m in default_models]
|
112 |
+
#output = [gr.Gallery(label = m, show_download_button=True, elem_classes="output", interactive=False, show_share_button=True, container=True, format="png", object_fit="contain") for m in default_models]
|
113 |
current_models = [gr.Textbox(m, visible = False) for m in default_models]
|
114 |
|
115 |
for m, o in zip(current_models, output):
|
116 |
gen_event = gen_button.click(gen_fn, [m, txt_input], o)
|
117 |
+
#gen_event = gen_button.click(gen_fn_gallery, [m, txt_input, o], o)
|
118 |
stop_button.click(lambda s: gr.update(interactive = False), None, stop_button, cancels = [gen_event])
|
119 |
with gr.Accordion('Model selection'):
|
|
|
|
|
|
|
120 |
model_choice = gr.CheckboxGroup(models, label = f'Choose up to {num_models} different models from the 866 available!', value = default_models, interactive = True)
|
|
|
121 |
model_choice.change(update_imgbox, model_choice, output)
|
122 |
+
#model_choice.change(update_imgbox_gallery, model_choice, output)
|
123 |
model_choice.change(extend_choices, model_choice, current_models)
|
124 |
with gr.Row():
|
125 |
gr.HTML(
|
|
|
130 |
"""
|
131 |
)
|
132 |
|
133 |
+
demo.queue()
|
134 |
+
demo.launch()
|
externalmod.py
CHANGED
@@ -115,7 +115,7 @@ def from_model(model_name: str, hf_token: str | None, alias: str | None, **kwarg
|
|
115 |
|
116 |
headers["X-Wait-For-Model"] = "true"
|
117 |
client = huggingface_hub.InferenceClient(
|
118 |
-
model=model_name, headers=headers, token=hf_token
|
119 |
)
|
120 |
|
121 |
# For tasks that are not yet supported by the InferenceClient
|
|
|
115 |
|
116 |
headers["X-Wait-For-Model"] = "true"
|
117 |
client = huggingface_hub.InferenceClient(
|
118 |
+
model=model_name, headers=headers, token=hf_token, timeout=120,
|
119 |
)
|
120 |
|
121 |
# For tasks that are not yet supported by the InferenceClient
|