Spaces:
Running
Running
Upload app.py
Browse files
app.py
CHANGED
@@ -1,213 +1,84 @@
|
|
1 |
-
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 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
return [
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
image = str(Path(png_path).resolve())
|
86 |
-
return image
|
87 |
-
return None
|
88 |
-
|
89 |
-
|
90 |
-
def gen_fn(model_str, prompt, nprompt="", height=None, width=None, steps=None, cfg=None, seed=-1):
|
91 |
-
try:
|
92 |
-
loop = asyncio.new_event_loop()
|
93 |
-
result = loop.run_until_complete(infer(model_str, prompt, nprompt,
|
94 |
-
height, width, steps, cfg, seed, inference_timeout))
|
95 |
-
except (Exception, asyncio.CancelledError) as e:
|
96 |
-
print(e)
|
97 |
-
print(f"Task aborted: {model_str}")
|
98 |
-
result = None
|
99 |
-
raise gr.Error(f"Task aborted: {model_str}, Error: {e}")
|
100 |
-
finally:
|
101 |
-
loop.close()
|
102 |
-
return result
|
103 |
-
|
104 |
-
|
105 |
-
def add_gallery(image, model_str, gallery):
|
106 |
-
if gallery is None: gallery = []
|
107 |
-
with lock:
|
108 |
-
if image is not None: gallery.insert(0, (image, model_str))
|
109 |
-
return gallery
|
110 |
-
|
111 |
-
|
112 |
-
CSS="""
|
113 |
-
.gradio-container { max-width: 1200px; margin: 0 auto; !important; }
|
114 |
-
.output { width=112px; height=112px; max_width=112px; max_height=112px; !important; }
|
115 |
-
.gallery { min_width=512px; min_height=512px; max_height=1024px; !important; }
|
116 |
-
.guide { text-align: center; !important; }
|
117 |
-
"""
|
118 |
-
|
119 |
-
|
120 |
-
with gr.Blocks(theme='Nymbo/Nymbo_Theme', fill_width=True, css=CSS) as demo:
|
121 |
-
with gr.Tab('The Dream'):
|
122 |
-
with gr.Column(scale=2):
|
123 |
-
with gr.Group():
|
124 |
-
txt_input = gr.Textbox(label='Your prompt:', lines=4)
|
125 |
-
neg_input = gr.Textbox(label='Negative prompt:', lines=1)
|
126 |
-
with gr.Accordion("Advanced", open=False, visible=True):
|
127 |
-
with gr.Row():
|
128 |
-
width = gr.Slider(label="Width", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
|
129 |
-
height = gr.Slider(label="Height", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
|
130 |
-
with gr.Row():
|
131 |
-
steps = gr.Slider(label="Number of inference steps", info="If 0, the default value is used.", maximum=100, step=1, value=0)
|
132 |
-
cfg = gr.Slider(label="Guidance scale", info="If 0, the default value is used.", maximum=30.0, step=0.1, value=0)
|
133 |
-
seed = gr.Slider(label="Seed", info="Randomize Seed if -1.", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
|
134 |
-
with gr.Row():
|
135 |
-
gen_button = gr.Button(f'Generate up to {int(num_models)} images in up to 3 minutes total', variant='primary', scale=3)
|
136 |
-
random_button = gr.Button(f'Random {int(num_models)} 🎲', variant='secondary', scale=1)
|
137 |
-
#stop_button = gr.Button('Stop', variant='stop', interactive=False, scale=1)
|
138 |
-
#gen_button.click(lambda: gr.update(interactive=True), None, stop_button)
|
139 |
-
gr.Markdown("Scroll down to see more images and select models.", elem_classes="guide")
|
140 |
-
|
141 |
-
with gr.Column(scale=1):
|
142 |
-
with gr.Group():
|
143 |
-
with gr.Row():
|
144 |
-
output = [gr.Image(label=m, show_download_button=True, elem_classes="output",
|
145 |
-
interactive=False, min_width=80, show_share_button=False, format="png",
|
146 |
-
visible=True) for m in default_models]
|
147 |
-
current_models = [gr.Textbox(m, visible=False) for m in default_models]
|
148 |
-
|
149 |
-
with gr.Column(scale=2):
|
150 |
-
gallery = gr.Gallery(label="Output", show_download_button=True, elem_classes="gallery",
|
151 |
-
interactive=False, show_share_button=True, container=True, format="png",
|
152 |
-
preview=True, object_fit="cover", columns=2, rows=2)
|
153 |
-
|
154 |
-
for m, o in zip(current_models, output):
|
155 |
-
gen_event = gr.on(triggers=[gen_button.click, txt_input.submit], fn=gen_fn,
|
156 |
-
inputs=[m, txt_input, neg_input, height, width, steps, cfg, seed], outputs=[o], concurrency_limit=None, queue=False)
|
157 |
-
o.change(add_gallery, [o, m, gallery], [gallery])
|
158 |
-
#stop_button.click(lambda: gr.update(interactive=False), None, stop_button, cancels=[gen_event])
|
159 |
-
|
160 |
-
with gr.Column(scale=4):
|
161 |
-
with gr.Accordion('Model selection'):
|
162 |
-
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)
|
163 |
-
model_choice.change(update_imgbox, model_choice, output)
|
164 |
-
model_choice.change(extend_choices, model_choice, current_models)
|
165 |
-
random_button.click(random_choices, None, model_choice)
|
166 |
-
|
167 |
-
with gr.Tab('Single model'):
|
168 |
-
with gr.Column(scale=2):
|
169 |
-
model_choice2 = gr.Dropdown(models, label='Choose model', value=models[0])
|
170 |
-
with gr.Group():
|
171 |
-
txt_input2 = gr.Textbox(label='Your prompt:', lines=4)
|
172 |
-
neg_input2 = gr.Textbox(label='Negative prompt:', lines=1)
|
173 |
-
with gr.Accordion("Advanced", open=False, visible=True):
|
174 |
-
with gr.Row():
|
175 |
-
width2 = gr.Slider(label="Width", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
|
176 |
-
height2 = gr.Slider(label="Height", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
|
177 |
-
with gr.Row():
|
178 |
-
steps2 = gr.Slider(label="Number of inference steps", info="If 0, the default value is used.", maximum=100, step=1, value=0)
|
179 |
-
cfg2 = gr.Slider(label="Guidance scale", info="If 0, the default value is used.", maximum=30.0, step=0.1, value=0)
|
180 |
-
seed2 = gr.Slider(label="Seed", info="Randomize Seed if -1.", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
|
181 |
-
num_images = gr.Slider(1, max_images, value=max_images, step=1, label='Number of images')
|
182 |
-
with gr.Row():
|
183 |
-
gen_button2 = gr.Button('Generate', variant='primary', scale=2)
|
184 |
-
#stop_button2 = gr.Button('Stop', variant='stop', interactive=False, scale=1)
|
185 |
-
#gen_button2.click(lambda: gr.update(interactive=True), None, stop_button2)
|
186 |
-
|
187 |
-
with gr.Column(scale=1):
|
188 |
-
with gr.Group():
|
189 |
-
with gr.Row():
|
190 |
-
output2 = [gr.Image(label='', show_download_button=True, elem_classes="output",
|
191 |
-
interactive=False, min_width=80, visible=True, format="png",
|
192 |
-
show_share_button=False, show_label=False) for _ in range(max_images)]
|
193 |
-
|
194 |
-
with gr.Column(scale=2):
|
195 |
-
gallery2 = gr.Gallery(label="Output", show_download_button=True, elem_classes="gallery",
|
196 |
-
interactive=False, show_share_button=True, container=True, format="png",
|
197 |
-
preview=True, object_fit="cover", columns=2, rows=2)
|
198 |
-
|
199 |
-
for i, o in enumerate(output2):
|
200 |
-
img_i = gr.Number(i, visible=False)
|
201 |
-
num_images.change(lambda i, n: gr.update(visible = (i < n)), [img_i, num_images], o, queue=False)
|
202 |
-
gen_event2 = gr.on(triggers=[gen_button2.click, txt_input2.submit],
|
203 |
-
fn=lambda i, n, m, t1, t2, n1, n2, n3, n4, n5: gen_fn(m, t1, t2, n1, n2, n3, n4, n5) if (i < n) else None,
|
204 |
-
inputs=[img_i, num_images, model_choice2, txt_input2, neg_input2,
|
205 |
-
height2, width2, steps2, cfg2, seed2], outputs=[o], concurrency_limit=None, queue=False)
|
206 |
-
o.change(add_gallery, [o, model_choice2, gallery2], [gallery2])
|
207 |
-
#stop_button2.click(lambda: gr.update(interactive=False), None, stop_button2, cancels=[gen_event2])
|
208 |
-
|
209 |
-
gr.Markdown("Based on the [TestGen](https://huggingface.co/spaces/derwahnsinn/TestGen) Space by derwahnsinn, the [SpacIO](https://huggingface.co/spaces/RdnUser77/SpacIO_v1) Space by RdnUser77 and Omnibus's Maximum Multiplier!")
|
210 |
-
|
211 |
-
demo.queue(default_concurrency_limit=200, max_size=200)
|
212 |
-
demo.launch(show_api=False, max_threads=400)
|
213 |
-
# https://github.com/gradio-app/gradio/issues/6339
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from random import randint
|
3 |
+
from all_models import models
|
4 |
+
|
5 |
+
|
6 |
+
|
7 |
+
def load_fn(models):
|
8 |
+
global models_load
|
9 |
+
models_load = {}
|
10 |
+
|
11 |
+
for model in models:
|
12 |
+
if model not in models_load.keys():
|
13 |
+
try:
|
14 |
+
m = gr.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 |
+
|
19 |
+
|
20 |
+
load_fn(models)
|
21 |
+
|
22 |
+
|
23 |
+
num_models = 2
|
24 |
+
default_models = models[:num_models]
|
25 |
+
|
26 |
+
|
27 |
+
|
28 |
+
def extend_choices(choices):
|
29 |
+
return choices + (num_models - len(choices)) * ['NA']
|
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 |
+
noise = str('') #str(randint(0, 99999999999))
|
41 |
+
return models_load[model_str](f'{prompt} {noise}')
|
42 |
+
|
43 |
+
|
44 |
+
|
45 |
+
with gr.Blocks() as demo:
|
46 |
+
with gr.Tab('Toy World'):
|
47 |
+
txt_input = gr.Textbox(label = 'Your prompt:', lines=4).style(container=False,min_width=1200)
|
48 |
+
gen_button = gr.Button('Generate up to 2 images in up to 20 seconds')
|
49 |
+
stop_button = gr.Button('Stop', variant = 'secondary', interactive = False)
|
50 |
+
gen_button.click(lambda s: gr.update(interactive = True), None, stop_button)
|
51 |
+
gr.HTML(
|
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 |
+
output = [gr.Image(label = m, min_width=480) for m in default_models]
|
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 2 available! Untick them to only use one!', 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 |
+
with gr.Row():
|
75 |
+
gr.HTML(
|
76 |
+
"""
|
77 |
+
<div class="footer">
|
78 |
+
<p> Use 901 models up to six at a time at <a href="https://huggingface.co/spaces/Yntec/ToyWorld">ToyWorld</a></a>!
|
79 |
+
</p>
|
80 |
+
"""
|
81 |
+
)
|
82 |
+
|
83 |
+
demo.queue(concurrency_count = 200)
|
84 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|