Jbowyer commited on
Commit
bac5874
·
verified ·
1 Parent(s): 3a1ba6e

Update gradio_app.py

Browse files
Files changed (1) hide show
  1. gradio_app.py +39 -78
gradio_app.py CHANGED
@@ -18,8 +18,6 @@ import sys
18
  sys.path.insert(0, './hy3dshape')
19
  sys.path.insert(0, './hy3dpaint')
20
 
21
- pythonpath = sys.executable
22
- print(pythonpath)
23
 
24
  try:
25
  from torchvision_fix import apply_fix
@@ -52,7 +50,7 @@ from hy3dpaint.convert_utils import create_glb_with_pbr_materials
52
 
53
 
54
  MAX_SEED = 1e7
55
- ENV = "Huggingface" # "Huggingface"
56
  if ENV == 'Huggingface':
57
  """
58
  Setup environment for running on Huggingface platform.
@@ -67,65 +65,11 @@ if ENV == 'Huggingface':
67
  with the specified directory structure.
68
  """
69
  import os, spaces, subprocess, sys, shlex
70
- from spaces import zero
71
-
72
- def install_cuda_toolkit():
73
- # CUDA_TOOLKIT_URL = "https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda_11.8.0_520.61.05_linux.run"
74
- CUDA_TOOLKIT_URL = "https://developer.download.nvidia.com/compute/cuda/12.2.0/local_installers/cuda_12.2.0_535.54.03_linux.run"
75
- CUDA_TOOLKIT_FILE = "/tmp/%s" % os.path.basename(CUDA_TOOLKIT_URL)
76
- subprocess.call(["wget", "-q", CUDA_TOOLKIT_URL, "-O", CUDA_TOOLKIT_FILE])
77
- subprocess.call(["chmod", "+x", CUDA_TOOLKIT_FILE])
78
- subprocess.call([CUDA_TOOLKIT_FILE, "--silent", "--toolkit"])
79
-
80
- os.environ["CUDA_HOME"] = "/usr/local/cuda"
81
- os.environ["PATH"] = "%s/bin:%s" % (os.environ["CUDA_HOME"], os.environ["PATH"])
82
- os.environ["LD_LIBRARY_PATH"] = "%s/lib:%s" % (
83
- os.environ["CUDA_HOME"],
84
- "" if "LD_LIBRARY_PATH" not in os.environ else os.environ["LD_LIBRARY_PATH"],
85
- )
86
- # Fix: arch_list[-1] += '+PTX'; IndexError: list index out of range
87
- os.environ["TORCH_CUDA_ARCH_LIST"] = "8.0;8.6"
88
-
89
- def prepare_env():
90
- # print('install custom')
91
- # os.system(f"cd /home/user/app/hy3dpaint/custom_rasterizer && {pythonpath} -m pip install -e .")
92
- # os.system(f"cd /home/user/app/hy3dpaint/packages/custom_rasterizer && pip install -e .")
93
- subprocess.run(shlex.split("pip install custom_rasterizer-0.1-cp310-cp310-linux_x86_64.whl"), check=True)
94
-
95
- print("cd /home/user/app/hy3dpaint/differentiable_renderer/ && bash compile_mesh_painter.sh")
96
- os.system("cd /home/user/app/hy3dpaint/DifferentiableRenderer && bash compile_mesh_painter.sh")
97
- # print("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth -P /home/user/app/hy3dpaint/ckpt")
98
- # os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth -P /home/user/app/hy3dpaint/ckpt")
99
-
100
- def check():
101
- import custom_rasterizer
102
- print(type(custom_rasterizer))
103
- print(dir(custom_rasterizer))
104
- print(getattr(custom_rasterizer, '__file__', None))
105
-
106
- package_dir = None
107
- if hasattr(custom_rasterizer, '__file__') and custom_rasterizer.__file__:
108
- package_dir = os.path.dirname(custom_rasterizer.__file__)
109
- elif hasattr(custom_rasterizer, '__path__'):
110
- package_dir = list(custom_rasterizer.__path__)[0]
111
- else:
112
- raise RuntimeError("Cannot determine package path")
113
- print(package_dir)
114
-
115
- for root, dirs, files in os.walk(package_dir):
116
- level = root.replace(package_dir, '').count(os.sep)
117
- indent = ' ' * 4 * level
118
- print(f"{indent}{os.path.basename(root)}/")
119
- subindent = ' ' * 4 * (level + 1)
120
- for f in files:
121
- print(f"{subindent}{f}")
122
-
123
- # print(torch.__version__)
124
- # install_cuda_toolkit()
125
- print(torch.__version__)
126
- prepare_env()
127
- check()
128
-
129
  else:
