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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from random import randint
|
| 3 |
+
from all_models import models
|
| 4 |
+
import torch
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = "" # Отключаем CUDA
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
device = "cpu" # Используем CPU
|
| 11 |
+
model_repo_id = "stabilityai/sdxl-turbo"
|
| 12 |
+
|
| 13 |
+
torch_dtype = torch.float32 # Тип данных для CPU
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def load_fn(models):
|
| 18 |
+
global models_load
|
| 19 |
+
models_load = {}
|
| 20 |
+
for model in models:
|
| 21 |
+
if model not in models_load.keys():
|
| 22 |
+
try:
|
| 23 |
+
m = gr.load(f'models/{model}')
|
| 24 |
+
except Exception as error:
|
| 25 |
+
m = gr.Interface(lambda txt: None, ['text'], ['image'])
|
| 26 |
+
models_load.update({model: m})
|
| 27 |
+
|
| 28 |
+
load_fn(models)
|
| 29 |
+
|
| 30 |
+
num_models = len(models)
|
| 31 |
+
default_models = models[:num_models]
|
| 32 |
+
|
| 33 |
+
def extend_choices(choices):
|
| 34 |
+
return choices + (num_models - len(choices)) * ['NA']
|
| 35 |
+
|
| 36 |
+
def update_imgbox(choices):
|
| 37 |
+
choices_plus = extend_choices(choices)
|
| 38 |
+
return [gr.Image(None, label=m, visible=(m != 'NA'), elem_id="custom_image") for m in choices_plus]
|
| 39 |
+
|
| 40 |
+
def gen_fn(model_str, prompt):
|
| 41 |
+
if model_str == 'NA':
|
| 42 |
+
return None
|
| 43 |
+
noise = str(randint(0, 9999999))
|
| 44 |
+
return models_load[model_str](f'{prompt} {noise}')
|
| 45 |
+
|
| 46 |
+
def make_me():
|
| 47 |
+
with gr.Row():
|
| 48 |
+
with gr.Column(scale=1):
|
| 49 |
+
txt_input = gr.Textbox(
|
| 50 |
+
label='Your prompt:',
|
| 51 |
+
lines=3,
|
| 52 |
+
container=False,
|
| 53 |
+
elem_id="custom_textbox",
|
| 54 |
+
placeholder="Prompt"
|
| 55 |
+
)
|
| 56 |
+
with gr.Row():
|
| 57 |
+
gen_button = gr.Button('Generate images', elem_id="custom_gen_button")
|
| 58 |
+
stop_button = gr.Button('Stop', variant='secondary', interactive=False, elem_id="custom_stop_button")
|
| 59 |
+
|
| 60 |
+
def on_generate_click():
|
| 61 |
+
return gr.Button('Generate images', elem_id="custom_gen_button"), gr.Button('Stop', variant='secondary', interactive=True, elem_id="custom_stop_button")
|
| 62 |
+
|
| 63 |
+
def on_stop_click():
|
| 64 |
+
return gr.Button('Generate images', elem_id="custom_gen_button"), gr.Button('Stop', variant='secondary', interactive=False, elem_id="custom_stop_button")
|
| 65 |
+
|
| 66 |
+
gen_button.click(on_generate_click, inputs=None, outputs=[gen_button, stop_button])
|
| 67 |
+
stop_button.click(on_stop_click, inputs=None, outputs=[gen_button, stop_button])
|
| 68 |
+
|
| 69 |
+
# Делим вывод изображений на два ряда
|
| 70 |
+
with gr.Row():
|
| 71 |
+
half = len(default_models) // 2
|
| 72 |
+
output_row1 = [gr.Image(label=m, min_width=250, height=250, elem_id="custom_image") for m in default_models[:half]]
|
| 73 |
+
output_row2 = [gr.Image(label=m, min_width=250, height=250, elem_id="custom_image") for m in default_models[half:]]
|
| 74 |
+
current_models_row1 = [gr.Textbox(m, visible=False) for m in default_models[:half]]
|
| 75 |
+
current_models_row2 = [gr.Textbox(m, visible=False) for m in default_models[half:]]
|
| 76 |
+
|
| 77 |
+
# Первый ряд изображений
|
| 78 |
+
for m, o in zip(current_models_row1, output_row1):
|
| 79 |
+
gen_event = gen_button.click(gen_fn, [m, txt_input], o)
|
| 80 |
+
stop_button.click(on_stop_click, inputs=None, outputs=[gen_button, stop_button], cancels=[gen_event])
|
| 81 |
+
|
| 82 |
+
# Второй ряд изображений
|
| 83 |
+
for m, o in zip(current_models_row2, output_row2):
|
| 84 |
+
gen_event = gen_button.click(gen_fn, [m, txt_input], o)
|
| 85 |
+
stop_button.click(on_stop_click, inputs=None, outputs=[gen_button, stop_button], cancels=[gen_event])
|
| 86 |
+
|
| 87 |
+
# Разделяем чекбоксы на два ряда
|
| 88 |
+
with gr.Accordion('Model selection', elem_id="custom_accordion"):
|
| 89 |
+
half = len(models) // 2
|
| 90 |
+
model_choice_row1 = models[:half]
|
| 91 |
+
model_choice_row2 = models[half:]
|
| 92 |
+
|
| 93 |
+
model_choice1 = gr.CheckboxGroup(
|
| 94 |
+
model_choice_row1,
|
| 95 |
+
label=f'{len(model_choice_row1)} models selected',
|
| 96 |
+
value=default_models[:half],
|
| 97 |
+
interactive=True,
|
| 98 |
+
elem_id="custom_checkbox_group"
|
| 99 |
+
)
|
| 100 |
+
model_choice2 = gr.CheckboxGroup(
|
| 101 |
+
model_choice_row2,
|
| 102 |
+
label=f'{len(model_choice_row2)} models selected',
|
| 103 |
+
value=default_models[half:],
|
| 104 |
+
interactive=True,
|
| 105 |
+
elem_id="custom_checkbox_group"
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
# Обновляем первый ряд
|
| 109 |
+
model_choice1.change(update_imgbox, model_choice1, output_row1)
|
| 110 |
+
model_choice1.change(extend_choices, model_choice1, current_models_row1)
|
| 111 |
+
|
| 112 |
+
# Обновляем второй ряд
|
| 113 |
+
model_choice2.change(update_imgbox, model_choice2, output_row2)
|
| 114 |
+
model_choice2.change(extend_choices, model_choice2, current_models_row2)
|
| 115 |
+
|
| 116 |
+
with gr.Row():
|
| 117 |
+
gr.HTML("")
|
| 118 |
+
|
| 119 |
+
custom_css = """
|
| 120 |
+
:root {
|
| 121 |
+
--body-background-fill: #2d3d4f;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
body {
|
| 125 |
+
background-color: var(--body-background-fill) !important;
|
| 126 |
+
color: #2d3d4f;
|
| 127 |
+
margin: 0;
|
| 128 |
+
padding: 0;
|
| 129 |
+
font-family: Arial, sans-serif;
|
| 130 |
+
height: 100vh;
|
| 131 |
+
overflow-y: auto;
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
.gradio-container {
|
| 135 |
+
background-color: #2d3d4f;
|
| 136 |
+
color: #c5c6c7;
|
| 137 |
+
padding: 20px;
|
| 138 |
+
border-radius: 8px;
|
| 139 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
|
| 140 |
+
width: 100%;
|
| 141 |
+
max-width: 1200px;
|
| 142 |
+
margin: 20px auto;
|
| 143 |
+
display: block;
|
| 144 |
+
min-height: 100vh;
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
.app_title {
|
| 148 |
+
background-color: #2d3d4f;
|
| 149 |
+
color: #c5c6c7;
|
| 150 |
+
padding: 10px 20px;
|
| 151 |
+
border-bottom: 1px solid #3b4252;
|
| 152 |
+
text-align: center;
|
| 153 |
+
font-size: 24px;
|
| 154 |
+
font-weight: bold;
|
| 155 |
+
width: 100%;
|
| 156 |
+
box-sizing: border-box;
|
| 157 |
+
margin-bottom: 20px;
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
.custom_textbox {
|
| 161 |
+
background-color: #2d343f;
|
| 162 |
+
border: 1px solid #3b4252;
|
| 163 |
+
color: #7f8184;
|
| 164 |
+
padding: 10px;
|
| 165 |
+
border-radius: 4px;
|
| 166 |
+
margin-bottom: 10px;
|
| 167 |
+
width: 100%;
|
| 168 |
+
box-sizing: border-box;
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
.custom_gen_button {
|
| 172 |
+
background-color: #8b38ff;
|
| 173 |
+
border: 1px solid #ffffff;
|
| 174 |
+
color: blue;
|
| 175 |
+
padding: 15px 32px;
|
| 176 |
+
text-align: center;
|
| 177 |
+
text-decoration: none;
|
| 178 |
+
display: inline-block;
|
| 179 |
+
font-size: 16px;
|
| 180 |
+
margin: 4px 2px;
|
| 181 |
+
cursor: pointer;
|
| 182 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
|
| 183 |
+
transition: transform 0.2s, box-shadow 0.2s;
|
| 184 |
+
border-radius: 4px;
|
| 185 |
+
}
|
| 186 |
+
.custom_gen_button:hover {
|
| 187 |
+
transform: translateY(-2px);
|
| 188 |
+
box-shadow: 0 6px 10px rgba(0, 0, 0, 0.3);
|
| 189 |
+
}
|
| 190 |
+
.custom_stop_button {
|
| 191 |
+
background-color: #6200ea;
|
| 192 |
+
border: 1px solid #ffffff;
|
| 193 |
+
color: blue;
|
| 194 |
+
padding: 15px 32px;
|
| 195 |
+
text-align: center;
|
| 196 |
+
text-decoration: none;
|
| 197 |
+
display: inline-block;
|
| 198 |
+
font-size: 16px;
|
| 199 |
+
margin: 4px 2px;
|
| 200 |
+
cursor: pointer;
|
| 201 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
|
| 202 |
+
transition: transform 0.2s, box-shadow 0.2s;
|
| 203 |
+
border-radius: 4px;
|
| 204 |
+
}
|
| 205 |
+
.custom_stop_button:hover {
|
| 206 |
+
transform: translateY(-2px);
|
| 207 |
+
box-shadow: 0 6px 10px rgba(0, 0, 0, 0.3);
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
.custom_image {
|
| 211 |
+
border: 1px solid #3b4252;
|
| 212 |
+
background-color: #2d343f;
|
| 213 |
+
border-radius: 4px;
|
| 214 |
+
margin: 10px;
|
| 215 |
+
max-width: 100%;
|
| 216 |
+
box-sizing: border-box;
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
.custom_accordion {
|
| 220 |
+
background-color: #2d3d4f;
|
| 221 |
+
color: #7f8184;
|
| 222 |
+
border: 1px solid #3b4252;
|
| 223 |
+
border-radius: 4px;
|
| 224 |
+
margin-top: 20px;
|
| 225 |
+
width: 100%;
|
| 226 |
+
box-sizing: border-box;
|
| 227 |
+
transition: margin 0.2s ease;
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
.custom_accordion .gr-accordion-header {
|
| 231 |
+
background-color: #2d3d4f;
|
| 232 |
+
color: #7f8184;
|
| 233 |
+
padding: 10px 20px;
|
| 234 |
+
border-bottom: 1px solid #5b6270;
|
| 235 |
+
cursor: pointer;
|
| 236 |
+
font-size: 18px;
|
| 237 |
+
font-weight: bold;
|
| 238 |
+
height: 40px;
|
| 239 |
+
display: flex;
|
| 240 |
+
align-items: center;
|
| 241 |
+
}
|
| 242 |
+
|
| 243 |
+
.custom_accordion .gr-accordion-header:hover {
|
| 244 |
+
background-color: #2d3d4f;
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
.custom_accordion .gr-accordion-content {
|
| 248 |
+
padding: 10px 20px;
|
| 249 |
+
background-color: #2d3d4f;
|
| 250 |
+
border-top: 1px solid #5b6270;
|
| 251 |
+
max-height: 0;
|
| 252 |
+
overflow: hidden;
|
| 253 |
+
transition: max-height 0.2s ease;
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
.custom_accordion .gr-accordion-content.open {
|
| 257 |
+
max-height: 500px;
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
.custom_checkbox_group {
|
| 261 |
+
background-color: #2d343f;
|
| 262 |
+
border: 1px solid #3b4252;
|
| 263 |
+
color: #7f8184;
|
| 264 |
+
border-radius: 4px;
|
| 265 |
+
padding: 10px;
|
| 266 |
+
width: 100%;
|
| 267 |
+
box-sizing: border-box;
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
@media (max-width: 768px) {
|
| 271 |
+
.gradio-container {
|
| 272 |
+
width: 100%;
|
| 273 |
+
margin: 0;
|
| 274 |
+
padding: 10px;
|
| 275 |
+
}
|
| 276 |
+
.custom_textbox,.custom_image,.custom_checkbox_group {
|
| 277 |
+
width: 100%;
|
| 278 |
+
box-sizing: border-box;
|
| 279 |
+
}
|
| 280 |
+
}
|
| 281 |
+
"""
|
| 282 |
+
|
| 283 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 284 |
+
make_me()
|
| 285 |
+
# Очередь и запуск интерфейса с параметрами
|
| 286 |
+
demo.queue(default_concurrency_limit=340, max_size=400)
|
| 287 |
+
demo.launch(max_threads=400, ssr_mode=False)
|
| 288 |
+
|
| 289 |
+
|
| 290 |
+
demo.queue(concurrency_count=50)
|
| 291 |
+
demo.launch()
|