akhaliq HF Staff commited on
Commit
dfe4904
·
verified ·
1 Parent(s): ad07c70

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -52
app.py CHANGED
@@ -8,10 +8,34 @@ REPO_URL = "https://github.com/facebookresearch/sam-3d-objects.git"
8
  REPO_DIR = "/home/user/app/sam-3d-objects"
9
 
10
  # ============ Install Dependencies & Setup ============
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  def install_dependencies():
12
  """
13
- Installs dependencies using the official repo method (pip install -e .[extras])
14
- instead of manual package listing.
15
  """
16
  print("Starting installation sequence...")
17
 
@@ -20,33 +44,35 @@ def install_dependencies():
20
  print(f"Cloning repository to {REPO_DIR}...")
21
  subprocess.run(["git", "clone", REPO_URL, REPO_DIR], check=True)
22
 
23
- # Switch working directory to repo for local installs
24
  os.chdir(REPO_DIR)
25
 
26
- # 2. Set Environment Variables for PIP
27
- # As per instructions: export PIP_EXTRA_INDEX_URL and PIP_FIND_LINKS
 
 
28
  env = os.environ.copy()
29
  env["PIP_EXTRA_INDEX_URL"] = "https://pypi.ngc.nvidia.com https://download.pytorch.org/whl/cu121"
30
  env["PIP_FIND_LINKS"] = "https://nvidia-kaolin.s3.us-east-2.amazonaws.com/torch-2.5.1_cu121.html"
31
 
32
- # Upgrade pip first
33
  subprocess.run([sys.executable, "-m", "pip", "install", "--upgrade", "pip"], env=env, check=True)
34
 
35
- # 3. Install Dependencies via setup.py extras
36
- # Step A: Install [dev]
 
37
  print("Installing [dev] dependencies...")
38
  subprocess.run([sys.executable, "-m", "pip", "install", "-e", ".[dev]"], env=env, check=True)
39
 
40
- # Step B: Install [p3d] - The 2-step approach mentioned in instructions
41
  print("Installing [p3d] dependencies...")
42
  subprocess.run([sys.executable, "-m", "pip", "install", "-e", ".[p3d]"], env=env, check=True)
43
 
44
- # Step C: Install [inference]
45
  print("Installing [inference] dependencies...")
46
  subprocess.run([sys.executable, "-m", "pip", "install", "-e", ".[inference]"], env=env, check=True)
47
 
48
- # 4. Apply Patches
49
- # Run ./patching/hydra
50
  patch_script = os.path.join(REPO_DIR, "patching", "hydra")
51
  if os.path.exists(patch_script):
52
  print("Applying Hydra patch...")
@@ -65,7 +91,6 @@ if REPO_DIR not in sys.path:
65
  # Set environment variables required for runtime
66
  os.environ["CUDA_HOME"] = "/usr/local/cuda"
67
  os.environ["LIDRA_SKIP_INIT"] = "true"
68
- # Often required to prevent Pytorch3D checks in certain container environments
69
  os.environ["PYTORCH3D_NO_CUDA_CHECK"] = "1"
70
 
71
  # ============ Imports ============
@@ -81,7 +106,7 @@ import math
81
  from omegaconf import OmegaConf, DictConfig, ListConfig
82
  from hydra.utils import instantiate, get_method
83
 
84
- # Lazy imports placehoder
85
  _sam3d_imported = False
86
  _pipeline = None
87
 
@@ -195,44 +220,6 @@ def run_inference(image: np.ndarray, mask: np.ndarray, config_file: str, seed: O
195
  pointmap=pointmap,
196
  )
197
 
198
- # ============ Rendering Helpers ============
199
- # (Retained from original script logic for rendering frames)
200
- def _yaw_pitch_r_fov_to_extrinsics_intrinsics(yaws, pitchs, rs, fovs):
201
- lazy_import_sam3d()
202
- is_list = isinstance(yaws, list)
203
- if not is_list:
204
- yaws, pitchs = [yaws], [pitchs]
205
- if not isinstance(rs, list):
206
- rs = [rs] * len(yaws)
207
- if not isinstance(fovs, list):
208
- fovs = [fovs] * len(yaws)
209
- extrinsics, intrinsics = [], []
210
- for yaw, pitch, r, fov in zip(yaws, pitchs, rs, fovs):
211
- fov_t = torch.deg2rad(torch.tensor(float(fov))).cuda()
212
- yaw_t = torch.tensor(float(yaw)).cuda()
213
- pitch_t = torch.tensor(float(pitch)).cuda()
214
- orig = torch.tensor([
215
- torch.sin(yaw_t) * torch.cos(pitch_t),
216
- torch.sin(pitch_t),
217
- torch.cos(yaw_t) * torch.cos(pitch_t),
218
- ]).cuda() * r
219
- extr = utils3d.torch.extrinsics_look_at(
220
- orig, torch.tensor([0, 0, 0]).float().cuda(), torch.tensor([0, 1, 0]).float().cuda())
221
- intr = utils3d.torch.intrinsics_from_fov_xy(fov_t, fov_t)
222
- extrinsics.append(extr)
223
- intrinsics.append(intr)
224
- if not is_list:
225
- return extrinsics[0], intrinsics[0]
226
- return extrinsics, intrinsics
227
-
228
- @spaces.GPU(duration=60)
229
- def render_video_gpu(sample, resolution=512, bg_color=(0,0,0), num_frames=300, r=2.0, fov=40, pitch_deg=0, yaw_start_deg=-90, **kwargs):
230
- lazy_import_sam3d()
231
- yaws = (torch.linspace(0, 2*torch.pi, num_frames) + math.radians(yaw_start_deg)).tolist()
232
- pitch = [math.radians(pitch_deg)] * num_frames
233
- extr, intr = _yaw_pitch_r_fov_to_extrinsics_intrinsics(yaws, pitch, r, fov)
234
- return render_utils.render_frames(sample, extr, intr, {"resolution": resolution, "bg_color": bg_color, "backend": "gsplat"}, **kwargs)
235
-
236
  # ============ Gradio Interface ============