130
  """
131
  Define a dummy `spaces` module with a GPU decorator class for local environment.
@@ -378,7 +322,7 @@ def _gen_shape(
378
  main_image = image if not MV_MODE else image['front']
379
  return mesh, main_image, save_folder, stats, seed
380
 
381
- @spaces.GPU(duration=130)
382
  def generation_all(
383
  caption=None,
384
  image=None,
@@ -395,6 +339,8 @@ def generation_all(
395
  randomize_seed: bool = False,
396
  ):
397
  start_time_0 = time.time()
 
 
398
  mesh, image, save_folder, stats, seed = _gen_shape(
399
  caption,
400
  image,
@@ -410,8 +356,12 @@ def generation_all(
410
  num_chunks=num_chunks,
411
  randomize_seed=randomize_seed,
412
  )
 
 
413
  path = export_mesh(mesh, save_folder, textured=False)
414
 
 
 
415
 
416
  print(path)
417
  print('='*40)
@@ -423,10 +373,18 @@ def generation_all(
423
  # stats['time']['postprocessing'] = time.time() - tmp_time
424
 
425
  tmp_time = time.time()
 
 
426
  mesh = face_reduce_worker(mesh)
 
 
427
 
428
  # path = export_mesh(mesh, save_folder, textured=False, type='glb')
 
 
429
  path = export_mesh(mesh, save_folder, textured=False, type='obj') # 这样操作也会 core dump
 
 
430
 
431
  logger.info("---Face Reduction takes %s seconds ---" % (time.time() - tmp_time))
432
  stats['time']['face reduction'] = time.time() - tmp_time
@@ -434,7 +392,11 @@ def generation_all(
434
  tmp_time = time.time()
435
 
436
  text_path = os.path.join(save_folder, f'textured_mesh.obj')
 
 
437
  path_textured = tex_pipeline(mesh_path=path, image_path=image, output_mesh_path=text_path, save_glb=False)
 
 
438
 
439
  logger.info("---Texture Generation takes %s seconds ---" % (time.time() - tmp_time))
440
  stats['time']['texture generation'] = time.time() - tmp_time
@@ -442,7 +404,11 @@ def generation_all(
442
  tmp_time = time.time()
443
  # Convert textured OBJ to GLB using obj2gltf with PBR support
444
  glb_path_textured = os.path.join(save_folder, 'textured_mesh.glb')
 
 
445
  conversion_success = quick_convert_with_obj2gltf(path_textured, glb_path_textured)
 
 
446
 
447
  logger.info("---Convert textured OBJ to GLB takes %s seconds ---" % (time.time() - tmp_time))
448
  stats['time']['convert textured OBJ to GLB'] = time.time() - tmp_time
@@ -797,7 +763,7 @@ if __name__ == '__main__':
797
  parser.add_argument('--host', type=str, default='0.0.0.0')
798
  parser.add_argument('--device', type=str, default='cuda')
799
  parser.add_argument('--mc_algo', type=str, default='mc')
800
- parser.add_argument('--cache-path', type=str, default='/root/save_dir')
801
  parser.add_argument('--enable_t23d', action='store_true')
802
  parser.add_argument('--disable_tex', action='store_true')
803
  parser.add_argument('--enable_flashvdm', action='store_true')
@@ -869,24 +835,26 @@ if __name__ == '__main__':
869
  HAS_TEXTUREGEN = True
870
 
871
  except Exception as e:
 
 
872
  print(f"Error loading texture generator: {e}")
873
  print("Failed to load texture generator.")
874
  print('Please try to install requirements by following README.md')
875
  HAS_TEXTUREGEN = False
876
 
877
- # HAS_T2I = True
878
- # if args.enable_t23d:
879
- # from hy3dgen.text2image import HunyuanDiTPipeline
880
 
881
- # t2i_worker = HunyuanDiTPipeline('Tencent-Hunyuan/HunyuanDiT-v1.1-Diffusers-Distilled')
882
- # HAS_T2I = True
883
 
884
  from hy3dshape import FaceReducer, FloaterRemover, DegenerateFaceRemover, MeshSimplifier, \
885
  Hunyuan3DDiTFlowMatchingPipeline
886
  from hy3dshape.pipelines import export_to_trimesh
887
- from hy3dshape.rembg import BackgroundRemover
888
 
889
- rmbg_worker = BackgroundRemover()
890
  i23d_worker = Hunyuan3DDiTFlowMatchingPipeline.from_pretrained(
891
  args.model_path,
892
  subfolder=args.subfolder,
@@ -915,13 +883,6 @@ if __name__ == '__main__':
915
 
916
  if args.low_vram_mode:
917
  torch.cuda.empty_cache()
918
-
919
  demo = build_app()
920
  app = gr.mount_gradio_app(app, demo, path="/")
921
-
922
- if ENV == 'Huggingface':
923
- # for Zerogpu
924
- from spaces import zero
925
- zero.startup()
926
-
927
  uvicorn.run(app, host=args.host, port=args.port)
 
18
  sys.path.insert(0, './hy3dshape')
19
  sys.path.insert(0, './hy3dpaint')
20
 
 
 
21
 
22
  try:
23
  from torchvision_fix import apply_fix
 
50
 
51
 
52
  MAX_SEED = 1e7
53
+ ENV = "Local" # "Huggingface"
54
  if ENV == 'Huggingface':
55
  """
