MrDrmm commited on
Commit
d77c33a
·
verified ·
1 Parent(s): ca877df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -148
app.py CHANGED
@@ -1,7 +1,10 @@
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 = {}
@@ -9,224 +12,120 @@ def load_fn(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, html {
72
- overflow-y: auto; /* Включаем прокрутку для всей страницы */
73
  height: 100%;
74
  margin: 0;
75
  padding: 0;
76
  }
77
 
 
 
 
 
 
 
 
 
78
  .custom_image {
 
 
 
 
79
  border: 1px solid #3b4252;
80
  background-color: #2d343f;
81
  border-radius: 4px;
 
82
  margin: 10px;
83
- max-width: 100%;
84
- max-height: 400px; /* Ограничиваем высоту */
85
- object-fit: contain;
86
- overflow: auto;
87
- box-sizing: border-box;
88
- cursor: grab; /* Меняем курсор для взаимодействия */
89
- }
90
-
91
-
92
- .app_title {
93
- background-color: #2d3d4f;
94
- color: #c5c6c7;
95
- padding: 10px 20px;
96
- border-bottom: 1px solid #3b4252;
97
- text-align: center;
98
- font-size: 24px;
99
- font-weight: bold;
100
- width: 100%;
101
- box-sizing: border-box;
102
- margin-bottom: 20px;
103
  }
104
 
 
105
  .custom_textbox {
106
  background-color: #2d343f;
107
  border: 1px solid #3b4252;
108
  color: #7f8184;
109
  padding: 10px;
110
  border-radius: 4px;
111
- margin-bottom: 10px;
112
  width: 100%;
113
  box-sizing: border-box;
114
  }
115
 
116
- .custom_gen_button {
117
- background-color: #8b38ff;
118
- border: 1px solid #ffffff;
119
- color: blue;
120
- padding: 15px 32px;
121
- text-align: center;
122
- text-decoration: none;
123
- display: inline-block;
124
- font-size: 16px;
125
- margin: 4px 2px;
126
- cursor: pointer;
127
- box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
128
- transition: transform 0.2s, box-shadow 0.2s;
129
  border-radius: 4px;
130
- }
131
- .custom_gen_button:hover {
132
- transform: translateY(-2px);
133
- box-shadow: 0 6px 10px rgba(0, 0, 0, 0.3);
134
- }
135
- .custom_stop_button {
136
- background-color: #6200ea;
137
- border: 1px solid #ffffff;
138
- color: blue;
139
- padding: 15px 32px;
140
- text-align: center;
141
- text-decoration: none;
142
- display: inline-block;
143
- font-size: 16px;
144
- margin: 4px 2px;
145
  cursor: pointer;
146
- box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
147
- transition: transform 0.2s, box-shadow 0.2s;
148
- border-radius: 4px;
149
- }
150
- .custom_stop_button:hover {
151
- transform: translateY(-2px);
152
- box-shadow: 0 6px 10px rgba(0, 0, 0, 0.3);
153
  }
154
 
155
- .custom_image {
156
- border: 1px solid #3b4252;
157
- background-color: #2d343f;
158
- border-radius: 4px;
159
- margin: 10px;
160
- max-width: 50%;
161
- box-sizing: border-box;
162
  }
163
 
164
  .custom_accordion {
165
- background-color: #2d3d4f;
166
- color: #7f8184;
167
- border: 1px solid #3b4252;
168
- border-radius: 4px;
169
  margin-top: 20px;
170
- width: 100%;
171
- box-sizing: border-box;
172
- transition: margin 0.2s ease;
173
- }
174
-
175
- .custom_accordion .gr-accordion-header {
176
- background-color: #2d3d4f;
177
- color: #7f8184;
178
- padding: 10px 20px;
179
- border-bottom: 1px solid #5b6270;
180
- cursor: pointer;
181
- font-size: 18px;
182
- font-weight: bold;
183
- height: 40px;
184
- display: flex;
185
- align-items: center;
186
- }
187
-
188
- .custom_accordion .gr-accordion-header:hover {
189
- background-color: #2d3d4f;
190
- }
191
-
192
- .custom_accordion .gr-accordion-content {
193
- padding: 10px 20px;
194
- background-color: #2d3d4f;
195
- border-top: 1px solid #5b6270;
196
- max-height: 0;
197
- overflow: hidden;
198
- transition: max-height 0.2s ease;
199
- }
200
-
201
- .custom_accordion .gr-accordion-content.open {
202
- max-height: 500px;
203
- }
204
-
205
- .custom_checkbox_group {
206
- background-color: #2d343f;
207
  border: 1px solid #3b4252;
208
- color: #7f8184;
209
  border-radius: 4px;
210
- padding: 10px;
211
- width: 100%;
212
- box-sizing: border-box;
213
- }
214
-
215
- @media (max-width: 320px) {
216
- .gradio-container {
217
- width: 90%; /* Исправлено */
218
- margin: 0 auto;
219
- padding: 10px;
220
- }
221
- .custom_textbox, .custom_image, .custom_checkbox_group {
222
- width: 100%;
223
- box-sizing: border-box;
224
- }
225
  }
226
  """
227
 
 
228
  with gr.Blocks(css=custom_css) as demo:
229
  make_me()
230
 
231
- demo.queue(default_concurrency_limit=240, max_size=240)
232
- demo.launch(max_threads=400, ssr_mode=True)
 
1
  import gradio as gr
2
  from random import randint
 
3
 
4
+ # Пример списка моделей
5
+ models = ["Model A", "Model B", "Model C"]
6
+
7
+ # Загрузка моделей
8
  def load_fn(models):
9
  global models_load
10
  models_load = {}
 
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(label='Your prompt:', lines=3, elem_id="custom_textbox", placeholder="Prompt")
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
+ gen_button.click(lambda: [gen_button, stop_button], None, [gen_button, stop_button])
51
+ stop_button.click(lambda: [gen_button, stop_button], None, [gen_button, stop_button])
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(lambda: [gen_button, stop_button], None, [gen_button, stop_button], cancels=[gen_event])
60
+
61
+ # Выбор моделей
62
  with gr.Accordion('Model selection', elem_id="custom_accordion"):
63
+ model_choice = gr.CheckboxGroup(models, label=f'{num_models} different models selected',
64
+ value=default_models, elem_id="custom_checkbox_group")
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%;
73
  margin: 0;
74
  padding: 0;
75
  }
76
 
77
+ .gradio-container {
78
+ overflow-y: auto !important;
79
+ height: 100vh;
80
+ display: flex;
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;
101
  color: #7f8184;
102
  padding: 10px;
103
  border-radius: 4px;
 
104
  width: 100%;
105
  box-sizing: border-box;
106
  }
107
 
108
+ .custom_gen_button, .custom_stop_button {
109
+ padding: 15px;
 
 
 
 
 
 
 
 
 
 
 
110
  border-radius: 4px;
111
+ transition: all 0.2s;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  cursor: pointer;
 
 
 
 
 
 
 
113
  }
114
 
115
+ .custom_gen_button:hover, .custom_stop_button:hover {
116
+ transform: scale(1.05);
 
 
 
 
 
117
  }
118
 
119
  .custom_accordion {
 
 
 
 
120
  margin-top: 20px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  border: 1px solid #3b4252;
 
122
  border-radius: 4px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  }
124
  """
125
 
126
+ # Создание интерфейса
127
  with gr.Blocks(css=custom_css) as demo:
128
  make_me()
129
 
130
+ demo.queue()
131
+ demo.launch()