app.py
Browse files
app.py
CHANGED
@@ -1,115 +1,221 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
|
|
3 |
|
4 |
-
models
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
model_idx = 1
|
15 |
-
for model_path in models:
|
16 |
-
try:
|
17 |
-
model_functions[model_idx] = gr.load(f"models/{model_path}", live=False)
|
18 |
-
except Exception as error:
|
19 |
-
def the_fn(txt):
|
20 |
-
return None
|
21 |
-
model_functions[model_idx] = gr.Interface(fn=the_fn, inputs=["text"], outputs=["image"])
|
22 |
-
model_idx += 1
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
output = (model_functions.get(str(idx)) or model_functions.get(str(1)))(prompt)
|
27 |
-
return output
|
28 |
-
return send_it_fn
|
29 |
|
30 |
-
def
|
31 |
-
return
|
32 |
|
33 |
-
def
|
34 |
-
|
35 |
-
|
36 |
-
else:
|
37 |
-
val = 0
|
38 |
-
return val
|
39 |
|
40 |
-
def
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
tog = gr.update(value=1)
|
46 |
-
else:
|
47 |
-
if cnt != 0:
|
48 |
-
d = gr.update(value=et)
|
49 |
-
else:
|
50 |
-
d = gr.update(value=0)
|
51 |
-
tog = gr.update(value=0)
|
52 |
-
return d, tog
|
53 |
|
54 |
-
def
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
return gr.update(value=t_stamp), gr.update(value=t_stamp), gr.update(value=0)
|
59 |
-
|
60 |
-
def clear_fn():
|
61 |
-
nn = len(models)
|
62 |
-
return tuple([None, *[None for _ in range(nn)]])
|
63 |
-
|
64 |
-
with gr.Blocks(title="SD Models") as my_interface:
|
65 |
-
with gr.Column(scale=12):
|
66 |
-
with gr.Row():
|
67 |
with gr.Row():
|
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 |
-
None,
|
104 |
-
[primary_prompt, *list(sd_outputs.values())],
|
105 |
-
cancels=[*list(runs_dict.values())]
|
106 |
-
)
|
107 |
-
tog_box.change(
|
108 |
-
clear_it,
|
109 |
-
tog_box,
|
110 |
-
tog_box,
|
111 |
-
cancels=[*list(runs_dict.values())]
|
112 |
-
)
|
113 |
|
114 |
-
|
115 |
-
|
|
|
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 |
+
body {
|
71 |
+
background-color: var(--body-background-fill) !important;
|
72 |
+
color: #2d3d4f;
|
73 |
+
margin: 0;
|
74 |
+
padding: 0;
|
75 |
+
font-family: Arial, sans-serif;
|
76 |
+
height: 100vh;
|
77 |
+
overflow-y: auto;
|
78 |
+
}
|
79 |
+
.gradio-container {
|
80 |
+
background-color: #2d3d4f;
|
81 |
+
color: #c5c6c7;
|
82 |
+
padding: 20px;
|
83 |
+
border-radius: 8px;
|
84 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
|
85 |
+
width: 100%;
|
86 |
+
max-width: 1200px;
|
87 |
+
margin: 20px auto; /* Center the container horizontally */
|
88 |
+
display: block; /* Ensure it's a block element */
|
89 |
+
min-height: 100vh; /* Adjust the height of the container */
|
90 |
+
}
|
91 |
+
.app_title {
|
92 |
+
background-color: #2d3d4f;
|
93 |
+
color: #c5c6c7;
|
94 |
+
padding: 10px 20px;
|
95 |
+
border-bottom: 1px solid #3b4252;
|
96 |
+
text-align: center;
|
97 |
+
font-size: 24px;
|
98 |
+
font-weight: bold;
|
99 |
+
width: 100%;
|
100 |
+
box-sizing: border-box;
|
101 |
+
margin-bottom: 20px; /* Add margin to separate the header from content */
|
102 |
+
}
|
103 |
+
.custom_textbox {
|
104 |
+
background-color: #2d343f;
|
105 |
+
border: 1px solid #3b4252;
|
106 |
+
color: #7f8184;
|
107 |
+
padding: 10px;
|
108 |
+
border-radius: 4px;
|
109 |
+
margin-bottom: 10px;
|
110 |
+
width: 100%;
|
111 |
+
box-sizing: border-box;
|
112 |
+
}
|
113 |
+
.custom_gen_button {
|
114 |
+
background-color: #8b38ff;
|
115 |
+
border: 1px solid #ffffff;
|
116 |
+
color: blue;
|
117 |
+
padding: 15px 32px;
|
118 |
+
text-align: center;
|
119 |
+
text-decoration: none;
|
120 |
+
display: inline-block;
|
121 |
+
font-size: 16px;
|
122 |
+
margin: 4px 2px;
|
123 |
+
cursor: pointer;
|
124 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
|
125 |
+
transition: transform 0.2s, box-shadow 0.2s;
|
126 |
+
border-radius: 4px;
|
127 |
+
}
|
128 |
+
.custom_gen_button:hover {
|
129 |
+
transform: translateY(-2px);
|
130 |
+
box-shadow: 0 6px 10px rgba(0, 0, 0, 0.3);
|
131 |
+
}
|
132 |
+
.custom_stop_button {
|
133 |
+
background-color: #6200ea;
|
134 |
+
border: 1px solid #ffffff;
|
135 |
+
color: blue;
|
136 |
+
padding: 15px 32px;
|
137 |
+
text-align: center;
|
138 |
+
text-decoration: none;
|
139 |
+
display: inline-block;
|
140 |
+
font-size: 16px;
|
141 |
+
margin: 4px 2px;
|
142 |
+
cursor: pointer;
|
143 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
|
144 |
+
transition: transform 0.2s, box-shadow 0.2s;
|
145 |
+
border-radius: 4px;
|
146 |
+
}
|
147 |
+
.custom_stop_button:hover {
|
148 |
+
transform: translateY(-2px);
|
149 |
+
box-shadow: 0 6px 10px rgba(0, 0, 0, 0.3);
|
150 |
+
}
|
151 |
+
.custom_image {
|
152 |
+
border: 1px solid #3b4252;
|
153 |
+
background-color: #2d343f;
|
154 |
+
border-radius: 4px;
|
155 |
+
margin: 10px;
|
156 |
+
max-width: 100%;
|
157 |
+
box-sizing: border-box;
|
158 |
+
}
|
159 |
+
.custom_accordion {
|
160 |
+
background-color: #2d3d4f;
|
161 |
+
color: #7f8184;
|
162 |
+
border: 1px solid #3b4252;
|
163 |
+
border-radius: 4px;
|
164 |
+
margin-top: 20px;
|
165 |
+
width: 100%;
|
166 |
+
box-sizing: border-box;
|
167 |
+
transition: margin 0.2s ease; /* Smooth transition for margin */
|
168 |
+
}
|
169 |
+
.custom_accordion .gr-accordion-header {
|
170 |
+
background-color: #2d3d4f;
|
171 |
+
color: #7f8184;
|
172 |
+
padding: 10px 20px;
|
173 |
+
border-bottom: 1px solid #5b6270;
|
174 |
+
cursor: pointer;
|
175 |
+
font-size: 18px;
|
176 |
+
font-weight: bold;
|
177 |
+
height: 40px; /* Fixed height for the header */
|
178 |
+
display: flex;
|
179 |
+
align-items: center;
|
180 |
+
}
|
181 |
+
.custom_accordion .gr-accordion-header:hover {
|
182 |
+
background-color: #2d3d4f;
|
183 |
+
}
|
184 |
+
.custom_accordion .gr-accordion-content {
|
185 |
+
padding: 10px 20px;
|
186 |
+
background-color: #2d3d4f;
|
187 |
+
border-top: 1px solid #5b6270;
|
188 |
+
max-height: 0; /* Start with no height */
|
189 |
+
overflow: hidden;
|
190 |
+
transition: max-height 0.2s ease;
|
191 |
+
}
|
192 |
+
.custom_accordion .gr-accordion-content.open {
|
193 |
+
max-height: 500px;
|
194 |
+
}
|
195 |
+
.custom_checkbox_group {
|
196 |
+
background-color: #2d343f;
|
197 |
+
border: 1px solid #3b4252;
|
198 |
+
color: #7f8184;
|
199 |
+
border-radius: 4px;
|
200 |
+
padding: 10px;
|
201 |
+
width: 100%;
|
202 |
+
box-sizing: border-box;
|
203 |
+
}
|
204 |
+
@media (max-width: 768px) {
|
205 |
+
.gradio-container {
|
206 |
+
width: 100%;
|
207 |
+
margin: 0;
|
208 |
+
padding: 10px;
|
209 |
+
}
|
210 |
+
.custom_textbox,.custom_image,.custom_checkbox_group {
|
211 |
+
width: 100%;
|
212 |
+
box-sizing: border-box;
|
213 |
+
}
|
214 |
+
}
|
215 |
+
"""
|
216 |
|
217 |
+
with gr.Blocks(css=custom_css) as demo:
|
218 |
+
make_me()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
|
220 |
+
demo.queue(concurrency_count=200)
|
221 |
+
demo.launch()
|