Spaces:
Runtime error
Runtime error
ccxccc
commited on
Commit
•
dd80569
1
Parent(s):
8f8f343
Update app.py
Browse files
app.py
CHANGED
@@ -2,83 +2,61 @@ import gradio as gr
|
|
2 |
from random import randint
|
3 |
from all_models import models
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
m = gr.Interface(lambda txt: None, ['text'], ['image'])
|
17 |
-
models_load.update({model: m})
|
18 |
-
|
19 |
-
|
20 |
-
load_fn(models)
|
21 |
-
|
22 |
|
23 |
num_models = 6
|
24 |
default_models = models[:num_models]
|
25 |
|
|
|
|
|
26 |
|
|
|
|
|
|
|
27 |
|
28 |
-
def
|
29 |
-
|
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 gen_fn(model_str, prompt):
|
38 |
-
if model_str == 'NA':
|
39 |
return None
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
|
45 |
with gr.Blocks() as demo:
|
46 |
-
with gr.Tab(
|
47 |
-
txt_input = gr.Textbox(label
|
48 |
-
gen_button = gr.Button(
|
49 |
-
stop_button = gr.Button(
|
50 |
-
gen_button.click(lambda
|
51 |
-
|
52 |
-
"""
|
53 |
-
<div style="text-align: center; max-width: 1200px; margin: 0 auto;">
|
54 |
-
<div>
|
55 |
-
<body>
|
56 |
-
<div class="center"><p style="margin-bottom: 10px; color: #000000;">Scroll down to see more images and select models.</p>
|
57 |
-
</div>
|
58 |
-
</body>
|
59 |
-
</div>
|
60 |
-
</div>
|
61 |
-
"""
|
62 |
-
)
|
63 |
with gr.Row():
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
for
|
68 |
-
gen_event = gen_button.click(
|
69 |
-
stop_button.click(lambda
|
70 |
-
|
71 |
-
|
72 |
-
model_choice.
|
73 |
-
model_choice.change(
|
|
|
|
|
74 |
with gr.Row():
|
75 |
-
gr.HTML(
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
"""
|
81 |
-
)
|
82 |
|
83 |
-
demo.queue(concurrency_count
|
84 |
-
demo.launch()
|
|
|
2 |
from random import randint
|
3 |
from all_models import models
|
4 |
|
5 |
+
def load_models(model_names):
|
6 |
+
loaded_models = {}
|
7 |
+
for model_name in model_names:
|
8 |
+
try:
|
9 |
+
model = gr.load(f'models/{model_name}')
|
10 |
+
except Exception:
|
11 |
+
model = gr.Interface(lambda txt: "Model load error", inputs=["text"], outputs=["image"])
|
12 |
+
loaded_models[model_name] = model
|
13 |
+
return loaded_models
|
14 |
+
|
15 |
+
models_load = load_models(models)
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
num_models = 6
|
18 |
default_models = models[:num_models]
|
19 |
|
20 |
+
def extend_choices(choices, num_models=6):
|
21 |
+
return choices + ['NA'] * (num_models - len(choices))
|
22 |
|
23 |
+
def update_image_boxes(choices):
|
24 |
+
extended_choices = extend_choices(choices)
|
25 |
+
return [gr.Image(label=model, visible=(model != 'NA')) for model in extended_choices]
|
26 |
|
27 |
+
def generate_image(model_name, prompt):
|
28 |
+
if model_name == 'NA':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
return None
|
30 |
+
# If you decide to add randomness to the prompt, uncomment the next line
|
31 |
+
# prompt += f" {randint(0, 999999999)}"
|
32 |
+
return models_load[model_name](prompt)
|
|
|
33 |
|
34 |
with gr.Blocks() as demo:
|
35 |
+
with gr.Tab("The Dream"):
|
36 |
+
txt_input = gr.Textbox(label="Your prompt:", lines=4).style(container=False, min_width=1200)
|
37 |
+
gen_button = gr.Button("Generate up to 6 images")
|
38 |
+
stop_button = gr.Button("Stop", variant="secondary")
|
39 |
+
gen_button.click(lambda _: gr.update(interactive=True), None, stop_button)
|
40 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
with gr.Row():
|
42 |
+
output_images = [gr.Image(label=model, min_width=480) for model in default_models]
|
43 |
+
model_textboxes = [gr.Textbox(model, visible=False) for model in default_models]
|
44 |
+
|
45 |
+
for model_textbox, output_image in zip(model_textboxes, output_images):
|
46 |
+
gen_event = gen_button.click(generate_image, [model_textbox, txt_input], output_image)
|
47 |
+
stop_button.click(lambda _: gr.update(interactive=False), None, stop_button, cancels=[gen_event])
|
48 |
+
|
49 |
+
with gr.Accordion("Model selection"):
|
50 |
+
model_choice = gr.CheckboxGroup(choices=models, label=f"Choose up to {num_models} models from the 686 available!", value=default_models, multiselect=True, max_choices=num_models, interactive=True)
|
51 |
+
model_choice.change(update_image_boxes, model_choice, output_images)
|
52 |
+
model_choice.change(lambda choices: extend_choices(choices, num_models), model_choice, model_textboxes)
|
53 |
+
|
54 |
with gr.Row():
|
55 |
+
gr.HTML("""
|
56 |
+
<div class="footer">
|
57 |
+
<p>Based on spaces by derwahnsinn, RdnUser77, and the Omnibus Maximum Multiplier!</p>
|
58 |
+
</div>
|
59 |
+
""")
|
|
|
|
|
60 |
|
61 |
+
demo.queue(concurrency_count=200)
|
62 |
+
demo.launch()
|