dev-bjoern commited on
Commit
40b3556
·
1 Parent(s): 4334222

Defer torch/bpy imports to GPU context

Browse files
Files changed (1) hide show
  1. app.py +12 -11
app.py CHANGED
@@ -12,8 +12,6 @@ from pathlib import Path
12
  import gradio as gr
13
  import numpy as np
14
  import spaces
15
- import torch
16
- import bpy
17
  from huggingface_hub import snapshot_download, login
18
 
19
  # Login with HF_TOKEN if available
@@ -42,20 +40,21 @@ FACES = None
42
  def load_model():
43
  """Load SAM 3D Body model"""
44
  global MODEL, FACES
45
-
46
  if MODEL is not None:
47
  return MODEL, FACES
48
-
 
49
  print("Loading SAM 3D Body model...")
50
-
51
  # Download checkpoint
52
  checkpoint_dir = snapshot_download(
53
  repo_id="facebook/sam-3d-body-dinov3",
54
  token=os.environ.get("HF_TOKEN")
55
  )
56
-
57
  from sam_3d_body import load_sam_3d_body, SAM3DBodyEstimator
58
-
59
  device = "cuda" if torch.cuda.is_available() else "cpu"
60
 
61
  model, model_cfg = load_sam_3d_body(
@@ -74,21 +73,23 @@ def load_model():
74
  return MODEL, FACES
75
 
76
 
77
- @spaces.GPU(duration=60)
78
  def reconstruct_body(image: np.ndarray) -> tuple:
79
  """
80
  Reconstruct 3D body mesh from image.
81
-
82
  Args:
83
  image: Input RGB image
84
-
85
  Returns:
86
  tuple: (glb_path, status)
87
  """
88
  if image is None:
89
  return None, "❌ No image provided"
90
-
91
  try:
 
 
92
  estimator, faces = load_model()
93
 
94
  # Process image
 
12
  import gradio as gr
13
  import numpy as np
14
  import spaces
 
 
15
  from huggingface_hub import snapshot_download, login
16
 
17
  # Login with HF_TOKEN if available
 
40
  def load_model():
41
  """Load SAM 3D Body model"""
42
  global MODEL, FACES
43
+
44
  if MODEL is not None:
45
  return MODEL, FACES
46
+
47
+ import torch
48
  print("Loading SAM 3D Body model...")
49
+
50
  # Download checkpoint
51
  checkpoint_dir = snapshot_download(
52
  repo_id="facebook/sam-3d-body-dinov3",
53
  token=os.environ.get("HF_TOKEN")
54
  )
55
+
56
  from sam_3d_body import load_sam_3d_body, SAM3DBodyEstimator
57
+
58
  device = "cuda" if torch.cuda.is_available() else "cpu"
59
 
60
  model, model_cfg = load_sam_3d_body(
 
73
  return MODEL, FACES
74
 
75
 
76
+ @spaces.GPU(duration=120)
77
  def reconstruct_body(image: np.ndarray) -> tuple:
78
  """
79
  Reconstruct 3D body mesh from image.
80
+
81
  Args:
82
  image: Input RGB image
83
+
84
  Returns:
85
  tuple: (glb_path, status)
86
  """
87
  if image is None:
88
  return None, "❌ No image provided"
89
+
90
  try:
91
+ import torch
92
+ import bpy
93
  estimator, faces = load_model()
94
 
95
  # Process image