Zaiiida commited on
Commit
c0af06f
1 Parent(s): fdafb07

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -103
app.py CHANGED
@@ -55,135 +55,139 @@ def generate(image):
55
  mesh_path2 = tempfile.NamedTemporaryFile(suffix=".glb", delete=False)
56
  mesh.export(mesh_path2.name)
57
  return mesh_path2.name
 
 
58
 
 
 
59
 
60
  # Настройка темы и CSS
61
  class CustomTheme(gr.themes.Base):
62
  def __init__(self):
63
  super().__init__()
64
- self.primary_hue = "#5271FF"
65
  self.background_fill_primary = "#191a1e"
 
 
66
  self.text_color_primary = "#FFFFFF"
 
 
67
  self.input_background_fill = "#191a1e"
68
  self.input_text_color = "#FFFFFF"
69
 
70
 
71
  css = """
72
- /* Лоадер */
73
- .loader-container {
 
74
  position: absolute;
75
- top: 0;
76
- left: 0;
77
- width: 100%;
78
- height: 100%;
79
- display: flex;
80
- justify-content: center;
81
- align-items: center;
82
- background: rgba(0, 0, 0, 0.5);
83
- z-index: 100;
84
- display: none;
85
- }
86
-
87
- #loading-bar {
88
- width: 60px;
89
- height: 60px;
90
- border: 8px solid #f3f3f3;
91
- border-top: 8px solid #5271FF;
92
  border-radius: 50%;
93
  animation: spin 1s linear infinite;
94
  }
95
 
 
96
  @keyframes spin {
97
- 0% { transform: rotate(0deg); }
98
- 100% { transform: rotate(360deg); }
99
- }
100
-
101
- /* Кнопка Generate */
102
- .generate-button {
103
- background-color: #5271FF !important;
104
- color: #FFFFFF !important;
105
- border: none;
106
- font-weight: bold;
107
- padding: 10px 20px;
108
- border-radius: 5px;
109
- }
110
- .generate-button:hover {
111
- background-color: #405BBF !important;
112
  }
113
 
114
- /* Верхние контейнеры */
115
- .gr-column .gr-box {
116
- position: relative;
117
- height: 300px;
 
118
  }
119
  """
120
 
121
  # Интерфейс
122
- with gr.Blocks(theme=CustomTheme(), css=css) as demo:
123
  with gr.Column():
124
- gr.Markdown("<span style='color: white; font-weight: bold;'>Upload and Process Your Image</span>")
125
- with gr.Row():
126
- with gr.Box():
127
- input_image = gr.Image(
128
- label="Upload Image",
129
- image_mode="RGBA",
130
- sources="upload",
131
- type="pil",
132
- )
133
- gr.HTML("<div class='loader-container' id='loader-dropimage'><div id='loading-bar'></div></div>")
134
- with gr.Box():
135
- processed_image = gr.Image(
136
- label="Processed Image",
137
- interactive=False,
138
- )
139
- gr.HTML("<div class='loader-container' id='loader-processedimage'><div id='loading-bar'></div></div>")
140
- gr.HTML("<div class='loader-container' id='loader-glb'><div id='loading-bar'></div></div>")
141
- foreground_ratio = gr.Slider(
142
- label="Foreground Ratio",
143
- minimum=0.5,
144
- maximum=1.0,
145
- value=0.85,
146
- step=0.05,
147
- )
148
- do_remove_background = gr.Checkbox(
149
- label="Remove Background",
150
- value=True,
151
- )
152
- submit = gr.Button("Generate", elem_classes="generate-button")
153
- output_model = gr.Model3D(
154
- label="Generated GLB Model",
155
- interactive=False,
156
- )
157
-
158
- submit.click(
159
- lambda: gr.update(visible=True, target="loader-dropimage"),
160
- inputs=[],
161
- outputs=[],
162
- ).then(
163
- fn=check_input_image,
164
- inputs=[input_image],
165
- outputs=[]
166
- ).then(
167
- lambda: gr.update(visible=True, target="loader-processedimage"),
168
- inputs=[],
169
- outputs=[],
170
- ).then(
171
- fn=preprocess,
172
- inputs=[input_image, do_remove_background, foreground_ratio],
173
- outputs=[processed_image]
174
- ).then(
175
- lambda: gr.update(visible=True, target="loader-glb"),
176
- inputs=[],
177
- outputs=[],
178
- ).then(
179
- fn=generate,
180
- inputs=[processed_image],
181
- outputs=[output_model]
182
- ).then(
183
- lambda: gr.update(visible=False, targets=["loader-dropimage", "loader-processedimage", "loader-glb"]),
184
- inputs=[],
185
- outputs=[],
186
- )
 
 
 
 
 
 
 
 
 
 
 
 
187
 
