Zaiiida commited on
Commit
4d1882c
1 Parent(s): 898452b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +190 -183
app.py CHANGED
@@ -13,200 +13,207 @@ 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
- # 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):
85
- super().__init__()
86
- self.primary_hue = "#191a1e"
87
- self.background_fill_primary = "#191a1e"
88
- self.background_fill_secondary = "#191a1e"
89
- self.background_fill_tertiary = "#191a1e"
90
- self.text_color_primary = "#FFFFFF"
91
- self.text_color_secondary = "#FFFFFF"
92
- self.text_color_tertiary = "#FFFFFF"
93
- self.input_background_fill = "#191a1e"
94
- self.input_text_color = "#FFFFFF"
95
-
96
 
97
  css = """
98
- /* Скрываем нижний колонтитул */
99
- footer {
100
- visibility: hidden;
101
- height: 0;
102
- margin: 0;
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;
110
- background-color: #191a1e!important;
111
- color: #FFFFFF;
112
- }
113
- /* Настройки заголовков */
114
- h1, h2, h3, h4, h5, h6 {
115
- font-family: 'Poppins', sans-serif;
116
- font-weight: 700;
117
- color: #FFFFFF;
118
- }
119
- /* Стиль для текстовых полей и кнопок */
120
- input[type="text"], textarea {
121
- background-color: #191a1e!important;
122
- color: #FFFFFF;
123
- border: 1px solid #FFFFFF;
124
- }
125
- /* Цвет кнопки Generate */
126
- .generate-button {
127
- background-color: #5271FF!important;
128
- color: #FFFFFF!important;
129
- border: none;
130
- font-weight: bold;
131
- }
132
- .generate-button:hover {
133
- background-color: #405BBF!important; /* Цвет при наведении */
134
- /* Выравнивание элементов */
135
- .drop-image-container {
136
- display: flex;
137
- flex-direction: column;
138
- align-items: center;
139
- }
140
- .drop-image,.processed-image {
141
- margin-bottom: 20px;
142
- }
143
- .foreground-ratio-container {
144
- margin-top: 20px;
145
- margin-bottom: 20px;
146
- }
147
- .generate-button {
148
- margin-top: 20px;
149
- margin-left: auto;
150
- margin-right: auto;
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()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  from tsr.system import TSR
14
  from tsr.utils import remove_background, resize_foreground, to_gradio_3d_orientation
15
 
16
+ #...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  css = """
19
+ /*... */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  """
21
 
22
  with gr.Blocks(theme=CustomTheme(), css=css) as demo:
23
+ #...
24
+
25
+ with gr.Column():
26
+ gr.Markdown("**Input Image**")
27
+ input_image = gr.Image(
28
+ label="",
29
+ image_mode="RGBA",
30
+ sources="upload",
31
+ type="pil",
32
+ elem_id="content_image",
33
+ width=500, # Увеличена ширина входных изображений
34
+ )
35
+ with gr.Column():
36
+ do_remove_background = gr.Checkbox(
37
+ label="Remove Background",
38
+ value=True,
39
+ elem_id="remove_background",
40
  )
41
+ foreground_ratio = gr.Slider(
42
+ label="Foreground Ratio",
43
+ minimum=0.5,
44
+ maximum=1.0,
45
+ value=0.85,
46
+ step=0.05,
47
+ elem_id="foreground_ratio",
48
+ )
49
+ with gr.Row():
50
+ submit = gr.Button(
51
+ label="Generate",
52
+ scale=0,
53
+ variant="primary",
54
+ elem_classes="generate-button",
55
  )
56
+ output_model = gr.Model3D(
57
+ label="Output Model",
58
+ interactive=False,
 
 
 
59
  )
60
+ output_model2 = gr.Model3D(
61
+ label="Output Model",
62
+ interactive=False,
63
+ )
64
+ with gr.Tab("obj"):
65
+ output_model = gr.Model3D(
66
+ label="Output Model",
67
+ interactive=False,
68
+ )
69
+ with gr.Tab("glb"):
70
+ output_model2 = gr.Model3D(
71
+ label="Output Model",
72
+ interactive=False,
73
+ )
74
+ #...
75
+
76
+ with gr.Row():
77
+ output_model = gr.Model3D(
78
+ label="Output Model",
79
+ interactive=False,
80
+ )
81
+ output_model2 = gr.Model3D(
82
+ label="Output Model",
83
+ interactive=False,
84
+ )
85
+ )
86
 
87
+ with gr.Column():
88
+ gr.Markdown("**Input Image**")
89
+ input_image = gr.Image(
90
+ label="",
91
+ image_mode="RGBA",
92
+ sources="upload",
93
+ type="pil",
94
+ elem_id="content_image",
95
+ width=500, # Увеличена ширина входных изображений
96
+ )
97
+ with gr.Column():
98
+ do_remove_background = gr.Checkbox(
99
+ label="Remove Background",
100
+ value=True,
101
+ elem_id="remove_background",
102
  )
103
+ foreground_ratio = gr.Slider(
104
+ label="Foreground Ratio",
105
+ minimum=0.5,
106
+ maximum=1.0,
107
+ value=0.85,
108
+ step=0.05,
109
+ elem_id="foreground_ratio",
110
  )
111
+ with gr.Row():
112
+ submit = gr.Button(
113
+ label="Generate",
114
+ scale=0,
115
+ variant="primary",
116
+ elem_classes="generate-button",
117
+ )
118
+ output_model = gr.Model3D(
119
+ label="Output Model",
120
+ interactive=False,
121
+ )
122
+ output_model2 = gr.Model3D(
123
+ label="Output Model",
124
+ interactive=False,
125
+ )
126
+ with gr.Tab("obj"):
127
+ output_model = gr.Model3D(
128
+ label="Output Model",
129
+ interactive=False,
130
+ )
131
+ output_model2 = gr.Model3D(
132
+ label="Output Model",
133
+ interactive=False,
134
+ )
135
+ with gr.Tab("glb"):
136
+ output_model2 = gr.Model3D(
137
+ label="Output Model",
138
+ interactive=False,
139
+ )
140
 
141
+ with gr.Column():
142
+ gr.Markdown("**Output Models**")
143
+ output_model = gr.Model3D(label="Output Model", interactive=False)
144
+ output_model2 = gr.Model3D(label="Output Model", interactive=False)
145
+
146
+ with gr.Row():
147
+ #...
148
+
149
+ with gr.Button("Download Example", elem_id="download_example", fn=partial(run_example, inputs=[input_image], outputs=[input_image, output_model]):
150
+ #...
151
+
152
+ with gr.Column():
153
+ gr.Examples(
154
+ examples=[
155
+ "examples/1.png",
156
+ "examples/2.png",
157
+ ],
158
+ inputs=[input_image],
159
+ outputs=[output_model, output_model2],
160
+ fn=partial(run_example),
161
+ label="Examples",
162
+ examples_per_page=20,
163
+ )
164
+ submit.click(
165
+ fn=partial(run_example),
166
+ inputs=[input_image],
167
+ outputs=[output_model, output_model2],
168
+ )
169
+
170
+ with gr.Button("Generate", elem_id="generate", variant="primary", fn=generate, inputs=[output_model, output_model2]):
171
+ #...
172
+
173
+ with gr.Row():
174
+ output_model = gr.Model3D(
175
+ label="Output Model",
176
+ interactive=False,
177
+ )
178
+ output_model2 = gr.Model3D(
179
+ label="Output Model",
180
+ interactive=False,
181
+ )
182
  )
183
 
184
+ with gr.Column():
185
+ gr.Markdown("**Input Image**")
186
+ input_image = gr.Image(
187
+ label="",
188
+ image_mode="RGBA",
189
+ sources="upload",
190
+ type="pil",
191
+ elem_id="content_image",
192
+ width=500, # Увеличена ширина входных изображений
193
+ )
194
+ with gr.Column():
195
+ do_remove_background = gr.Checkbox(
196
+ label="Remove Background",
197
+ value=True,
198
+ elem_id="remove_background",
199
+ )
200
+ foreground_ratio = gr.Slider(
201
+ label="Foreground Ratio",
202
+ minimum=0.5,
203
+ maximum=1.0,
204
+ value=0.85,
205
+ step=0.05,
206
+ elem_id="foreground_ratio",
207
+ )
208
+ with gr.Row():
209
+ submit = gr.Button(
210
+ label="Generate",
211
+ scale=0,
212
+ variant="primary",
213
+ elem_classes="generate-button",
214
+ )
215
+ output_model = gr.Model3D(
216
+ label="Output Model",
217
+ interactive=False,
218
+ )
219
+ output_model2