Update app.py
Browse files
app.py
CHANGED
@@ -1,85 +1,115 @@
|
|
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 |
-
m = gr.
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
num_models = 32
|
22 |
-
default_models = models[:num_models]
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
model_choice.
|
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 |
demo.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from random import randint
|
3 |
+
from all_models import models
|
4 |
+
from PIL import Image
|
5 |
+
import io
|
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 |
+
load_fn(models)
|
20 |
+
|
21 |
+
num_models = 32
|
22 |
+
default_models = models[:num_models]
|
23 |
+
|
24 |
+
def extend_choices(choices):
|
25 |
+
return choices + (num_models - len(choices)) * ['NA']
|
26 |
+
|
27 |
+
def update_imgbox(choices, height, width):
|
28 |
+
choices_plus = extend_choices(choices)
|
29 |
+
return [gr.Image(None, label=m, visible=(m != 'NA'), height=height, width=width) for m in choices_plus]
|
30 |
+
|
31 |
+
def resize_image(image, height, width):
|
32 |
+
"""Resize image to specified dimensions"""
|
33 |
+
if image is None:
|
34 |
+
return None
|
35 |
+
try:
|
36 |
+
# Convert Gradio image to PIL Image
|
37 |
+
pil_image = Image.fromarray(image)
|
38 |
+
# Resize while maintaining aspect ratio if one dimension is None
|
39 |
+
resized_image = pil_image.resize((width, height), Image.Resampling.LANCZOS)
|
40 |
+
return resized_image
|
41 |
+
except Exception as e:
|
42 |
+
return image # Return original if resize fails
|
43 |
+
|
44 |
+
def gen_fn(model_str, prompt, height, width):
|
45 |
+
if model_str == 'NA':
|
46 |
+
return None
|
47 |
+
noise = str(randint(0, 99999999999))
|
48 |
+
# Generate image
|
49 |
+
generated = models_load[model_str](f'{prompt} {noise}')
|
50 |
+
# Resize to specified dimensions
|
51 |
+
return resize_image(generated, height, width)
|
52 |
+
|
53 |
+
with gr.Blocks() as demo:
|
54 |
+
with gr.Tab('Multiple models'):
|
55 |
+
with gr.Accordion('Model selection'):
|
56 |
+
model_choice = gr.CheckboxGroup(models, label=f'Choose up to {num_models} different models', value=default_models, multiselect=True, max_choices=num_models, interactive=True, filterable=False)
|
57 |
+
|
58 |
+
with gr.Row():
|
59 |
+
img_height = gr.Slider(100, 1000, value=300, step=10, label='Image Height (px)')
|
60 |
+
img_width = gr.Slider(100, 1000, value=300, step=10, label='Image Width (px)')
|
61 |
+
|
62 |
+
txt_input = gr.Textbox(label='Prompt text')
|
63 |
+
gen_button = gr.Button('Generate')
|
64 |
+
stop_button = gr.Button('Stop', variant='secondary', interactive=False)
|
65 |
+
gen_button.click(lambda s: gr.update(interactive=True), None, stop_button)
|
66 |
+
|
67 |
+
with gr.Row():
|
68 |
+
output = [gr.Image(label=m, height=300, width=300) for m in default_models]
|
69 |
+
current_models = [gr.Textbox(m, visible=False) for m in default_models]
|
70 |
+
|
71 |
+
model_choice.change(update_imgbox, [model_choice, img_height, img_width], output)
|
72 |
+
model_choice.change(extend_choices, model_choice, current_models)
|
73 |
+
img_height.change(update_imgbox, [model_choice, img_height, img_width], output)
|
74 |
+
img_width.change(update_imgbox, [model_choice, img_height, img_width], output)
|
75 |
+
|
76 |
+
for m, o in zip(current_models, output):
|
77 |
+
gen_event = gen_button.click(
|
78 |
+
gen_fn,
|
79 |
+
[m, txt_input, img_height, img_width],
|
80 |
+
o,
|
81 |
+
queue=False
|
82 |
+
)
|
83 |
+
|
84 |
+
with gr.Tab('Single model'):
|
85 |
+
model_choice2 = gr.Dropdown(models, label='Choose model', value=models[0], filterable=False)
|
86 |
+
txt_input2 = gr.Textbox(label='Prompt text')
|
87 |
+
|
88 |
+
with gr.Row():
|
89 |
+
img_height2 = gr.Slider(100, 1000, value=300, step=10, label='Image Height (px)')
|
90 |
+
img_width2 = gr.Slider(100, 1000, value=300, step=10, label='Image Width (px)')
|
91 |
+
|
92 |
+
max_images = 16
|
93 |
+
num_images = gr.Slider(1, max_images, value=max_images, step=1, label='Number of images')
|
94 |
+
|
95 |
+
gen_button2 = gr.Button('Generate')
|
96 |
+
stop_button2 = gr.Button('Stop', variant='secondary', interactive=False)
|
97 |
+
gen_button2.click(lambda s: gr.update(interactive=True), None, stop_button2)
|
98 |
+
|
99 |
+
with gr.Row():
|
100 |
+
output2 = [gr.Image(label='', height=300, width=300) for _ in range(max_images)]
|
101 |
+
|
102 |
+
for i, o in enumerate(output2):
|
103 |
+
img_i = gr.Number(i, visible=False)
|
104 |
+
num_images.change(lambda i, n: gr.update(visible=(i < n)), [img_i, num_images], o)
|
105 |
+
img_height2.change(lambda h: gr.update(height=h), img_height2, o)
|
106 |
+
img_width2.change(lambda w: gr.update(width=w), img_width2, o)
|
107 |
+
gen_event2 = gen_button2.click(
|
108 |
+
lambda i, n, m, t, h, w: gen_fn(m, t, h, w) if (i < n) else None,
|
109 |
+
[img_i, num_images, model_choice2, txt_input2, img_height2, img_width2],
|
110 |
+
o
|
111 |
+
)
|
112 |
+
stop_button2.click(lambda s: gr.update(interactive=False), None, stop_button2, cancels=[gen_event2])
|
113 |
+
|
114 |
+
demo.queue(concurrency_count=36)
|
115 |
demo.launch()
|