188
  demo.launch(
189
  server_name="0.0.0.0",
 
55
  mesh_path2 = tempfile.NamedTemporaryFile(suffix=".glb", delete=False)
56
  mesh.export(mesh_path2.name)
57
  return mesh_path2.name
58
+ def start_loading(loader_id):
59
+ return f"<script>document.getElementById('{loader_id}').style.display = 'block';</script>"
60
 
61
+ def stop_loading(loader_id):
62
+ return f"<script>document.getElementById('{loader_id}').style.display = 'none';</script>"
63
 
64
  # Настройка темы и CSS
65
  class CustomTheme(gr.themes.Base):
66
  def __init__(self):
67
  super().__init__()
68
+ self.primary_hue = "#191a1e"
69
  self.background_fill_primary = "#191a1e"
70
+ self.background_fill_secondary = "#191a1e"
71
+ self.background_fill_tertiary = "#191a1e"
72
  self.text_color_primary = "#FFFFFF"
73
+ self.text_color_secondary = "#FFFFFF"
74
+ self.text_color_tertiary = "#FFFFFF"
75
  self.input_background_fill = "#191a1e"
76
  self.input_text_color = "#FFFFFF"
77
 
78
 
79
  css = """
80
+ /* Общий стиль лоадеров */
81
+ .loader {
82
+ display: none; /* Скрыт по умолчанию */
83
  position: absolute;
84
+ top: 50%;
85
+ left: 50%;
86
+ transform: translate(-50%, -50%);
87
+ width: 40px;
88
+ height: 40px;
89
+ border: 5px solid #f3f3f3; /* Белый круг */
90
+ border-top: 5px solid #5271FF; /* Синий круг */
 
 
 
 
 
 
 
 
 
 
91
  border-radius: 50%;
92
  animation: spin 1s linear infinite;
93
  }
94
 
95
+ /* Анимация вращения */
96
  @keyframes spin {
97
+ 0% {
98
+ transform: translate(-50%, -50%) rotate(0deg);
99
+ }
100
+ 100% {
101
+ transform: translate(-50%, -50%) rotate(360deg);
102
+ }
 
 
 
 
 
 
 
 
 
103
  }
104
 
105
+ /* Добавить позицию для контейнеров */
106
+ #image-container,
107
+ #process-container,
108
+ #generate-container {
109
+ position: relative; /* Для размещения лоадера внутри */
110
  }
111
  """
112
 
113
  # Интерфейс
114
+ with gr.Blocks(css=css) as demo:
115
  with gr.Column():
116
+ with gr.Row(elem_id="image-container"):
117
+ input_image = gr.Image(
118
+ label="Upload Image",
119
+ image_mode="RGBA",
120
+ sources="upload",
121
+ type="pil",
122
+ width=400,
123
+ height=300,
124
+ )
125
+ processed_image = gr.Image(
126
+ label="Processed Image",
127
+ interactive=False,
128
+ width=400,
129
+ height=300,
130
+ )
131
+ loading_bar_image = gr.HTML("<div class='loader' id='image-loader'></div>")
132
+
133
+ with gr.Row(elem_id="process-container"):
134
+ foreground_ratio = gr.Slider(
135
+ label="Foreground Ratio",
136
+ minimum=0.5,
137
+ maximum=1.0,
138
+ value=0.85,
139
+ step=0.05,
140
+ )
141
+ do_remove_background = gr.Checkbox(label="Remove Background", value=True)
142
+ loading_bar_process = gr.HTML("<div class='loader' id='process-loader'></div>")
143
+
144
+ with gr.Row(elem_id="generate-container"):
145
+ submit = gr.Button("Generate", elem_classes="generate-button")
146
+ output_model = gr.Model3D(
147
+ label="Generated GLB Model",
148
+ interactive=False,
149
+ elem_classes="gr-model3d-container",
150
+ )
151
+ loading_bar_generate = gr.HTML("<div class='loader' id='generate-loader'></div>")
152
+ # Обновленная цепочка действий для кнопки submit
153
+ submit.click(
154
+ fn=lambda: start_loading('image-loader'), # Показать лоадер для загрузки изображения
155
+ inputs=[],
156
+ outputs=[loading_bar_image]
157
+ ).then(
158
+ fn=check_input_image, # Проверить изображение
159
+ inputs=[input_image],
160
+ outputs=[]
161
+ ).then(
162
+ fn=lambda: stop_loading('image-loader'), # Скрыть лоадер для загрузки
163
+ inputs=[],
164
+ outputs=[loading_bar_image]
165
+ ).then(
166
+ fn=lambda: start_loading('process-loader'), # Показать лоадер для обработки
167
+ inputs=[],
168
+ outputs=[loading_bar_process]
169
+ ).then(
170
+ fn=preprocess, # Обработка изображения
171
+ inputs=[input_image, do_remove_background, foreground_ratio],
172
+ outputs=[processed_image]
173
+ ).then(
174
+ fn=lambda: stop_loading('process-loader'), # Скрыть лоадер для обработки
175
+ inputs=[],
176
+ outputs=[loading_bar_process]
177
+ ).then(
178
+ fn=lambda: start_loading('generate-loader'), # Показать лоадер для генерации
179
+ inputs=[],
180
+ outputs=[loading_bar_generate]
181
+ ).then(
182
+ fn=generate, # Генерация модели
183
+ inputs=[processed_image],
184
+ outputs=[output_model]
185
+ ).then(
186
+ fn=lambda: stop_loading('generate-loader'), # Скрыть лоадер для генерации
187
+ inputs=[],
188
+ outputs=[loading_bar_generate]
189
+ )
190
+
191
 
192
  demo.launch(
193
  server_name="0.0.0.0",