Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,118 +1,234 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
|
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 |
-
return
|
33 |
-
|
34 |
-
def
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
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 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from random import randint
|
3 |
+
from all_models import models
|
4 |
+
|
5 |
+
def load_fn(models):
|
6 |
+
global models_load
|
7 |
+
models_load = {}
|
8 |
+
for model in models:
|
9 |
+
if model not in models_load.keys():
|
10 |
+
try:
|
11 |
+
m = gr.load(f'models/{model}')
|
12 |
+
except Exception as error:
|
13 |
+
m = gr.Interface(lambda txt: None, ['text'], ['image'])
|
14 |
+
models_load.update({model: m})
|
15 |
+
|
16 |
+
load_fn(models)
|
17 |
+
|
18 |
+
num_models = len(models)
|
19 |
+
default_models = models[:num_models]
|
20 |
+
|
21 |
+
def extend_choices(choices):
|
22 |
+
return choices + (num_models - len(choices)) * ['NA']
|
23 |
+
|
24 |
+
def update_imgbox(choices):
|
25 |
+
choices_plus = extend_choices(choices)
|
26 |
+
return [gr.Image(None, label=m, visible=(m != 'NA'), elem_id="custom_image") for m in choices_plus]
|
27 |
+
|
28 |
+
def gen_fn(model_str, prompt):
|
29 |
+
if model_str == 'NA':
|
30 |
+
return None
|
31 |
+
noise = str(randint(0, 9999999))
|
32 |
+
return models_load[model_str](f'{prompt} {noise}')
|
33 |
+
|
34 |
+
def make_me():
|
35 |
+
with gr.Row():
|
36 |
+
with gr.Column(scale=1):
|
37 |
+
txt_input = gr.Textbox(label='Your prompt:', lines=3, container=False, elem_id="custom_textbox", placeholder="Prompt")
|
38 |
+
with gr.Row():
|
39 |
+
gen_button = gr.Button('Generate images', elem_id="custom_gen_button")
|
40 |
+
stop_button = gr.Button('Stop', variant='secondary', interactive=False, elem_id="custom_stop_button")
|
41 |
+
|
42 |
+
def on_generate_click():
|
43 |
+
return gr.Button('Generate images', elem_id="custom_gen_button"), gr.Button('Stop', variant='secondary', interactive=True, elem_id="custom_stop_button")
|
44 |
+
|
45 |
+
def on_stop_click():
|
46 |
+
return gr.Button('Generate images', elem_id="custom_gen_button"), gr.Button('Stop', variant='secondary', interactive=False, elem_id="custom_stop_button")
|
47 |
+
|
48 |
+
gen_button.click(on_generate_click, inputs=None, outputs=[gen_button, stop_button])
|
49 |
+
stop_button.click(on_stop_click, inputs=None, outputs=[gen_button, stop_button])
|
50 |
+
|
51 |
+
with gr.Row():
|
52 |
+
output = [gr.Image(label=m, min_width=250, height=250, elem_id="custom_image") for m in default_models]
|
53 |
+
current_models = [gr.Textbox(m, visible=False) for m in default_models]
|
54 |
+
for m, o in zip(current_models, output):
|
55 |
+
gen_event = gen_button.click(gen_fn, [m, txt_input], o)
|
56 |
+
stop_button.click(on_stop_click, inputs=None, outputs=[gen_button, stop_button], cancels=[gen_event])
|
57 |
+
|
58 |
+
with gr.Accordion('Model selection', elem_id="custom_accordion"):
|
59 |
+
model_choice = gr.CheckboxGroup(models, label=f'{num_models} different models selected', value=default_models, interactive=True, elem_id="custom_checkbox_group")
|
60 |
+
model_choice.change(update_imgbox, model_choice, output)
|
61 |
+
model_choice.change(extend_choices, model_choice, current_models)
|
62 |
+
|
63 |
+
with gr.Row():
|
64 |
+
gr.HTML("")
|
65 |
+
|
66 |
+
custom_css = """
|
67 |
+
:root {
|
68 |
+
--body-background-fill: #2d3d4f;
|
69 |
+
}
|
70 |
+
|
71 |
+
body {
|
72 |
+
background-color: var(--body-background-fill) !important;
|
73 |
+
color: #2d3d4f;
|
74 |
+
margin: 0;
|
75 |
+
padding: 0;
|
76 |
+
font-family: Arial, sans-serif;
|
77 |
+
height: 100vh;
|
78 |
+
overflow-y: auto;
|
79 |
+
}
|
80 |
+
|
81 |
+
.gradio-container {
|
82 |
+
background-color: #2d3d4f;
|
83 |
+
color: #c5c6c7;
|
84 |
+
padding: 20px;
|
85 |
+
border-radius: 8px;
|
86 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
|
87 |
+
width: 100%;
|
88 |
+
max-width: 1200px;
|
89 |
+
margin: 20px auto;
|
90 |
+
display: block;
|
91 |
+
min-height: 100vh;
|
92 |
+
}
|
93 |
+
|
94 |
+
.app_title {
|
95 |
+
background-color: #2d3d4f;
|
96 |
+
color: #c5c6c7;
|
97 |
+
padding: 10px 20px;
|
98 |
+
border-bottom: 1px solid #3b4252;
|
99 |
+
text-align: center;
|
100 |
+
font-size: 24px;
|
101 |
+
font-weight: bold;
|
102 |
+
width: 100%;
|
103 |
+
box-sizing: border-box;
|
104 |
+
margin-bottom: 20px;
|
105 |
+
}
|
106 |
+
|
107 |
+
.custom_textbox {
|
108 |
+
background-color: #2d343f;
|
109 |
+
border: 1px solid #3b4252;
|
110 |
+
color: #7f8184;
|
111 |
+
padding: 10px;
|
112 |
+
border-radius: 4px;
|
113 |
+
margin-bottom: 10px;
|
114 |
+
width: 100%;
|
115 |
+
box-sizing: border-box;
|
116 |
+
}
|
117 |
+
|
118 |
+
.custom_gen_button {
|
119 |
+
background-color: #8b38ff;
|
120 |
+
border: 1px solid #ffffff;
|
121 |
+
color: blue;
|
122 |
+
padding: 15px 32px;
|
123 |
+
text-align: center;
|
124 |
+
text-decoration: none;
|
125 |
+
display: inline-block;
|
126 |
+
font-size: 16px;
|
127 |
+
margin: 4px 2px;
|
128 |
+
cursor: pointer;
|
129 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
|
130 |
+
transition: transform 0.2s, box-shadow 0.2s;
|
131 |
+
border-radius: 4px;
|
132 |
+
}
|
133 |
+
.custom_gen_button:hover {
|
134 |
+
transform: translateY(-2px);
|
135 |
+
box-shadow: 0 6px 10px rgba(0, 0, 0, 0.3);
|
136 |
+
}
|
137 |
+
.custom_stop_button {
|
138 |
+
background-color: #6200ea;
|
139 |
+
border: 1px solid #ffffff;
|
140 |
+
color: blue;
|
141 |
+
padding: 15px 32px;
|
142 |
+
text-align: center;
|
143 |
+
text-decoration: none;
|
144 |
+
display: inline-block;
|
145 |
+
font-size: 16px;
|
146 |
+
margin: 4px 2px;
|
147 |
+
cursor: pointer;
|
148 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
|
149 |
+
transition: transform 0.2s, box-shadow 0.2s;
|
150 |
+
border-radius: 4px;
|
151 |
+
}
|
152 |
+
.custom_stop_button:hover {
|
153 |
+
transform: translateY(-2px);
|
154 |
+
box-shadow: 0 6px 10px rgba(0, 0, 0, 0.3);
|
155 |
+
}
|
156 |
+
|
157 |
+
.custom_image {
|
158 |
+
border: 1px solid #3b4252;
|
159 |
+
background-color: #2d343f;
|
160 |
+
border-radius: 4px;
|
161 |
+
margin: 10px;
|
162 |
+
max-width: 100%;
|
163 |
+
box-sizing: border-box;
|
164 |
+
}
|
165 |
+
|
166 |
+
.custom_accordion {
|
167 |
+
background-color: #2d3d4f;
|
168 |
+
color: #7f8184;
|
169 |
+
border: 1px solid #3b4252;
|
170 |
+
border-radius: 4px;
|
171 |
+
margin-top: 20px;
|
172 |
+
width: 100%;
|
173 |
+
box-sizing: border-box;
|
174 |
+
transition: margin 0.2s ease;
|
175 |
+
}
|
176 |
+
|
177 |
+
.custom_accordion .gr-accordion-header {
|
178 |
+
background-color: #2d3d4f;
|
179 |
+
color: #7f8184;
|
180 |
+
padding: 10px 20px;
|
181 |
+
border-bottom: 1px solid #5b6270;
|
182 |
+
cursor: pointer;
|
183 |
+
font-size: 18px;
|
184 |
+
font-weight: bold;
|
185 |
+
height: 40px;
|
186 |
+
display: flex;
|
187 |
+
align-items: center;
|
188 |
+
}
|
189 |
+
|
190 |
+
.custom_accordion .gr-accordion-header:hover {
|
191 |
+
background-color: #2d3d4f;
|
192 |
+
}
|
193 |
+
|
194 |
+
.custom_accordion .gr-accordion-content {
|
195 |
+
padding: 10px 20px;
|
196 |
+
background-color: #2d3d4f;
|
197 |
+
border-top: 1px solid #5b6270;
|
198 |
+
max-height: 0;
|
199 |
+
overflow: hidden;
|
200 |
+
transition: max-height 0.2s ease;
|
201 |
+
}
|
202 |
+
|
203 |
+
.custom_accordion .gr-accordion-content.open {
|
204 |
+
max-height: 500px;
|
205 |
+
}
|
206 |
+
|
207 |
+
.custom_checkbox_group {
|
208 |
+
background-color: #2d343f;
|
209 |
+
border: 1px solid #3b4252;
|
210 |
+
color: #7f8184;
|
211 |
+
border-radius: 4px;
|
212 |
+
padding: 10px;
|
213 |
+
width: 100%;
|
214 |
+
box-sizing: border-box;
|
215 |
+
}
|
216 |
+
|
217 |
+
@media (max-width: 768px) {
|
218 |
+
.gradio-container {
|
219 |
+
width: 100%;
|
220 |
+
margin: 0;
|
221 |
+
padding: 10px;
|
222 |
+
}
|
223 |
+
.custom_textbox,.custom_image,.custom_checkbox_group {
|
224 |
+
width: 100%;
|
225 |
+
box-sizing: border-box;
|
226 |
+
}
|
227 |
+
}
|
228 |
+
"""
|
229 |
+
|
230 |
+
with gr.Blocks(css=custom_css) as demo:
|
231 |
+
make_me()
|
232 |
+
|
233 |
+
demo.queue(concurrency_count=200)
|
234 |
+
demo.launch()
|