Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
from random import randint
|
3 |
|
4 |
-
#
|
5 |
models = ["Model A", "Model B", "Model C"]
|
6 |
|
7 |
# Загрузка моделей
|
@@ -21,11 +21,11 @@ load_fn(models)
|
|
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]
|
@@ -41,32 +41,48 @@ def gen_fn(model_str, prompt):
|
|
41 |
def make_me():
|
42 |
with gr.Row():
|
43 |
with gr.Column(scale=1):
|
44 |
-
txt_input = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
45 |
with gr.Row():
|
46 |
gen_button = gr.Button('Generate images', elem_id="custom_gen_button")
|
47 |
stop_button = gr.Button('Stop', variant='secondary', interactive=False, elem_id="custom_stop_button")
|
48 |
|
49 |
# Логика кнопок
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
# Вывод изображений
|
54 |
with gr.Row():
|
55 |
output = [gr.Image(label=m, elem_id="custom_image", tool="editor") for m in default_models]
|
56 |
current_models = [gr.Textbox(m, visible=False) for m in default_models]
|
57 |
for m, o in zip(current_models, output):
|
58 |
gen_event = gen_button.click(gen_fn, [m, txt_input], o)
|
59 |
-
stop_button.click(
|
60 |
|
61 |
-
# Выбор моделей
|
62 |
with gr.Accordion('Model selection', elem_id="custom_accordion"):
|
63 |
-
model_choice = gr.CheckboxGroup(
|
64 |
-
|
|
|
|
|
|
|
|
|
65 |
model_choice.change(update_imgbox, model_choice, output)
|
66 |
model_choice.change(extend_choices, model_choice, current_models)
|
67 |
|
|
|
|
|
|
|
|
|
68 |
custom_css = """
|
69 |
-
/* Основные стили */
|
70 |
body, html {
|
71 |
overflow-y: auto;
|
72 |
height: 100%;
|
@@ -81,20 +97,7 @@ body, html {
|
|
81 |
flex-direction: column;
|
82 |
}
|
83 |
|
84 |
-
/*
|
85 |
-
.custom_image {
|
86 |
-
max-width: 100%;
|
87 |
-
height: auto;
|
88 |
-
object-fit: contain;
|
89 |
-
overflow: auto;
|
90 |
-
border: 1px solid #3b4252;
|
91 |
-
background-color: #2d343f;
|
92 |
-
border-radius: 4px;
|
93 |
-
cursor: grab;
|
94 |
-
margin: 10px;
|
95 |
-
}
|
96 |
-
|
97 |
-
/* Стили текста и кнопок */
|
98 |
.custom_textbox {
|
99 |
background-color: #2d343f;
|
100 |
border: 1px solid #3b4252;
|
@@ -105,6 +108,7 @@ body, html {
|
|
105 |
box-sizing: border-box;
|
106 |
}
|
107 |
|
|
|
108 |
.custom_gen_button, .custom_stop_button {
|
109 |
padding: 15px;
|
110 |
border-radius: 4px;
|
@@ -116,6 +120,20 @@ body, html {
|
|
116 |
transform: scale(1.05);
|
117 |
}
|
118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
.custom_accordion {
|
120 |
margin-top: 20px;
|
121 |
border: 1px solid #3b4252;
|
@@ -124,8 +142,14 @@ body, html {
|
|
124 |
"""
|
125 |
|
126 |
# Создание интерфейса
|
127 |
-
with gr.Blocks(css=custom_css) as demo:
|
128 |
make_me()
|
|
|
|
|
|
|
|
|
|
|
129 |
|
|
|
130 |
demo.queue()
|
131 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from random import randint
|
3 |
|
4 |
+
# Список моделей (пример)
|
5 |
models = ["Model A", "Model B", "Model C"]
|
6 |
|
7 |
# Загрузка моделей
|
|
|
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]
|
|
|
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", tool="editor") for m in default_models]
|
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%;
|
|
|
97 |
flex-direction: column;
|
98 |
}
|
99 |
|
100 |
+
/* Стили для текста */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
.custom_textbox {
|
102 |
background-color: #2d343f;
|
103 |
border: 1px solid #3b4252;
|
|
|
108 |
box-sizing: border-box;
|
109 |
}
|
110 |
|
111 |
+
/* Стили для кнопок */
|
112 |
.custom_gen_button, .custom_stop_button {
|
113 |
padding: 15px;
|
114 |
border-radius: 4px;
|
|
|
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;
|
|
|
142 |
"""
|
143 |
|
144 |
# Создание интерфейса
|
145 |
+
with gr.Blocks(css=custom_css) as demo:
|
146 |
make_me()
|
147 |
+
demo.queue(default_concurrency_limit=240, max_size=240)
|
148 |
+
demo.launch(max_threads=400, ssr_mode=True)
|
149 |
+
# https://github.com/gradio-app/gradio/issues/6339
|
150 |
+
|
151 |
+
|
152 |
|
153 |
+
# Очередь и запуск интерфейса
|
154 |
demo.queue()
|
155 |
demo.launch()
|