237
  CONFIG_FILE = os.path.join(REPO_DIR, "configs/inference.yaml")
238
 
 
8
  REPO_DIR = "/home/user/app/sam-3d-objects"
9
 
10
  # ============ Install Dependencies & Setup ============
11
+ def patch_pyproject_toml():
12
+ """
13
+ Removes 'bpy==4.3.0' from pyproject.toml to prevent installation failures.
14
+ This package is often unavailable on PyPI and usually not needed for inference.
15
+ """
16
+ print("Patching pyproject.toml to remove strict bpy dependency...")
17
+ pyproject_path = os.path.join(REPO_DIR, "pyproject.toml")
18
+
19
+ if os.path.exists(pyproject_path):
20
+ with open(pyproject_path, "r") as f:
21
+ content = f.read()
22
+
23
+ # Remove dependency entries for bpy==4.3.0
24
+ # We try multiple formats to ensure we catch it (with/without quotes/commas)
25
+ new_content = content.replace('"bpy==4.3.0",', '')
26
+ new_content = new_content.replace("'bpy==4.3.0',", '')
27
+ new_content = new_content.replace('"bpy==4.3.0"', '')
28
+ new_content = new_content.replace("'bpy==4.3.0'", '')
29
+
30
+ with open(pyproject_path, "w") as f:
31
+ f.write(new_content)
32
+ print("Patch applied successfully.")
33
+ else:
34
+ print(f"Warning: {pyproject_path} not found. Skipping patch.")
35
+
36
  def install_dependencies():
37
  """
38
+ Installs dependencies using the official repo method (pip install -e .[extras]).
 
39
  """
40
  print("Starting installation sequence...")
41
 
 
44
  print(f"Cloning repository to {REPO_DIR}...")
45
  subprocess.run(["git", "clone", REPO_URL, REPO_DIR], check=True)
46
 
47
+ # Switch working directory to repo
48
  os.chdir(REPO_DIR)
49
 
50
+ # 2. Patch the faulty dependency BEFORE installing
51
+ patch_pyproject_toml()
52
+
53
+ # 3. Set Environment Variables for PIP
54
  env = os.environ.copy()
55
  env["PIP_EXTRA_INDEX_URL"] = "https://pypi.ngc.nvidia.com https://download.pytorch.org/whl/cu121"
56
  env["PIP_FIND_LINKS"] = "https://nvidia-kaolin.s3.us-east-2.amazonaws.com/torch-2.5.1_cu121.html"
57
 
58
+ # Upgrade pip
59
  subprocess.run([sys.executable, "-m", "pip", "install", "--upgrade", "pip"], env=env, check=True)
60
 
61
+ # 4. Install Dependencies via setup.py extras
62
+
63
+ # Install [dev] (Modified by patch to exclude bpy)
64
  print("Installing [dev] dependencies...")
65
  subprocess.run([sys.executable, "-m", "pip", "install", "-e", ".[dev]"], env=env, check=True)
66
 
67
+ # Install [p3d]
68
  print("Installing [p3d] dependencies...")
69
  subprocess.run([sys.executable, "-m", "pip", "install", "-e", ".[p3d]"], env=env, check=True)
70
 
71
+ # Install [inference]
72
  print("Installing [inference] dependencies...")
73
  subprocess.run([sys.executable, "-m", "pip", "install", "-e", ".[inference]"], env=env, check=True)
74
 
75
+ # 5. Apply Hydra Patch
 
76
  patch_script = os.path.join(REPO_DIR, "patching", "hydra")
77
  if os.path.exists(patch_script):
78
  print("Applying Hydra patch...")
 
91
  # Set environment variables required for runtime
92
  os.environ["CUDA_HOME"] = "/usr/local/cuda"
93
  os.environ["LIDRA_SKIP_INIT"] = "true"
 
94
  os.environ["PYTORCH3D_NO_CUDA_CHECK"] = "1"
95
 
96
  # ============ Imports ============
 
106
  from omegaconf import OmegaConf, DictConfig, ListConfig
107
  from hydra.utils import instantiate, get_method
108
 
109
+ # Lazy imports placeholder
110
  _sam3d_imported = False
111
  _pipeline = None
112
 
 
220
  pointmap=pointmap,
221
  )
222
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
223
  # ============ Gradio Interface ============
224
  CONFIG_FILE = os.path.join(REPO_DIR, "configs/inference.yaml")
225