Delete app.py
Browse files
app.py
DELETED
|
@@ -1,151 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from random import randint
|
| 3 |
-
|
| 4 |
-
# Список моделей (пример)
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
# Загрузка моделей
|
| 8 |
-
def load_fn(models):
|
| 9 |
-
global models_load
|
| 10 |
-
models_load = {}
|
| 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:
|
| 16 |
-
m = gr.Interface(lambda txt: None, ['text'], ['image'])
|
| 17 |
-
models_load[model] = m
|
| 18 |
-
|
| 19 |
-
load_fn(models)
|
| 20 |
-
|
| 21 |
-
num_models = len(models)
|
| 22 |
-
default_models = models[:num_models]
|
| 23 |
-
|
| 24 |
-
# Расширение выбора моделей
|
| 25 |
-
def extend_choices(choices):
|
| 26 |
-
return choices + (num_models - len(choices)) * ['NA']
|
| 27 |
-
|
| 28 |
-
# Обновление изображений
|
| 29 |
-
def update_imgbox(choices):
|
| 30 |
-
choices_plus = extend_choices(choices)
|
| 31 |
-
return [gr.Image(None, label=m, visible=(m != 'NA'), elem_id="custom_image") for m in choices_plus]
|
| 32 |
-
|
| 33 |
-
# Генерация изображений
|
| 34 |
-
def gen_fn(model_str, prompt):
|
| 35 |
-
if model_str == 'NA':
|
| 36 |
-
return None
|
| 37 |
-
noise = str(randint(0, 9999999))
|
| 38 |
-
return models_load[model_str](f'{prompt} {noise}')
|
| 39 |
-
|
| 40 |
-
# Создание интерфейса
|
| 41 |
-
def make_me():
|
| 42 |
-
with gr.Row():
|
| 43 |
-
with gr.Column(scale=1):
|
| 44 |
-
txt_input = gr.Textbox(
|
| 45 |
-
label='Your prompt:',
|
| 46 |
-
lines=3,
|
| 47 |
-
elem_id="custom_textbox",
|
| 48 |
-
placeholder="Enter your prompt"
|
| 49 |
-
)
|
| 50 |
-
with gr.Row():
|
| 51 |
-
gen_button = gr.Button('Generate images', elem_id="custom_gen_button")
|
| 52 |
-
stop_button = gr.Button('Stop', variant='secondary', interactive=False, elem_id="custom_stop_button")
|
| 53 |
-
|
| 54 |
-
# Логика кнопок
|
| 55 |
-
def on_generate_click():
|
| 56 |
-
return gen_button, stop_button.interactive(True)
|
| 57 |
-
|
| 58 |
-
def on_stop_click():
|
| 59 |
-
return gen_button, stop_button.interactive(False)
|
| 60 |
-
|
| 61 |
-
gen_button.click(on_generate_click, None, [gen_button, stop_button])
|
| 62 |
-
stop_button.click(on_stop_click, None, [gen_button, stop_button])
|
| 63 |
-
|
| 64 |
-
with gr.Row():
|
| 65 |
-
output = [gr.Image(label=m, elem_id="custom_image") for m in default_models] # Убрали tool="editor"
|
| 66 |
-
current_models = [gr.Textbox(m, visible=False) for m in default_models]
|
| 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(on_stop_click, None, [gen_button, stop_button], cancels=[gen_event])
|
| 70 |
-
|
| 71 |
-
with gr.Accordion('Model selection', elem_id="custom_accordion"):
|
| 72 |
-
model_choice = gr.CheckboxGroup(
|
| 73 |
-
models,
|
| 74 |
-
label=f'{num_models} models selected',
|
| 75 |
-
value=default_models,
|
| 76 |
-
elem_id="custom_checkbox_group"
|
| 77 |
-
)
|
| 78 |
-
model_choice.change(update_imgbox, model_choice, output)
|
| 79 |
-
model_choice.change(extend_choices, model_choice, current_models)
|
| 80 |
-
|
| 81 |
-
with gr.Row():
|
| 82 |
-
gr.HTML("")
|
| 83 |
-
|
| 84 |
-
# Кастомный CSS для интерфейса
|
| 85 |
-
custom_css = """
|
| 86 |
-
body, html {
|
| 87 |
-
overflow-y: auto;
|
| 88 |
-
height: 100%;
|
| 89 |
-
margin: 0;
|
| 90 |
-
padding: 0;
|
| 91 |
-
}
|
| 92 |
-
|
| 93 |
-
.gradio-container {
|
| 94 |
-
overflow-y: auto !important;
|
| 95 |
-
height: 100vh;
|
| 96 |
-
display: flex;
|
| 97 |
-
flex-direction: column;
|
| 98 |
-
}
|
| 99 |
-
|
| 100 |
-
/* Стили для текста */
|
| 101 |
-
.custom_textbox {
|
| 102 |
-
background-color: #2d343f;
|
| 103 |
-
border: 1px solid #3b4252;
|
| 104 |
-
color: #7f8184;
|
| 105 |
-
padding: 10px;
|
| 106 |
-
border-radius: 4px;
|
| 107 |
-
width: 100%;
|
| 108 |
-
box-sizing: border-box;
|
| 109 |
-
}
|
| 110 |
-
|
| 111 |
-
/* Стили для кнопок */
|
| 112 |
-
.custom_gen_button, .custom_stop_button {
|
| 113 |
-
padding: 15px;
|
| 114 |
-
border-radius: 4px;
|
| 115 |
-
transition: all 0.2s;
|
| 116 |
-
cursor: pointer;
|
| 117 |
-
}
|
| 118 |
-
|
| 119 |
-
.custom_gen_button:hover, .custom_stop_button:hover {
|
| 120 |
-
transform: scale(1.05);
|
| 121 |
-
}
|
| 122 |
-
|
| 123 |
-
/* Стили для изображений */
|
| 124 |
-
.custom_image {
|
| 125 |
-
max-width: 100%;
|
| 126 |
-
height: auto;
|
| 127 |
-
object-fit: contain;
|
| 128 |
-
overflow: auto;
|
| 129 |
-
border: 1px solid #3b4252;
|
| 130 |
-
background-color: #2d343f;
|
| 131 |
-
border-radius: 4px;
|
| 132 |
-
cursor: grab;
|
| 133 |
-
margin: 10px;
|
| 134 |
-
}
|
| 135 |
-
|
| 136 |
-
/* Стили для выбора моделей */
|
| 137 |
-
.custom_accordion {
|
| 138 |
-
margin-top: 20px;
|
| 139 |
-
border: 1px solid #3b4252;
|
| 140 |
-
border-radius: 4px;
|
| 141 |
-
}
|
| 142 |
-
"""
|
| 143 |
-
|
| 144 |
-
# Создание интерфейса
|
| 145 |
-
with gr.Blocks(css=custom_css) as demo:
|
| 146 |
-
make_me()
|
| 147 |
-
|
| 148 |
-
# Очередь и запуск интерфейса с параметрами
|
| 149 |
-
demo.queue(default_concurrency_limit=240, max_size=240)
|
| 150 |
-
demo.launch(max_threads=400, ssr_mode=True)
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|