56
  Setup environment for running on Huggingface platform.
 
65
  with the specified directory structure.
66
  """
67
  import os, spaces, subprocess, sys, shlex
68
+ print("cd /home/user/app/hy3dgen/texgen/differentiable_renderer/ && bash compile_mesh_painter.sh")
69
+ os.system("cd /home/user/app/hy3dgen/texgen/differentiable_renderer/ && bash compile_mesh_painter.sh")
70
+ print('install custom')
71
+ subprocess.run(shlex.split("pip install custom_rasterizer-0.1-cp310-cp310-linux_x86_64.whl"),
72
+ check=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  else:
74
  """
75
  Define a dummy `spaces` module with a GPU decorator class for local environment.
 
322
  main_image = image if not MV_MODE else image['front']
323
  return mesh, main_image, save_folder, stats, seed
324
 
325
+ @spaces.GPU(duration=60)
326
  def generation_all(
327
  caption=None,
328
  image=None,
 
339
  randomize_seed: bool = False,
340
  ):
341
  start_time_0 = time.time()
342
+ print("DEBUG: Starting generation_all...")
343
+ sys.stdout.flush()
344
  mesh, image, save_folder, stats, seed = _gen_shape(
345
  caption,
346
  image,
 
356
  num_chunks=num_chunks,
357
  randomize_seed=randomize_seed,
358
  )
359
+ print("DEBUG: _gen_shape finished.")
360
+ sys.stdout.flush()
361
  path = export_mesh(mesh, save_folder, textured=False)
362
 
363
+ print("DEBUG: Initial export_mesh finished.")
364
+ sys.stdout.flush()
365
 
366
  print(path)
367
  print('='*40)
 
373
  # stats['time']['postprocessing'] = time.time() - tmp_time
374
 
375
  tmp_time = time.time()
376
+ print("DEBUG: Starting face_reduce_worker...")
377
+ sys.stdout.flush()
378
  mesh = face_reduce_worker(mesh)
379
+ print("DEBUG: face_reduce_worker finished.")
380
+ sys.stdout.flush()
381
 
