Zaiiida commited on
Commit
69983e5
·
verified ·
1 Parent(s): 4817a50

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -49
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import gradio as gr
2
  import time # Для эмуляции времени загрузки
3
  import tempfile
@@ -57,13 +59,19 @@ def generate(image):
57
  return mesh_path2.name
58
 
59
 
60
- # Функция для отображения загрузки
61
- def start_loading():
62
- return "<div id='loading-bar' style='display: block;'></div>"
63
-
 
 
 
64
 
65
- def stop_loading():
66
- return "<div id='loading-bar' style='display: none;'></div>"
 
 
 
67
 
68
 
69
  # Настройка темы и CSS
@@ -82,8 +90,7 @@ class CustomTheme(gr.themes.Base):
82
 
83
 
84
  css = """
85
- /* Скрываем нижний колонтитул */
86
- footer {
87
  visibility: hidden;
88
  height: 0;
89
  margin: 0;
@@ -115,23 +122,23 @@ input[type="text"], textarea {
115
 
116
  /* Слайдер */
117
  .gr-slider {
118
- height: 40px !important; /* Увеличение высоты слайдера */
119
- transition: all 0.3s ease !important; /* Плавные переходы */
120
  }
121
 
122
  .gr-slider .slider-value {
123
- display: none !important; /* Прячем текст значения */
124
  }
125
 
126
  .gr-slider .slider::-webkit-slider-thumb {
127
  height: 15px !important;
128
  width: 15px !important;
129
- background-color: #5271FF !important; /* Цвет совпадает с кнопкой */
130
  border-radius: 50%;
131
  }
132
 
133
  .gr-slider .slider::-webkit-slider-runnable-track {
134
- background: #5271FF !important; /* Цвет заполнения слайдера */
135
  }
136
 
137
  /* Кнопка Generate */
@@ -143,7 +150,7 @@ input[type="text"], textarea {
143
  }
144
 
145
  .generate-button:hover {
146
- background-color: #405BBF !important; /* Цвет при наведении */
147
  }
148
 
149
  /* Выделяем текст для Prompt */
@@ -152,9 +159,16 @@ input[type="text"], textarea {
152
  color: #FFFFFF;
153
  }
154
 
155
- /* Лоадер (анимация) */
 
 
 
 
 
 
 
156
  #loading-bar {
157
- display: none; /* Скрыт по умолчанию */
158
  position: absolute;
159
  top: 50%;
160
  left: 50%;
@@ -162,9 +176,10 @@ input[type="text"], textarea {
162
  width: 60px;
163
  height: 60px;
164
  border: 8px solid #f3f3f3;
165
- border-top: 8px solid #5271FF; /* Цвет лоадера */
166
  border-radius: 50%;
167
  animation: spin 1s linear infinite;
 
168
  }
169
 
170
  /* Анимация вращения */
@@ -177,6 +192,19 @@ input[type="text"], textarea {
177
  .gr-checkbox label {
178
  color: #FFFFFF !important;
179
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
180
  """
181
 
182
  # Интерфейс
@@ -184,20 +212,26 @@ with gr.Blocks(theme=CustomTheme(), css=css) as demo:
184
  with gr.Column():
185
  gr.Markdown("<span style='color: white; font-weight: bold;'>Upload and Process Your Image</span>")
186
  with gr.Row():
187
- input_image = gr.Image(
188
- label="Upload Image",
189
- image_mode="RGBA",
190
- sources="upload",
191
- type="pil",
192
- width=400,
193
- height=300,
194
- )
195
- processed_image = gr.Image(
196
- label="Processed Image",
197
- interactive=False,
198
- width=400,
199
- height=300,
200
- )
 
 
 
 
 
 
201
  foreground_ratio = gr.Slider(
202
  label="Foreground Ratio",
203
  minimum=0.5,
@@ -206,42 +240,59 @@ with gr.Blocks(theme=CustomTheme(), css=css) as demo:
206
  step=0.05,
207
  )
208
  do_remove_background = gr.Checkbox(
209
- label="Remove Background", # Текст перекрашен в белый через CSS
210
  value=True,
211
  )
212
  submit = gr.Button("Generate", elem_classes="generate-button")
213
- loading_bar = gr.HTML("<div id='loading-bar'></div>")
214
- output_model = gr.Model3D(
215
- label="Generated GLB Model",
216
- interactive=False,
217
- elem_classes="gr-model3d-container",
218
- )
 
219
 
 
220
  submit.click(
221
- fn=start_loading, # Включить прогресс-бар
222
- inputs=[],
223
- outputs=[loading_bar],
224
  ).then(
225
  fn=check_input_image,
226
  inputs=[input_image],
227
- outputs=[]
 
 
 
 
228
  ).then(
229
  fn=preprocess,
230
  inputs=[input_image, do_remove_background, foreground_ratio],
231
- outputs=[processed_image]
 
 
 
 
 
 
 
 
232
  ).then(
233
  fn=generate,
234
  inputs=[processed_image],
235
- outputs=[output_model]
 
 
 
 
236
  ).then(
237
- fn=stop_loading, # Отключаем прогресс-бар после завершения генерации
238
- inputs=[],
239
- outputs=[loading_bar],
240
  )
241
-
242
  # Запуск приложения
243
  demo.launch(
244
  server_name="0.0.0.0",
245
  server_port=7860,
246
  share=True,
247
- )
 
1
+
2
+
3
  import gradio as gr
4
  import time # Для эмуляции времени загрузки
5
  import tempfile
 
59
  return mesh_path2.name
60
 
61
 
62
+ def start_loading(container_id):
63
+ return f"""
64
+ <div class="loader" id="loader-{container_id}"></div>
65
+ <script>
66
+ document.getElementById('loader-{container_id}').style.display = 'block';
67
+ </script>
68
+ """
69
 
