-
Notifications
You must be signed in to change notification settings - Fork 1
/
gradio_app.py
executable file
·401 lines (358 loc) · 14 KB
/
gradio_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import logging
import os
import tempfile
import time
import gradio as gr
import numpy as np
import rembg
import torch
from PIL import Image
from functools import partial
from tsr.system import TSR
from tsr.utils import remove_background, resize_foreground, to_gradio_3d_orientation
from diffusers import ControlNetModel, StableDiffusionControlNetPipeline, DiffusionPipeline
from diffusion_webui.diffusion_models.base_controlnet_pipeline import (
ControlnetPipeline,
)
from diffusion_webui.utils.model_list import (
controlnet_model_list,
stable_model_list,
)
from diffusion_webui.utils.preprocces_utils import PREPROCCES_DICT
from diffusion_webui.utils.scheduler_list import (
SCHEDULER_MAPPING,
get_scheduler,
)
if torch.cuda.is_available():
device = "cuda:0"
else:
device = "cpu"
import argparse
if torch.cuda.is_available():
device = "cuda:0"
else:
device = "cpu"
model = TSR.from_pretrained(
"stabilityai/TripoSR",
config_name="config.yaml",
weight_name="model.ckpt",
)
# adjust the chunk size to balance between speed and memory usage
model.renderer.set_chunk_size(8192)
model.to(device)
rembg_session = rembg.new_session()
def check_input_image(input_image):
if input_image is None:
raise gr.Error("No image uploaded!")
def preprocess(input_image, do_remove_background, foreground_ratio):
def fill_background(image):
image = np.array(image).astype(np.float32) / 255.0
image = image[:, :, :3] * image[:, :, 3:4] + (1 - image[:, :, 3:4]) * 0.5
image = Image.fromarray((image * 255.0).astype(np.uint8))
return image
if do_remove_background:
image = input_image.convert("RGB")
image = remove_background(image, rembg_session)
image = resize_foreground(image, foreground_ratio)
image = fill_background(image)
else:
image = input_image
if image.mode == "RGBA":
image = fill_background(image)
return image
def generate(image, mc_resolution, formats=["obj", "glb"]):
scene_codes = model(image, device=device)
mesh = model.extract_mesh(scene_codes, resolution=mc_resolution)[0]
mesh = to_gradio_3d_orientation(mesh)
rv = []
for format in formats:
mesh_path = tempfile.NamedTemporaryFile(suffix=f".{format}", delete=False)
mesh.export(mesh_path.name)
rv.append(mesh_path.name)
return rv[0]
class StableDiffusionControlNetGenerator(ControlnetPipeline):
def __init__(self):
self.pipe = None
def load_model(self, stable_model_path, controlnet_model_path, scheduler):
if self.pipe is None:
controlnet = ControlNetModel.from_pretrained(
controlnet_model_path, torch_dtype=torch.float16, cache_dir="./model/"
)
self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
pretrained_model_name_or_path=stable_model_path,
controlnet=controlnet,
safety_checker=None,
torch_dtype=torch.float16,
cache_dir="./model/"
)
self.pipe = get_scheduler(pipe=self.pipe, scheduler=scheduler)
self.pipe.to("cuda")
self.pipe.enable_xformers_memory_efficient_attention()
return self.pipe
def controlnet_preprocces(
self,
read_image: str,
preprocces_type: str,
):
processed_image = PREPROCCES_DICT[preprocces_type](read_image)
return processed_image
def generate_image(
self,
image_path: str,
stable_model_path: str,
controlnet_model_path: str,
height: int,
width: int,
guess_mode: bool,
controlnet_conditioning_scale: int,
prompt: str,
negative_prompt: str,
num_images_per_prompt: int,
guidance_scale: int,
num_inference_step: int,
scheduler: str,
seed_generator: int,
preprocces_type: str = "Lineart",
):
print("stable model: ", stable_model_path)
print("controlnet model: ", controlnet_model_path)
pipe = self.load_model(
stable_model_path=stable_model_path,
controlnet_model_path=controlnet_model_path,
scheduler=scheduler,
)
read_image = image_path
controlnet_image = self.controlnet_preprocces(
read_image=read_image, preprocces_type=preprocces_type
)
if seed_generator == 0:
random_seed = torch.randint(0, 1000000, (1,))
generator = torch.manual_seed(random_seed)
else:
generator = torch.manual_seed(seed_generator)
output = pipe(
prompt=prompt,
height=height,
width=width,
controlnet_conditioning_scale=float(controlnet_conditioning_scale),
guess_mode=guess_mode,
image=controlnet_image,
negative_prompt=negative_prompt,
num_images_per_prompt=num_images_per_prompt,
num_inference_steps=num_inference_step,
guidance_scale=guidance_scale,
generator=generator,
).images
return output[0]
def get_sketch(im):
return im["composite"]
with gr.Blocks(title="scratch to 3D") as interface:
# gr.Markdown(
# """
# # TripoSR Demo
# [TripoSR](https://github.com/VAST-AI-Research/TripoSR) is a state-of-the-art open-source model for **fast** feedforward 3D reconstruction from a single image, collaboratively developed by [Tripo AI](https://www.tripo3d.ai/) and [Stability AI](https://stability.ai/).
#
# **Tips:**
# 1. If you find the result is unsatisfied, please try to change the foreground ratio. It might improve the results.
# 2. It's better to disable "Remove Background" for the provided examples (except fot the last one) since they have been already preprocessed.
# 3. Otherwise, please disable "Remove Background" option only if your input image is RGBA with transparent background, image contents are centered and occupy more than 70% of image width or height.
# """
# )
with gr.Row(variant="panel"):
with gr.Column():
controlnet_image_path = gr.ImageEditor(
sources="upload",
label="Input Image",
type="pil",
# height=700,
)
with gr.Column():
controlnet_prompt = gr.Textbox(
lines=1, placeholder="Prompt", show_label=False
)
controlnet_negative_prompt = gr.Textbox(
lines=1, placeholder="Negative Prompt", show_label=False
)
with gr.Row(visible=False):
with gr.Column():
controlnet_stable_model_path = gr.Dropdown(
choices=stable_model_list,
value=stable_model_list[0],
label="Stable Model Path",
visible=True,
interactive=True
)
controlnet_preprocces_type = gr.Dropdown(
choices=list(PREPROCCES_DICT.keys()),
value="Lineart",
label="Preprocess Type",
visible=False,
interactive=True
)
controlnet_conditioning_scale = gr.Slider(
minimum=0.0,
maximum=1.0,
step=0.1,
value=1.0,
visible=True,
label="ControlNet Conditioning Scale",
interactive=True
)
controlnet_guidance_scale = gr.Slider(
minimum=0.1,
maximum=15,
step=0.1,
value=7.5,
visible=True,
label="Guidance Scale",
interactive=True
)
controlnet_width = gr.Slider(
minimum=128,
maximum=1280,
step=32,
value=512,
visible=False,
label="Width",
interactive=True
)
with gr.Column():
controlnet_model_path = gr.Dropdown(
choices=controlnet_model_list,
value="lllyasviel/control_v11p_sd15_lineart",
label="ControlNet Model Path",
visible=True,
interactive=True
)
controlnet_scheduler = gr.Dropdown(
choices=list(SCHEDULER_MAPPING.keys()),
value=list(SCHEDULER_MAPPING.keys())[0],
label="Scheduler",
visible=True,
interactive=True
)
controlnet_num_inference_step = gr.Slider(
minimum=1,
maximum=150,
step=1,
value=30,
label="Num Inference Step",
visible=True,
interactive=True
)
controlnet_num_images_per_prompt = gr.Slider(
minimum=1,
maximum=4,
step=1,
value=1,
label="Number Of Images",
visible=False,
interactive=True
)
controlnet_seed_generator = gr.Slider(
minimum=0,
maximum=1000000,
step=1,
value=0,
label="Seed(0 for random)",
visible=False,
interactive=True
)
controlnet_guess_mode = gr.Checkbox(
label="Guess Mode",
value=True,
visible=False,
interactive=True
)
controlnet_height = gr.Slider(
minimum=128,
maximum=1280,
step=32,
value=512,
label="Height",
visible=False,
interactive=True
)
with gr.Row():
with gr.Column():
with gr.Row():
input_image = gr.Image(
label="2D Image",
image_mode="RGBA",
type="pil",
elem_id="content_image",
height=400,
interactive=False,
)
processed_image = gr.Image(
label="Processed Image",
interactive=False,
height=400,
visible=False
)
with gr.Row():
generate_2d = gr.Button("Generate 2D", elem_id="generate", variant="primary")
with gr.Column():
with gr.Row():
output_model_obj = gr.Model3D(
label="Output Model (OBJ Format)",
interactive=False,
height=400,
)
with gr.Row():
generate_3d = gr.Button("Generate 3D", elem_id="generate", variant="primary")
with gr.Row(visible=False):
with gr.Group():
do_remove_background = gr.Checkbox(
label="Remove Background", value=True
)
foreground_ratio = gr.Slider(
label="Foreground Ratio",
minimum=0.5,
maximum=1.0,
value=0.85,
step=0.05,
)
mc_resolution = gr.Slider(
label="Marching Cubes Resolution",
minimum=32,
maximum=320,
value=256,
step=32
)
generate_2d.click(
fn=get_sketch,
inputs=[controlnet_image_path],
outputs=[input_image]
).success(
fn=StableDiffusionControlNetGenerator().generate_image,
inputs=[
input_image,
controlnet_stable_model_path,
controlnet_model_path,
controlnet_height,
controlnet_width,
controlnet_guess_mode,
controlnet_conditioning_scale,
controlnet_prompt,
controlnet_negative_prompt,
controlnet_num_images_per_prompt,
controlnet_guidance_scale,
controlnet_num_inference_step,
controlnet_scheduler,
controlnet_seed_generator,
controlnet_preprocces_type,
],
outputs=[input_image],
).success(
fn=preprocess,
inputs=[input_image, do_remove_background, foreground_ratio],
outputs=[processed_image],
)
generate_3d.click(fn=check_input_image, inputs=[processed_image]).success(
fn=generate,
inputs=[processed_image, mc_resolution],
outputs=[output_model_obj],
)
if __name__ == '__main__':
interface.launch(server_name='0.0.0.0', share=False)