382
  # path = export_mesh(mesh, save_folder, textured=False, type='glb')
383
+ print("DEBUG: Starting second export_mesh (obj)...")
384
+ sys.stdout.flush()
385
  path = export_mesh(mesh, save_folder, textured=False, type='obj') # 这样操作也会 core dump
386
+ print("DEBUG: Second export_mesh (obj) finished.")
387
+ sys.stdout.flush()
388
 
389
  logger.info("---Face Reduction takes %s seconds ---" % (time.time() - tmp_time))
390
  stats['time']['face reduction'] = time.time() - tmp_time
 
392
  tmp_time = time.time()
393
 
394
  text_path = os.path.join(save_folder, f'textured_mesh.obj')
395
+ print("DEBUG: Starting tex_pipeline...")
396
+ sys.stdout.flush()
397
  path_textured = tex_pipeline(mesh_path=path, image_path=image, output_mesh_path=text_path, save_glb=False)
398
+ print("DEBUG: tex_pipeline finished.")
399
+ sys.stdout.flush()
400
 
401
  logger.info("---Texture Generation takes %s seconds ---" % (time.time() - tmp_time))
402
  stats['time']['texture generation'] = time.time() - tmp_time
 
404
  tmp_time = time.time()
405
  # Convert textured OBJ to GLB using obj2gltf with PBR support
406
  glb_path_textured = os.path.join(save_folder, 'textured_mesh.glb')
407
+ print("DEBUG: Starting quick_convert_with_obj2gltf...")
408
+ sys.stdout.flush()
409
  conversion_success = quick_convert_with_obj2gltf(path_textured, glb_path_textured)
410
+ print("DEBUG: quick_convert_with_obj2gltf finished.")
411
+ sys.stdout.flush()
412
 
413
  logger.info("---Convert textured OBJ to GLB takes %s seconds ---" % (time.time() - tmp_time))
414
  stats['time']['convert textured OBJ to GLB'] = time.time() - tmp_time
 
763
  parser.add_argument('--host', type=str, default='0.0.0.0')
764
  parser.add_argument('--device', type=str, default='cuda')
765
  parser.add_argument('--mc_algo', type=str, default='mc')
766
+ parser.add_argument('--cache-path', type=str, default='./save_dir')
767
  parser.add_argument('--enable_t23d', action='store_true')
768
  parser.add_argument('--disable_tex', action='store_true')
769
  parser.add_argument('--enable_flashvdm', action='store_true')
 
835
  HAS_TEXTUREGEN = True
836
 
837
  except Exception as e:
838
+ import traceback
839
+ traceback.print_exc()
840
  print(f"Error loading texture generator: {e}")
841
  print("Failed to load texture generator.")
842
  print('Please try to install requirements by following README.md')
843
  HAS_TEXTUREGEN = False
844
 
845
+ HAS_T2I = True
846
+ if args.enable_t23d:
847
+ from hy3dgen.text2image import HunyuanDiTPipeline
848
 
849
+ t2i_worker = HunyuanDiTPipeline('Tencent-Hunyuan/HunyuanDiT-v1.1-Diffusers-Distilled')
850
+ HAS_T2I = True
851
 
852
  from hy3dshape import FaceReducer, FloaterRemover, DegenerateFaceRemover, MeshSimplifier, \
853
  Hunyuan3DDiTFlowMatchingPipeline
854
  from hy3dshape.pipelines import export_to_trimesh
855
+ from hy3dshape.birefnet_rembg import BiRefNetRemover
856
 
857
+ rmbg_worker = BiRefNetRemover()
858
  i23d_worker = Hunyuan3DDiTFlowMatchingPipeline.from_pretrained(
859
  args.model_path,
860
  subfolder=args.subfolder,
 
883
 
884
  if args.low_vram_mode:
885
  torch.cuda.empty_cache()
 
886
  demo = build_app()
887
  app = gr.mount_gradio_app(app, demo, path="/")
 
 
 
 
 
 
888
  uvicorn.run(app, host=args.host, port=args.port)