Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -19,11 +19,13 @@ model = TSR.from_pretrained(
|
|
19 |
model.renderer.set_chunk_size(131072)
|
20 |
model.to(device)
|
21 |
|
|
|
22 |
# Функция для проверки изображения
|
23 |
def check_input_image(input_image):
|
24 |
if input_image is None:
|
25 |
raise gr.Error("No image uploaded!")
|
26 |
|
|
|
27 |
# Функция обработки изображения
|
28 |
def preprocess(input_image, do_remove_background, foreground_ratio):
|
29 |
def fill_background(image):
|
@@ -43,6 +45,7 @@ def preprocess(input_image, do_remove_background, foreground_ratio):
|
|
43 |
image = fill_background(image)
|
44 |
return image
|
45 |
|
|
|
46 |
# Функция генерации 3D модели
|
47 |
def generate(image):
|
48 |
time.sleep(3) # Эмуляция времени обработки
|
@@ -53,19 +56,41 @@ def generate(image):
|
|
53 |
mesh.export(mesh_path2.name)
|
54 |
return mesh_path2.name
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
return f""
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
"""
|
66 |
|
67 |
# Настройка темы и CSS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
css = """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
#loading-bar {
|
70 |
display: none; /* Скрыт по умолчанию */
|
71 |
position: absolute;
|
@@ -79,40 +104,54 @@ css = """
|
|
79 |
border-radius: 50%;
|
80 |
animation: spin 1s linear infinite;
|
81 |
}
|
|
|
82 |
@keyframes spin {
|
83 |
0% { transform: translate(-50%, -50%) rotate(0deg); }
|
84 |
100% { transform: translate(-50%, -50%) rotate(360deg); }
|
85 |
}
|
86 |
-
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
}
|
89 |
"""
|
90 |
|
91 |
# Интерфейс
|
92 |
-
with gr.Blocks(css=css) as demo:
|
93 |
with gr.Column():
|
94 |
gr.Markdown("<span style='color: white; font-weight: bold;'>Upload and Process Your Image</span>")
|
95 |
with gr.Row():
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
interactive=False,
|
111 |
-
width=400,
|
112 |
-
height=300,
|
113 |
-
)
|
114 |
-
process_loader = gr.HTML("<div id='loading-bar'></div>")
|
115 |
-
|
116 |
foreground_ratio = gr.Slider(
|
117 |
label="Foreground Ratio",
|
118 |
minimum=0.5,
|
@@ -120,43 +159,46 @@ with gr.Blocks(css=css) as demo:
|
|
120 |
value=0.85,
|
121 |
step=0.05,
|
122 |
)
|
123 |
-
do_remove_background = gr.Checkbox(
|
|
|
|
|
|
|
124 |
submit = gr.Button("Generate", elem_classes="generate-button")
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
|
132 |
submit.click(
|
133 |
-
fn=lambda:
|
134 |
inputs=[],
|
135 |
-
outputs=[
|
136 |
).then(
|
137 |
fn=check_input_image,
|
138 |
inputs=[input_image],
|
139 |
outputs=[]
|
|
|
|
|
|
|
|
|
140 |
).then(
|
141 |
fn=preprocess,
|
142 |
inputs=[input_image, do_remove_background, foreground_ratio],
|
143 |
outputs=[processed_image]
|
144 |
).then(
|
145 |
-
fn=lambda:
|
146 |
-
inputs=[],
|
147 |
-
outputs=[
|
148 |
-
).then(
|
149 |
-
fn=lambda: toggle_loader("GLB", True), # Включаем лоадер в GLB
|
150 |
-
inputs=[],
|
151 |
-
outputs=[glb_loader],
|
152 |
).then(
|
153 |
fn=generate,
|
154 |
inputs=[processed_image],
|
155 |
outputs=[output_model]
|
156 |
).then(
|
157 |
-
fn=
|
158 |
inputs=[],
|
159 |
-
outputs=[
|
160 |
)
|
161 |
|
162 |
# Запуск приложения
|
|
|
19 |
model.renderer.set_chunk_size(131072)
|
20 |
model.to(device)
|
21 |
|
22 |
+
|
23 |
# Функция для проверки изображения
|
24 |
def check_input_image(input_image):
|
25 |
if input_image is None:
|
26 |
raise gr.Error("No image uploaded!")
|
27 |
|
28 |
+
|
29 |
# Функция обработки изображения
|
30 |
def preprocess(input_image, do_remove_background, foreground_ratio):
|
31 |
def fill_background(image):
|
|
|
45 |
image = fill_background(image)
|
46 |
return image
|
47 |
|
48 |
+
|
49 |
# Функция генерации 3D модели
|
50 |
def generate(image):
|
51 |
time.sleep(3) # Эмуляция времени обработки
|
|
|
56 |
mesh.export(mesh_path2.name)
|
57 |
return mesh_path2.name
|
58 |
|
59 |
+
|
60 |
+
# Функция для отображения загрузки
|
61 |
+
def set_loader_context(context):
|
62 |
+
return f"<div id='loading-bar' class='loader-{context}' style='display: block;'></div>"
|
63 |
+
|
64 |
+
|
65 |
+
def stop_loading():
|
66 |
+
return "<div id='loading-bar' style='display: none;'></div>"
|
67 |
+
|
|
|
68 |
|
69 |
# Настройка темы и CSS
|
70 |
+
class CustomTheme(gr.themes.Base):
|
71 |
+
def __init__(self):
|
72 |
+
super().__init__()
|
73 |
+
self.primary_hue = "#191a1e"
|
74 |
+
self.background_fill_primary = "#191a1e"
|
75 |
+
self.background_fill_secondary = "#191a1e"
|
76 |
+
self.background_fill_tertiary = "#191a1e"
|
77 |
+
self.text_color_primary = "#FFFFFF"
|
78 |
+
self.text_color_secondary = "#FFFFFF"
|
79 |
+
self.text_color_tertiary = "#FFFFFF"
|
80 |
+
self.input_background_fill = "#191a1e"
|
81 |
+
self.input_text_color = "#FFFFFF"
|
82 |
+
|
83 |
+
|
84 |
css = """
|
85 |
+
/* Скрываем нижний колонтитул */
|
86 |
+
footer {
|
87 |
+
visibility: hidden;
|
88 |
+
height: 0;
|
89 |
+
margin: 0;
|
90 |
+
padding: 0;
|
91 |
+
overflow: hidden;
|
92 |
+
}
|
93 |
+
/* Лоадер (анимация) */
|
94 |
#loading-bar {
|
95 |
display: none; /* Скрыт по умолчанию */
|
96 |
position: absolute;
|
|
|
104 |
border-radius: 50%;
|
105 |
animation: spin 1s linear infinite;
|
106 |
}
|
107 |
+
/* Анимация вращения */
|
108 |
@keyframes spin {
|
109 |
0% { transform: translate(-50%, -50%) rotate(0deg); }
|
110 |
100% { transform: translate(-50%, -50%) rotate(360deg); }
|
111 |
}
|
112 |
+
/* Контексты лоадера */
|
113 |
+
.loader-DropImage {
|
114 |
+
position: absolute;
|
115 |
+
width: 100%;
|
116 |
+
height: 100%;
|
117 |
+
top: 0;
|
118 |
+
left: 0;
|
119 |
+
}
|
120 |
+
.loader-ProcessedImage {
|
121 |
+
position: absolute;
|
122 |
+
width: 100%;
|
123 |
+
height: 100%;
|
124 |
+
top: 0;
|
125 |
+
left: 0;
|
126 |
+
}
|
127 |
+
.loader-GLB {
|
128 |
+
position: absolute;
|
129 |
+
width: 100%;
|
130 |
+
height: 100%;
|
131 |
+
top: 0;
|
132 |
+
left: 0;
|
133 |
}
|
134 |
"""
|
135 |
|
136 |
# Интерфейс
|
137 |
+
with gr.Blocks(theme=CustomTheme(), css=css) as demo:
|
138 |
with gr.Column():
|
139 |
gr.Markdown("<span style='color: white; font-weight: bold;'>Upload and Process Your Image</span>")
|
140 |
with gr.Row():
|
141 |
+
input_image = gr.Image(
|
142 |
+
label="Upload Image",
|
143 |
+
image_mode="RGBA",
|
144 |
+
sources="upload",
|
145 |
+
type="pil",
|
146 |
+
width=400,
|
147 |
+
height=300,
|
148 |
+
)
|
149 |
+
processed_image = gr.Image(
|
150 |
+
label="Processed Image",
|
151 |
+
interactive=False,
|
152 |
+
width=400,
|
153 |
+
height=300,
|
154 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
foreground_ratio = gr.Slider(
|
156 |
label="Foreground Ratio",
|
157 |
minimum=0.5,
|
|
|
159 |
value=0.85,
|
160 |
step=0.05,
|
161 |
)
|
162 |
+
do_remove_background = gr.Checkbox(
|
163 |
+
label="Remove Background", # Текст перекрашен в белый через CSS
|
164 |
+
value=True,
|
165 |
+
)
|
166 |
submit = gr.Button("Generate", elem_classes="generate-button")
|
167 |
+
loading_bar = gr.HTML("<div id='loading-bar'></div>")
|
168 |
+
output_model = gr.Model3D(
|
169 |
+
label="Generated GLB Model",
|
170 |
+
interactive=False,
|
171 |
+
elem_classes="gr-model3d-container",
|
172 |
+
)
|
173 |
|
174 |
submit.click(
|
175 |
+
fn=lambda: set_loader_context("DropImage"), # Установить лоадер для DropImage
|
176 |
inputs=[],
|
177 |
+
outputs=[loading_bar],
|
178 |
).then(
|
179 |
fn=check_input_image,
|
180 |
inputs=[input_image],
|
181 |
outputs=[]
|
182 |
+
).then(
|
183 |
+
fn=lambda image, do_remove_bg, ratio: set_loader_context("ProcessedImage"),
|
184 |
+
inputs=[input_image, do_remove_background, foreground_ratio],
|
185 |
+
outputs=[loading_bar],
|
186 |
).then(
|
187 |
fn=preprocess,
|
188 |
inputs=[input_image, do_remove_background, foreground_ratio],
|
189 |
outputs=[processed_image]
|
190 |
).then(
|
191 |
+
fn=lambda image: set_loader_context("GLB"),
|
192 |
+
inputs=[processed_image],
|
193 |
+
outputs=[loading_bar],
|
|
|
|
|
|
|
|
|
194 |
).then(
|
195 |
fn=generate,
|
196 |
inputs=[processed_image],
|
197 |
outputs=[output_model]
|
198 |
).then(
|
199 |
+
fn=stop_loading, # Отключаем прогресс-бар после завершения генерации
|
200 |
inputs=[],
|
201 |
+
outputs=[loading_bar],
|
202 |
)
|
203 |
|
204 |
# Запуск приложения
|