Zaiiida commited on
Commit
fc2d6a1
1 Parent(s): 94c8594

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +313 -88
app.py CHANGED
@@ -13,22 +13,72 @@ from functools import partial
13
  from tsr.system import TSR
14
  from tsr.utils import remove_background, resize_foreground, to_gradio_3d_orientation
15
 
16
- # DEVICE
17
- DEVICE_ENV = os.environ.get("DEVICE", None)
18
- DEVICE = DEVICE_ENV if DEVICE_ENV else ("cuda:0" if torch.cuda.is_available() else "cpu")
19
 
20
- # MODEL
21
- MODEL_NAME = "stabilityai/TripoSR"
22
- CONFIG_NAME = "config.yaml"
23
- WEIGHT_NAME = "model.ckpt"
24
- # HF_TOKEN = os.getenv("HF_TOKEN") # Если нужен токен
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
- # REMBG
27
  rembg_session = rembg.new_session()
28
 
29
- # THEME
30
- HEADER = """
31
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  class CustomTheme(gr.themes.Base):
34
  def __init__(self):
@@ -43,6 +93,7 @@ class CustomTheme(gr.themes.Base):
43
  self.input_background_fill = "#191a1e"
44
  self.input_text_color = "#FFFFFF"
45
 
 
46
  css = """
47
  /* Скрываем нижний колонтитул */
