Spaces:
Paused
Paused
Update app.py
Browse files
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 = "#
|
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
|
|
|
74 |
position: absolute;
|
75 |
-
top:
|
76 |
-
left:
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
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% {
|
98 |
-
|
99 |
-
}
|
100 |
-
|
101 |
-
|
102 |
-
|
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 |
-
|
116 |
-
|
117 |
-
|
|
|
118 |
}
|
119 |
"""
|
120 |
|
121 |
# Интерфейс
|
122 |
-
with gr.Blocks(
|
123 |
with gr.Column():
|
124 |
-
gr.
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
label="Remove Background",
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
)
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
)
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
)
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
)
|
183 |
-
|
184 |
-
|
185 |
-
|
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",
|