Zaiiida commited on
Commit
3158a8e
·
verified ·
1 Parent(s): 8d17e28

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -128
app.py CHANGED
@@ -1,27 +1,16 @@
1
- import logging
2
- import os
3
- import tempfile
4
- import time
5
-
6
  import gradio as gr
 
 
7
  import numpy as np
8
- import rembg
9
  import torch
10
  from PIL import Image
11
- from functools import partial
12
-
13
  from tsr.system import TSR
14
  from tsr.utils import remove_background, resize_foreground, to_gradio_3d_orientation
15
 
16
- if torch.cuda.is_available():
17
- device = "cuda:0"
18
- else:
19
- device = "cpu"
20
-
21
- d = os.environ.get("DEVICE", None)
22
- if d != None:
23
- device = d
24
 
 
25
  model = TSR.from_pretrained(
26
  "stabilityai/TripoSR",
27
  config_name="config.yaml",
@@ -30,14 +19,14 @@ model = TSR.from_pretrained(
30
  model.renderer.set_chunk_size(131072)
31
  model.to(device)
32
 
33
- rembg_session = rembg.new_session()
34
-
35
 
 
36
  def check_input_image(input_image):
37
  if input_image is None:
38
  raise gr.Error("No image uploaded!")
39
 
40
 
 
41
  def preprocess(input_image, do_remove_background, foreground_ratio):
42
  def fill_background(image):
43
  image = np.array(image).astype(np.float32) / 255.0
@@ -47,7 +36,7 @@ def preprocess(input_image, do_remove_background, foreground_ratio):
47
 
48
  if do_remove_background:
49
  image = input_image.convert("RGB")
50
- image = remove_background(image, rembg_session)
51
  image = resize_foreground(image, foreground_ratio)
52
  image = fill_background(image)
53
  else:
@@ -57,6 +46,7 @@ def preprocess(input_image, do_remove_background, foreground_ratio):
57
  return image
58
 
59
 
 
60
  def generate(image):
61
  scene_codes = model(image, device=device)
62
  mesh = model.extract_mesh(scene_codes)[0]
@@ -66,6 +56,7 @@ def generate(image):
66
  return mesh_path2.name
67
 
68
 
 
69
  class CustomTheme(gr.themes.Base):
70
  def __init__(self):
71
  super().__init__()
@@ -81,145 +72,64 @@ class CustomTheme(gr.themes.Base):
81
 
82
 
83
  css = """
84
- footer {
85
- visibility: hidden;
86
- height: 0;
87
- margin: 0;
88
- padding: 0;
89
- overflow: hidden;
90
- }
91
-
92
- body, input, button, textarea, select, .gr-button {
93
- font-family: 'Poppins', sans-serif;
94
  background-color: #191a1e !important;
95
- color: #FFFFFF;
96
  }
97
 
98
- h1, h2, h3, h4, h5, h6 {
99
- font-family: 'Poppins', sans-serif;
100
- font-weight: 700;
101
  color: #FFFFFF;
102
  }
103
 
104
- input[type="text"], textarea {
105
  background-color: #191a1e !important;
106
- color: #FFFFFF;
107
  border: 1px solid #FFFFFF;
108
  }
109
 
110
- .generate-button {
111
  background-color: #5271FF !important;
112
- color: #FFFFFF !important;
113
- border: none;
114
- font-weight: bold;
115
  }
116
 
117
- .generate-button:hover {
118
  background-color: #405BBF !important;
119
  }
120
-
121
- gr-image-upload {
122
- background-color: #191a1e !important;
123
- border: 1px dashed #FFFFFF !important;
124
- position: relative;
125
- }
126
-
127
- .gr-image-upload .dropzone::before {
128
- content: 'Drop Image Here';
129
- color: #FFFFFF;
130
- font-size: 1rem;
131
- font-weight: bold;
132
- text-align: center;
133
- position: absolute;
134
- top: 50%;
135
- left: 50%;
136
- transform: translate(-50%, -50%);
137
- }
138
-
139
- .gr-image-upload .dropzone .file-upload {
140
- display: none !important;
141
- }
142
-
143
- .drop-image-container {
144
- display: flex;
145
- flex-direction: column;
146
- align-items: center;
147
- }
148
-
149
- .drop-image, .processed-image {
150
- margin-bottom: 20px;
151
- }
152
-
153
- .foreground-ratio-container {
154
- margin-top: 20px;
155
- margin-bottom: 20px;
156
- }
157
-
158
- /* Вкладки GLB Model */
159
- .gr-tab {
160
- background-color: #252525; /* Чуть светлее общий фон */
161
- border: 1px solid #FFFFFF; /* Белая рамка для структуры */
162
- border-radius: 5px; /* Скругление рамок */
163
- margin-top: 10px; /* Отступ от верхнего элемента */
164
- }
165
-
166
- .gr-tab label {
167
- color: #FFFFFF; /* Цвет текста */
168
- font-weight: bold;
169
- }
170
-
171
  """
172
 
 
173
  with gr.Blocks(theme=CustomTheme(), css=css) as demo:
174
- with gr.Column(elem_id="main-container"):
175
- gr.Markdown("**Upload and Process Your Image**", elem_classes="prompt-text")
176
-
177
- with gr.Row(elem_id="image-row"):
178
  input_image = gr.Image(
179
  label="Upload Image",
180
  image_mode="RGBA",
181
  sources="upload",
182
  type="pil",
183
- elem_id="content_image",
184
  width=400,
185
  height=300,
186
  )
187
  processed_image = gr.Image(
188
  label="Processed Image",
189
  interactive=False,
190
- elem_id="processed_image",
191
  width=400,
192
  height=300,
193
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
194
 
195
- with gr.Row(elem_id="slider-row"):
196
- foreground_ratio = gr.Slider(
197
- label="Foreground Ratio",
198
- minimum=0.5,
199
- maximum=1.0,
200
- value=0.85,
201
- step=0.05,
202
- )
203
-
204
- with gr.Row(elem_id="settings-row"):
205
- do_remove_background = gr.Checkbox(
206
- label="Remove Background",
207
- value=True,
208
- )
209
- submit = gr.Button(
210
- "Generate",
211
- scale=0,
212
- variant="primary",
213
- elem_classes="generate-button",
214
- )
215
-
216
- with gr.Row(elem_id="output-model-container"):
217
- with gr.Tab("GLB Model"):
218
- output_model2 = gr.Model3D(
219
- label="Generated GLB Model",
220
- interactive=False,
221
- )
222
-
223
  submit.click(
224
  fn=check_input_image,
225
  inputs=[input_image],
@@ -231,9 +141,8 @@ with gr.Blocks(theme=CustomTheme(), css=css) as demo:
231
  ).then(
232
  fn=generate,
233
  inputs=[processed_image],
234
- outputs=[output_model2]
235
  )
236
 
237
-
238
- demo.queue(max_size=10)
239
  demo.launch()
 
 
 
 
 
 
1
  import gradio as gr
2
+ import tempfile
3
+ import os
4
  import numpy as np
 
5
  import torch
6
  from PIL import Image
 
 
7
  from tsr.system import TSR
8
  from tsr.utils import remove_background, resize_foreground, to_gradio_3d_orientation
9
 
10
+ # Проверяем наличие GPU
11
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
 
 
 
 
 
 
12
 
13
+ # Загружаем модель
14
  model = TSR.from_pretrained(
15
  "stabilityai/TripoSR",
16
  config_name="config.yaml",
 
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):
32
  image = np.array(image).astype(np.float32) / 255.0
 
36
 
37
  if do_remove_background:
38
  image = input_image.convert("RGB")
39
+ image = remove_background(image)
40
  image = resize_foreground(image, foreground_ratio)
41
  image = fill_background(image)
42
  else:
 
46
  return image
47
 
48
 
49
+ # Функция генерации 3D модели
50
  def generate(image):
51
  scene_codes = model(image, device=device)
52
  mesh = model.extract_mesh(scene_codes)[0]
 
56
  return mesh_path2.name
57
 
58
 
59
+ # Настройка темы и CSS
60
  class CustomTheme(gr.themes.Base):
61
  def __init__(self):
62
  super().__init__()
 
72
 
73
 
74
  css = """
75
+ body {
 
 
 
 
 
 
 
 
 
76
  background-color: #191a1e !important;
 
77
  }
78
 
79
+ .gr-container {
80
+ background-color: #191a1e !important;
 
81
  color: #FFFFFF;
82
  }
83
 
84
+ .gr-box, .gr-button, input, textarea {
85
  background-color: #191a1e !important;
86
+ color: #FFFFFF !important;
87
  border: 1px solid #FFFFFF;
88
  }
89
 
90
+ .gr-button {
91
  background-color: #5271FF !important;
 
 
 
92
  }
93
 
94
+ .gr-button:hover {
95
  background-color: #405BBF !important;
96
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  """
98
 
99
+ # Интерфейс
100
  with gr.Blocks(theme=CustomTheme(), css=css) as demo:
101
+ with gr.Column():
102
+ gr.Markdown("**Upload and Process Your Image**")
103
+ with gr.Row():
 
104
  input_image = gr.Image(
105
  label="Upload Image",
106
  image_mode="RGBA",
107
  sources="upload",
108
  type="pil",
 
109
  width=400,
110
  height=300,
111
  )
112
  processed_image = gr.Image(
113
  label="Processed Image",
114
  interactive=False,
 
115
  width=400,
116
  height=300,
117
  )
118
+ foreground_ratio = gr.Slider(
119
+ label="Foreground Ratio",
120
+ minimum=0.5,
121
+ maximum=1.0,
122
+ value=0.85,
123
+ step=0.05,
124
+ )
125
+ do_remove_background = gr.Checkbox(
126
+ label="Remove Background",
127
+ value=True,
128
+ )
129
+ submit = gr.Button("Generate")
130
+ output_model = gr.Model3D(label="Generated GLB Model", interactive=False)
131
 
132
+ # Обработка кликов
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  submit.click(
134
  fn=check_input_image,
135
  inputs=[input_image],
 
141
  ).then(
142
  fn=generate,
143
  inputs=[processed_image],
144
+ outputs=[output_model]
145
  )
146
 
147
+ # Запуск приложения
 
148
  demo.launch()