48
  footer {
@@ -52,7 +103,7 @@ footer {
52
  padding: 0;
53
  overflow: hidden;
54
  }
55
- @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;700&display=swap');
56
  /* Применяем шрифты */
57
  body, input, button, textarea, select,.gr-button {
58
  font-family: 'Poppins', sans-serif;
@@ -80,7 +131,6 @@ input[type="text"], textarea {
80
  }
81
  .generate-button:hover {
82
  background-color: #405BBF!important; /* Цвет при наведении */
83
- }
84
  /* Выравнивание элементов */
85
  .drop-image-container {
86
  display: flex;
@@ -101,106 +151,281 @@ input[type="text"], textarea {
101
  }
102
  """
103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  def check_input_image(input_image):
105
  if input_image is None:
106
- raise gr.Error("No image uploaded!")
107
-
108
- def fill_background(image):
109
- image = np.array(image).astype(np.float32) / 255.0
110
- image = image[:, :, :3] * image[:, :, 3:4] + (1 - image[:, :, 3:4]) * 0.5
111
- image = Image.fromarray((image * 255.0).astype(np.uint8))
112
- return image
113
 
114
  def preprocess(input_image, do_remove_background, foreground_ratio):
 
 
 
 
 
115
  if do_remove_background:
116
  image = input_image.convert("RGB")
117
  image = remove_background(image, rembg_session)
118
  image = resize_foreground(image, foreground_ratio)
119
  image = fill_background(image)
120
- else:
121
- image = input_image
122
- if image.mode == "RGBA":
123
- image = fill_background(image)
124
  return image
125
 
126
  def generate(image):
127
- model = TSR.from_pretrained(
128
- MODEL_NAME,
129
- config_name=CONFIG_NAME,
130
- weight_name=WEIGHT_NAME,
131
- # token=HF_TOKEN
132
- )
133
- model.renderer.set_chunk_size(131072)
134
- model.to(DEVICE)
135
-
136
- scene_codes = model(image, device=DEVICE)
137
  mesh = model.extract_mesh(scene_codes)[0]
138
  mesh = to_gradio_3d_orientation(mesh)
139
- mesh_path = tempfile.NamedTemporaryFile(suffix=".obj", delete=False)
140
  mesh_path2 = tempfile.NamedTemporaryFile(suffix=".glb", delete=False)
141
- mesh.export(mesh_path.name)
142
- mesh.export(mesh_path2.name)
143
  return mesh_path.name, mesh_path2.name
144
 
145
- def run_example(image_pil):
146
- preprocessed = preprocess(image_pil, False, 0.9)
147
- mesh_name, mesh_name2 = generate(preprocessed)
148
- return preprocessed, mesh_name, mesh_name2
149
 
150
- with gr.Blocks(theme=CustomTheme(), css=css) as demo:
151
- # **Header**
152
- with gr.Row():
153
- gr.Markdown("# 3D Model Generator", elem_id="title")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
 
155
- # **Input Section**
156
- with gr.Accordion("Input Image", open=False):
157
- with gr.Column():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
  input_image = gr.Image(
159
- label="Upload Image",
160
  image_mode="RGBA",
161
  sources="upload",
162
  type="pil",
163
  elem_id="content_image",
164
- width=500,
165
- )
166
- with gr.Row():
167
- do_remove_background = gr.Checkbox(
168
- label="Remove Background",
169
- value=True,
170
- )
171
- foreground_ratio = gr.Slider(
172
- label="Foreground Ratio",
173
- minimum=0.5,
174
- maximum=1.0,
175
- value=0.85,
176
- step=0.05,
177
- )
 
 
 
 
 
 
178
 
179
- # **Processing and Generation**
180
- with gr.Accordion("Generate 3D Model", open=False):
181
- with gr.Column():
182
- submit = gr.Button("Generate", elem_classes="generate-button")
 
 
 
 
 
 
 
 
 
 
 
 
 
183
  processed_image = gr.Image(label="Processed Image", interactive=False)
184
- with gr.Tabs():
185
- with gr.Tab("OBJ Model"):
186
- output_model = gr.Model3D(label="Output Model", interactive=False)
187
- with gr.Tab("GLB Model"):
188
- output_model2 = gr.Model3D(label="Output Model", interactive=False)
189
-
190
- # **Event Triggers**
191
- submit.click(
192
- fn=check_input_image,
193
- inputs=[input_image],
194
- outputs=[gr.update(label="Processing...", interactive=False)]
195
- ).then(
196
- fn=preprocess,
197
- inputs=[input_image, do_remove_background, foreground_ratio],
198
- outputs=[processed_image]
199
- ).then(
200
- fn=generate,
201
- inputs=[processed_image],
202
- outputs=[output_model, output_model2]
203
  )
204
 
205
  demo.queue(max_size=10)
206
- demo.launch()
 
13
  from tsr.system import TSR
14
  from tsr.utils import remove_background, resize_foreground, to_gradio_3d_orientation
15
 
16
+ # HF_TOKEN = os.getenv("HF_TOKEN")
 
 
17
 
18
+ HEADER = """
19
+ """
20
+
21
+ if torch.cuda.is_available():
22
+ device = "cuda:0"
23
+ else:
24
+ device = "cpu"
25
+
26
+ d = os.environ.get("DEVICE", None)
27
+ if d!= None:
28
+ device = d
29
+
30
+ model = TSR.from_pretrained(
31
+ "stabilityai/TripoSR",
32
+ config_name="config.yaml",
33
+ weight_name="model.ckpt",
34
+ # token=HF_TOKEN
35
+ )
36
+ model.renderer.set_chunk_size(131072)
37
+ model.to(device)
38
 
 
39
  rembg_session = rembg.new_session()
40
 
41
+
42
+ def check_input_image(input_image):
43
+ if input_image is None:
44
+ raise gr.Error("No image uploaded!")
45
+
46
+
47
+ def preprocess(input_image, do_remove_background, foreground_ratio):
48
+ def fill_background(image):
49
+ image = np.array(image).astype(np.float32) / 255.0
50
+ image = image[:, :, :3] * image[:, :, 3:4] + (1 - image[:, :, 3:4]) * 0.5
51
+ image = Image.fromarray((image * 255.0).astype(np.uint8))
52
+ return image
53
+
54
+ if do_remove_background:
55
+ image = input_image.convert("RGB")
56
+ image = remove_background(image, rembg_session)
57
+ image = resize_foreground(image, foreground_ratio)
58
+ image = fill_background(image)
59
+ else:
60
+ image = input_image
61
+ if image.mode == "RGBA":
62
+ image = fill_background(image)
63
+ return image
64
+
65
+
66
+ def generate(image):
67
+ scene_codes = model(image, device=device)
68
+ mesh = model.extract_mesh(scene_codes)[0]
69
+ mesh = to_gradio_3d_orientation(mesh)
70
+ mesh_path = tempfile.NamedTemporaryFile(suffix=".obj", delete=False)
71
+ mesh_path2 = tempfile.NamedTemporaryFile(suffix=".glb", delete=False)
72
+ mesh.export(mesh_path.name)
73
+ mesh.export(mesh_path2.name)
74
+ return mesh_path.name, mesh_path2.name
75
+
76
+
77
+ def run_example(image_pil):
78
+ preprocessed = preprocess(image_pil, False, 0.9)
79
+ mesh_name, mesn_name2 = generate(preprocessed)
80
+ return preprocessed, mesh_name, mesn_name2
81
+
82
 
83
  class CustomTheme(gr.themes.Base):
84
  def __init__(self):
 
93
  self.input_background_fill = "#191a1e"
94
  self.input_text_color = "#FFFFFF"
95
 
96
+
97
  css = """
98
  /* Скрываем нижний колонтитул */
99
  footer {
 
103
  padding: 0;
104
  overflow: hidden;
105
  }
106
+ @import url('https://fonts.googleapis.com/css2?familyPoppins:wght@400;500;700&display=swap');
107
  /* Применяем шрифты */
108
  body, input, button, textarea, select,.gr-button {
109
  font-family: 'Poppins', sans-serif;
 
131
  }
132
  .generate-button:hover {
133
  background-color: #405BBF!important; /* Цвет при наведении */
 
134
  /* Выравнивание элементов */
135
  .drop-image-container {
136
  display: flex;
 
151
  }
152
  """
153
 
154
+ with gr.Blocks(theme=CustomTheme(), css=css) as demo:
155
+ with gr.Column(elem_id="col-container"):
156
+ gr.Markdown("**Input Image**", elem_classes="prompt-text")
157
+
158
+ with gr.Row():
159
+ input_image = gr.Image(
160
+ label="",
161
+ image_mode="RGBA",
162
+ sources="upload",
163
+ type="pil",
164
+ elem_id="content_image",
165
+ width=500, # Увеличена ширина входного изображения
166
+ )
167
+ with gr.Column():
168
+ do_remove_background = gr.Checkbox(
169
+ label="Remove Background",
170
+ value=True,
171
+ )
172
+ foreground_ratio = gr.Slider(
173
+ label="Foreground Ratio",
174
+ minimum=0.5,
175
+ maximum=1.0,
176
+ value=0.85,
177
+ step=0.05,
178
+ )
179
+
180
+ processed_image = gr.Image(label="Processed Image", interactive=False)
181
+
182
+ with gr.Row():
183
+ submit = gr.Button(
184
+ "Generate",
185
+ scale=0,
186
+ variant="primary",
187
+ elem_classes="generate-button",
188
+ )
189
+
190
+ with gr.Tab("obj"):
191
+ output_model = gr.Model3D(
192
+ label="Output Model",
193
+ interactive=False,
194
+ )
195
+ with gr.Tab("glb"):
196
+ output_model2 = gr.Model3D(
197
+ label="Output Model",
198
+ interactive=False,
199
+ )
200
+
201
+ submit.click(fn=check_input_image, inputs=[input_image]).success(
202
+ fn=preprocess,
203
+ inputs=[input_image, do_remove_background, foreground_ratio],
204
+ outputs=[processed_image],
205
+ ).success(
206
+ fn=generate,
207
+ inputs=[processed_image],
208
+ outputs=[output_model, output_model2],
209
+ )
210
+
211
+ demo.queue(max_size=10)
212
+ demo.launch()
213
+
214
+ Улучши структуру отображения пользовательского интерфейса но не меняй стиль
215
+
216
+ Конечно, я могу помочь улучшить структуру отображения пользовательского интерфейса, сохранив при этом выбранный вами стиль. Вот обновленный код с улучшенной структурой:
217
+
218
+ import logging
219
+ import os
220
+ import tempfile
221
+ import time
222
+ import gradio as gr
223
+ import numpy as np
224
+ import rembg
225
+ import torch
226
+ from PIL import Image
227
+ from functools import partial
228
+ from tsr.system import TSR
229
+ from tsr.utils importremove_background, resize_foreground, to_gradio_3d_orientation
230
+
231
+ # HF_TOKEN = os.getenv("HF_TOKEN")
232
+
233
+ HEADER = """
234
+ """
235
+
236
+ if torch.cuda.is_available():
237
+ device = "cuda:0"
238
+ else:
239
+ device = "cpu"
240
+
241
+ d = os.environ.get("DEVICE", None)
242
+ if d is not None:
243
+ device = d
244
+
245
+ model = TSR.from_pretrained(
246
+ "stabilityai/TripoSR",
247
+ config_name="config",
248
+ weight_name="model.ckpt",
249
+ # token=HF_TOKEN
250
+ )
251
+ model.renderer.set_chunk_size(131072)
252
+
253
+ model.to(device)
254
+
255
+ rembg_session = rembg.remb_session()
256
+
257
  def check_input_image(input_image):
258
  if input_image is None:
259
+ raise Gr.Error("No image uploaded!")
 
 
 
 
 
 
260
 
261
  def preprocess(input_image, do_remove_background, foreground_ratio):
262
+ def fill_background(image):
263
+ image = np.array(image).astype(np.float32) / 255.0
264
+ image = image[:, :, :3] * image[:, :, 3:4] + 0.5 * (1 - image[:, :, 3:4])
265
+ image = Image.fromarray((image * 255.0).astype(np.uint8)
266
+
267
  if do_remove_background:
268
  image = input_image.convert("RGB")
269
  image = remove_background(image, rembg_session)
270
  image = resize_foreground(image, foreground_ratio)
271
  image = fill_background(image)
272
+
 
 
 
273
  return image
274
 
275
  def generate(image):
276
+ scene_codes = model(image, device=device)
 
 
 
 
 
 
 
 
 
277
  mesh = model.extract_mesh(scene_codes)[0]
278
  mesh = to_gradio_3d_orientation(mesh)
279
+ mesh_path = tempfile.NamedTemporaryFile(suffix=".obj", delete):
280
  mesh_path2 = tempfile.NamedTemporaryFile(suffix=".glb", delete=False)
281
+ mesh_path2 = tempfile.NamedTemporaryFile(suffix=".glb", delete=False)
282
+
283
  return mesh_path.name, mesh_path2.name
284
 
285
+ def run_example(image_pил):
286
+ preprocessed = preprocess(image_pил, False, 0.9)
287
+ mesh_name, mes_path2.name = generate(image_pил)
 
288
 
289
+ return preprocessed, mesh_name, mesh_name2.name
290
+
291
+ class CustomTheme(Gr.themes.Base):
292
+ def __init__(self):
293
+ self.primary_h = "#191a1e"
294
+ self.background_fill_primary = "#191a1e"
295
+ self.background_fill_secondary = "#191a1e"
296
+ self.background_fill_tertiary = "#191a1e"
297
+ self.text_color_primary = "#FFFFFF"
298
+ self.text_color_secondary = "#FFFFFF"
299
+ self.text_color_tertiар = "#FFFFFF"
300
+ self.input_background_fill = "#191a1e"
301
+ self.input_text_color = "#FFFFFF"
302
+
303
+ css = """
304
+ /* Скрываем нижление */
305
+ footer {
306
+ display: none;
307
+ height: 0;
308
+ margin: 0;
309
+ padding: 0;
310
+ overflow: hidden;
311
+ }
312
+ @import url('https://fonts.googleapis.com/css2?family=Poppins:w сем/css_code = """
313
+ /* Применяем шрифты */
314
+ body, input, button, textarea, select,.gr-button {
315
+ font-family: 'Poppins', sans-serif;
316
+ background-color: #191a1e!important;
317
+ color: #FFFFFF;
318
+ }
319
+ /* Настройки заголовков */
320
+ h1, h2, h3, h4, h5, h6 {
321
+ font-family: 'Poppins', sans-serif;
322
+ font-weight: 700;
323
+ color: #FFFFFF;
324
+ }
325
+ /* Стиль для текстовых пололей */
326
+ input[type="text"], textarea {
327
+ background-color: #191a11e!important;
328
+ color: #FFFFFF;
329
+ border: 1px solid #FFFFFF;
330
+ }
331
 
332
+ /* Цвет кнопки Generate */
333
+ .generate-button {
334
+ background-color: #5271FF!important;
335
+ color: #FFFFFF!important;
336
+ border: none;
337
+ font-weight: bold;
338
+ }
339
+ .generate-button:hover {
340
+ background-color: #405BBF!important; /* Цвет при наведении */
341
+ }
342
+
343
+ .drop-image-container {
344
+ display: flex;
345
+ flex-direction: column;
346
+ align-items: center;
347
+ }
348
+ .drop-image,.processed-image {
349
+ margin-bottom: 20px;
350
+ }
351
+ .foreground-ratio-container {
352
+ margin-top: 20px;
353
+ margin-bottom: 20px;
354
+ }
355
+ .generate-button {
356
+ margin-top: 20px;
357
+ margin-left: auto;
358
+ margin-right: auto;
359
+ }
360
+
361
+ with Gr.ColumnTheme() as demo:
362
+ with gr.Column(elem_id="col-container"):
363
+ gr.Markdown("**Input Image**", elem_classes="prompt-text")
364
+
365
+ with gr.Row():
366
  input_image = gr.Image(
367
+ label="",
368
  image_mode="RGBA",
369
  sources="upload",
370
  type="pil",
371
  elem_id="content_image",
372
+ width=500, /* Увелинена ширина */
373
+ processed_image = gr image(label="Processed Image", interactive=False)
374
+
375
+ with gr.Row():
376
+ submitовуй разия:
377
+ preprocessed = generate(input_image, inputs=[input_image, do_remove_background, foreground_ratio])
378
+ outputs=[processed_image]
379
+
380
+ submit = gr.Button(
381
+ "Generate",
382
+ scale=0,
383
+ variant="primary",
384
+ elem_classes="generate-button",
385
+ )
386
+
387
+ with gr.Tab("obj"):
388
+ output_model = gr.Model3D(
389
+ label="Output Model",
390
+ interactive=False,
391
+ )
392
 
393
+ with gr.tab("glb"):
394
+ output_model2 = gr Model3D(
395
+ label="Output Model",
396
+ interactive=False,
397
+ )
398
+
399
+ with gr.Column(elem_id="col-container"):
400
+ gr.Markdown("**Input Image**", elem_classes="prompt-text")
401
+
402
+ with gr.Row():
403
+ input_image = gr.Image(
404
+ label="",
405
+ image_mode="RGBA",
406
+ sources="upload",
407
+ type="pil",
408
+ elem_id="content_image",
409
+ width=500, /* Увелинена ширина */
410
  processed_image = gr.Image(label="Processed Image", interactive=False)
411
+
412
+ with gr.Row():
413
+ submit = gr.Button(
414
+ "Generate",
415
+ scale=0,
416
+ variant="primary",
417
+ elem_classes="generate-button",
418
+ )
419
+
420
+ submit.click(fn=check_input_image, inputs=[input_image]).success(
421
+ fn=preprocess,
422
+ inputs=[input_image, do_remove_background, foreground_ratio],
423
+ outputs=[processed_image],
424
+ ).success(
425
+ fn=generate,
426
+ inputs=[processed_image],
427
+ outputs=[output_model, output_model2],
 
 
428
  )
429
 
430
  demo.queue(max_size=10)
431
+ demo.launch()