70
+ def stop_loading(container_id):
71
+ return f"""
72
+ <script>
73
+ document.getElementById('loader-{container_id}').style.display = 'none';
74
+ </script>
75
 
76
 
77
  # Настройка темы и CSS
 
90
 
91
 
92
  css = """
93
+ ooter {
 
94
  visibility: hidden;
95
  height: 0;
96
  margin: 0;
 
122
 
123
  /* Слайдер */
124
  .gr-slider {
125
+ height: 40px !important;
126
+ transition: all 0.3s ease !important;
127
  }
128
 
129
  .gr-slider .slider-value {
130
+ display: none !important;
131
  }
132
 
133
  .gr-slider .slider::-webkit-slider-thumb {
134
  height: 15px !important;
135
  width: 15px !important;
136
+ background-color: #5271FF !important;
137
  border-radius: 50%;
138
  }
139
 
140
  .gr-slider .slider::-webkit-slider-runnable-track {
141
+ background: #5271FF !important;
142
  }
143
 
144
  /* Кнопка Generate */
 
150
  }
151
 
152
  .generate-button:hover {
153
+ background-color: #405BBF !important;
154
  }
155
 
156
  /* Выделяем текст для Prompt */
 
159
  color: #FFFFFF;
160
  }
161
 
162
+ /* Контейнеры для правильного позиционирования лоадера */
163
+ .upload-container,
164
+ .processed-container,
165
+ .gr-model3d-container {
166
+ position: relative !important;
167
+ }
168
+
169
+ /* Обновленный стиль лоадера */
170
  #loading-bar {
171
+ display: none;
172
  position: absolute;
173
  top: 50%;
174
  left: 50%;
 
176
  width: 60px;
177
  height: 60px;
178
  border: 8px solid #f3f3f3;
179
+ border-top: 8px solid #5271FF;
180
  border-radius: 50%;
181
  animation: spin 1s linear infinite;
182
+ z-index: 1000;
183
  }
184
 
185
  /* Анимация вращения */
 
192
  .gr-checkbox label {
193
  color: #FFFFFF !important;
194
  }
195
+
196
+ /* Убедимся, что контейнеры изображений правильно позиционированы */
197
+ .image-container {
198
+ position: relative !important;
199
+ width: 100%;
200
+ height: 100%;
201
+ }
202
+
203
+ /* Стили для контейнера с 3D моделью */
204
+ .gr-model3d-container {
205
+ position: relative !important;
206
+ min-height: 400px;
207
+ }
208
  """
209
 
210
  # Интерфейс
 
212
  with gr.Column():
213
  gr.Markdown("<span style='color: white; font-weight: bold;'>Upload and Process Your Image</span>")
214
  with gr.Row():
215
+ with gr.Column(elem_classes="upload-container"):
216
+ input_image = gr.Image(
217
+ label="Upload Image",
218
+ image_mode="RGBA",
219
+ sources="upload",
220
+ type="pil",
221
+ width=400,
222
+ height=300,
223
+ )
224
+ upload_loader = gr.HTML(visible=False)
225
+
226
+ with gr.Column(elem_classes="processed-container"):
227
+ processed_image = gr.Image(
228
+ label="Processed Image",
229
+ interactive=False,
230
+ width=400,
231
+ height=300,
232
+ )
233
+ process_loader = gr.HTML(visible=False)
234
+
235
  foreground_ratio = gr.Slider(
236
  label="Foreground Ratio",
237
  minimum=0.5,
 
240
  step=0.05,
241
  )
242
  do_remove_background = gr.Checkbox(
243
+ label="Remove Background",
244
  value=True,
245
  )
246
  submit = gr.Button("Generate", elem_classes="generate-button")
247
+
248
+ with gr.Column(elem_classes="model3d-container"):
249
+ output_model = gr.Model3D(
250
+ label="Generated GLB Model",
251
+ interactive=False,
252
+ )
253
+ model_loader = gr.HTML(visible=False)
254
 
255
+ # Обновляем цепочку событий
256
  submit.click(
257
+ fn=lambda: """<div id='loading-bar' style='display: block;'></div>""",
258
+ outputs=[upload_loader],
259
+ show_progress=False,
260
  ).then(
261
  fn=check_input_image,
262
  inputs=[input_image],
263
+ outputs=[],
264
+ ).then(
265
+ fn=lambda: """<div id='loading-bar' style='display: block;'></div>""",
266
+ outputs=[process_loader],
267
+ show_progress=False,
268
  ).then(
269
  fn=preprocess,
270
  inputs=[input_image, do_remove_background, foreground_ratio],
271
+ outputs=[processed_image],
272
+ ).then(
273
+ fn=lambda: """<div id='loading-bar' style='display: none;'></div>""",
274
+ outputs=[upload_loader],
275
+ show_progress=False,
276
+ ).then(
277
+ fn=lambda: """<div id='loading-bar' style='display: block;'></div>""",
278
+ outputs=[model_loader],
279
+ show_progress=False,
280
  ).then(
281
  fn=generate,
282
  inputs=[processed_image],
283
+ outputs=[output_model],
284
+ ).then(
285
+ fn=lambda: """<div id='loading-bar' style='display: none;'></div>""",
286
+ outputs=[process_loader],
287
+ show_progress=False,
288
  ).then(
289
+ fn=lambda: """<div id='loading-bar' style='display: none;'></div>""",
290
+ outputs=[model_loader],
291
+ show_progress=False,
292
  )
 
293
  # Запуск приложения
294
  demo.launch(
295
  server_name="0.0.0.0",
296
  server_port=7860,
297
  share=True,
298
+ )