Spaces:
Running
on
Zero
Running
on
Zero
File size: 10,443 Bytes
521472f 7518724 521472f 7518724 521472f 7518724 521472f 76d9c6d |
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 |
import spaces
import os
import warnings
from huggingface_hub import snapshot_download
import gradio as gr
from glob import glob
import shutil
import torch
import numpy as np
from PIL import Image
from einops import rearrange
import argparse
# Suppress warnings
warnings.simplefilter('ignore', category=UserWarning)
warnings.simplefilter('ignore', category=FutureWarning)
warnings.simplefilter('ignore', category=DeprecationWarning)
def download_models():
# Create weights directory if it doesn't exist
os.makedirs("weights", exist_ok=True)
os.makedirs("weights/hunyuanDiT", exist_ok=True)
# Download Hunyuan3D-1 model
try:
snapshot_download(
repo_id="tencent/Hunyuan3D-1",
local_dir="./weights",
resume_download=True
)
print("Successfully downloaded Hunyuan3D-1 model")
except Exception as e:
print(f"Error downloading Hunyuan3D-1: {e}")
# Download HunyuanDiT model
try:
snapshot_download(
repo_id="Tencent-Hunyuan/HunyuanDiT-v1.1-Diffusers-Distilled",
local_dir="./weights/hunyuanDiT",
resume_download=True
)
print("Successfully downloaded HunyuanDiT model")
except Exception as e:
print(f"Error downloading HunyuanDiT: {e}")
# Download models before starting the app
download_models()
# Parse arguments
parser = argparse.ArgumentParser()
parser.add_argument("--use_lite", default=False, action="store_true")
parser.add_argument("--mv23d_cfg_path", default="./svrm/configs/svrm.yaml", type=str)
parser.add_argument("--mv23d_ckt_path", default="weights/svrm/svrm.safetensors", type=str)
parser.add_argument("--text2image_path", default="weights/hunyuanDiT", type=str)
parser.add_argument("--save_memory", default=False, action="store_true")
parser.add_argument("--device", default="cuda:0", type=str)
args = parser.parse_args()
# Constants
CONST_PORT = 8080
CONST_MAX_QUEUE = 1
CONST_SERVER = '0.0.0.0'
CONST_HEADER = '''
<h2><b>Official 🤗 Gradio Demo</b></h2>
<h2><a href='https://github.com/tencent/Hunyuan3D-1' target='_blank'>
<b>Hunyuan3D-1.0: A Unified Framework for Text-to-3D and Image-to-3D Generation</b></a></h2>
'''
# Helper functions
def get_example_img_list():
print('Loading example img list ...')
return sorted(glob('./demos/example_*.png'))
def get_example_txt_list():
print('Loading example txt list ...')
txt_list = []
for line in open('./demos/example_list.txt'):
txt_list.append(line.strip())
return txt_list
example_is = get_example_img_list()
example_ts = get_example_txt_list()
# Import required workers
from infer import seed_everything, save_gif
from infer import Text2Image, Removebg, Image2Views, Views2Mesh, GifRenderer
# Initialize workers
worker_xbg = Removebg()
print(f"loading {args.text2image_path}")
worker_t2i = Text2Image(
pretrain=args.text2image_path,
device=args.device,
save_memory=args.save_memory
)
worker_i2v = Image2Views(
use_lite=args.use_lite,
device=args.device
)
worker_v23 = Views2Mesh(
args.mv23d_cfg_path,
args.mv23d_ckt_path,
use_lite=args.use_lite,
device=args.device
)
worker_gif = GifRenderer(args.device)
# Pipeline stages
@spaces.GPU
def stage_0_t2i(text, image, seed, step):
os.makedirs('./outputs/app_output', exist_ok=True)
exists = set(int(_) for _ in os.listdir('./outputs/app_output') if not _.startswith("."))
cur_id = min(set(range(30)) - exists) if len(exists) < 30 else 0
if os.path.exists(f"./outputs/app_output/{(cur_id + 1) % 30}"):
shutil.rmtree(f"./outputs/app_output/{(cur_id + 1) % 30}")
save_folder = f'./outputs/app_output/{cur_id}'
os.makedirs(save_folder, exist_ok=True)
dst = save_folder + '/img.png'
if not text:
if image is None:
return dst, save_folder
image.save(dst)
return dst, save_folder
image = worker_t2i(text, seed, step)
image.save(dst)
dst = worker_xbg(image, save_folder)
return dst, save_folder
@spaces.GPU
def stage_1_xbg(image, save_folder):
if isinstance(image, str):
image = Image.open(image)
dst = save_folder + '/img_nobg.png'
rgba = worker_xbg(image)
rgba.save(dst)
return dst
@spaces.GPU
def stage_2_i2v(image, seed, step, save_folder):
if isinstance(image, str):
image = Image.open(image)
gif_dst = save_folder + '/views.gif'
res_img, pils = worker_i2v(image, seed, step)
save_gif(pils, gif_dst)
views_img, cond_img = res_img[0], res_img[1]
img_array = np.asarray(views_img, dtype=np.uint8)
show_img = rearrange(img_array, '(n h) (m w) c -> (n m) h w c', n=3, m=2)
show_img = show_img[worker_i2v.order, ...]
show_img = rearrange(show_img, '(n m) h w c -> (n h) (m w) c', n=2, m=3)
show_img = Image.fromarray(show_img)
return views_img, cond_img, show_img
@spaces.GPU
def stage_3_v23(views_pil, cond_pil, seed, save_folder, target_face_count=30000,
do_texture_mapping=True, do_render=True):
do_texture_mapping = do_texture_mapping or do_render
obj_dst = save_folder + '/mesh_with_colors.obj'
glb_dst = save_folder + '/mesh.glb'
worker_v23(
views_pil,
cond_pil,
seed=seed,
save_folder=save_folder,
target_face_count=target_face_count,
do_texture_mapping=do_texture_mapping
)
return obj_dst, glb_dst
@spaces.GPU
def stage_4_gif(obj_dst, save_folder, do_render_gif=True):
if not do_render_gif:
return None
gif_dst = save_folder + '/output.gif'
worker_gif(
save_folder + '/mesh.obj',
gif_dst_path=gif_dst
)
return gif_dst
# Gradio Interface
with gr.Blocks() as demo:
gr.Markdown(CONST_HEADER)
with gr.Row(variant="panel"):
with gr.Column(scale=2):
with gr.Tab("Text to 3D"):
with gr.Column():
text = gr.TextArea('一只黑白相间的熊猫在白色背景上居中坐着,呈现出卡通风格和可爱氛围。',
lines=1, max_lines=10, label='Input text')
with gr.Row():
textgen_seed = gr.Number(value=0, label="T2I seed", precision=0)
textgen_step = gr.Number(value=25, label="T2I step", precision=0)
textgen_SEED = gr.Number(value=0, label="Gen seed", precision=0)
textgen_STEP = gr.Number(value=50, label="Gen step", precision=0)
textgen_max_faces = gr.Number(value=90000, label="max number of faces", precision=0)
with gr.Row():
textgen_do_texture_mapping = gr.Checkbox(label="texture mapping", value=False)
textgen_do_render_gif = gr.Checkbox(label="Render gif", value=False)
textgen_submit = gr.Button("Generate", variant="primary")
gr.Examples(examples=example_ts, inputs=[text], label="Txt examples")
with gr.Tab("Image to 3D"):
with gr.Column():
input_image = gr.Image(label="Input image", width=256, height=256,
type="pil", image_mode="RGBA", sources="upload")
with gr.Row():
imggen_SEED = gr.Number(value=0, label="Gen seed", precision=0)
imggen_STEP = gr.Number(value=50, label="Gen step", precision=0)
imggen_max_faces = gr.Number(value=90000, label="max number of faces", precision=0)
with gr.Row():
imggen_do_texture_mapping = gr.Checkbox(label="texture mapping", value=False)
imggen_do_render_gif = gr.Checkbox(label="Render gif", value=False)
imggen_submit = gr.Button("Generate", variant="primary")
gr.Examples(examples=example_is, inputs=[input_image], label="Img examples")
with gr.Column(scale=3):
with gr.Tab("rembg image"):
rem_bg_image = gr.Image(label="No background image", width=256, height=256,
type="pil", image_mode="RGBA")
with gr.Tab("Multi views"):
result_image = gr.Image(label="Multi views", type="pil")
with gr.Tab("Obj"):
result_3dobj = gr.Model3D(label="Output obj")
with gr.Tab("Glb"):
result_3dglb = gr.Model3D(label="Output glb")
with gr.Tab("GIF"):
result_gif = gr.Image(label="Rendered GIF")
# States
none = gr.State(None)
save_folder = gr.State()
cond_image = gr.State()
views_image = gr.State()
text_image = gr.State()
# Event handlers
textgen_submit.click(
fn=stage_0_t2i,
inputs=[text, none, textgen_seed, textgen_step],
outputs=[rem_bg_image, save_folder],
).success(
fn=stage_2_i2v,
inputs=[rem_bg_image, textgen_SEED, textgen_STEP, save_folder],
outputs=[views_image, cond_image, result_image],
).success(
fn=stage_3_v23,
inputs=[views_image, cond_image, textgen_SEED, save_folder, textgen_max_faces,
textgen_do_texture_mapping, textgen_do_render_gif],
outputs=[result_3dobj, result_3dglb],
).success(
fn=stage_4_gif,
inputs=[result_3dglb, save_folder, textgen_do_render_gif],
outputs=[result_gif],
)
imggen_submit.click(
fn=stage_0_t2i,
inputs=[none, input_image, textgen_seed, textgen_step],
outputs=[text_image, save_folder],
).success(
fn=stage_1_xbg,
inputs=[text_image, save_folder],
outputs=[rem_bg_image],
).success(
fn=stage_2_i2v,
inputs=[rem_bg_image, imggen_SEED, imggen_STEP, save_folder],
outputs=[views_image, cond_image, result_image],
).success(
fn=stage_3_v23,
inputs=[views_image, cond_image, imggen_SEED, save_folder, imggen_max_faces,
imggen_do_texture_mapping, imggen_do_render_gif],
outputs=[result_3dobj, result_3dglb],
).success(
fn=stage_4_gif,
inputs=[result_3dglb, save_folder, imggen_do_render_gif],
outputs=[result_gif],
)
demo.queue()
demo.launch() |