Spaces:
Paused
Paused
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -10,7 +10,7 @@ import gradio as gr
|
|
| 10 |
import numpy as np
|
| 11 |
from PIL import Image
|
| 12 |
from huggingface_hub import snapshot_download, login
|
| 13 |
-
import tempfile
|
| 14 |
from pathlib import Path
|
| 15 |
|
| 16 |
if os.environ.get("HF_TOKEN"):
|
|
@@ -34,13 +34,12 @@ def _pip(*a):
|
|
| 34 |
print(f" pip OK: {tag}")
|
| 35 |
else:
|
| 36 |
print(f" pip FAIL: {tag}")
|
| 37 |
-
print(f"
|
| 38 |
return ok
|
| 39 |
|
| 40 |
print("=== Runtime installs ===")
|
| 41 |
_pip("open3d>=0.18.0")
|
| 42 |
-
|
| 43 |
-
_pip("-v", "utils3d")
|
| 44 |
_pip("iopath")
|
| 45 |
_pip("--no-deps", "sam2>=1.1.0")
|
| 46 |
_pip("--no-deps", "git+https://github.com/microsoft/MoGe.git@a8c37341bc0325ca99b9d57981cc3bb2bd3e255b")
|
|
@@ -51,13 +50,8 @@ for idx in ["https://docs.gsplat.studio/whl/pt210cu128",
|
|
| 51 |
if _pip("--no-deps", f"--extra-index-url={idx}", "gsplat"):
|
| 52 |
break
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
| 56 |
-
try:
|
| 57 |
-
__import__(mod)
|
| 58 |
-
print(f" import {mod}: OK")
|
| 59 |
-
except ImportError as e:
|
| 60 |
-
print(f" import {mod}: FAIL - {e}")
|
| 61 |
|
| 62 |
# --- Clone sam-3d-objects ---
|
| 63 |
SAM3D_PATH = Path("/home/user/app/sam-3d-objects")
|
|
@@ -89,27 +83,38 @@ if hf_ckpt.exists() and not local_ckpt.exists():
|
|
| 89 |
local_ckpt.symlink_to(hf_ckpt)
|
| 90 |
CONFIG_PATH = str(local_ckpt / "pipeline.yaml")
|
| 91 |
print(f"Config exists: {Path(CONFIG_PATH).exists()}")
|
| 92 |
-
print("=== Startup complete ===")
|
| 93 |
|
| 94 |
# --- Endpoints ---
|
| 95 |
|
| 96 |
@spaces.GPU(duration=60)
|
| 97 |
def diagnose():
|
|
|
|
| 98 |
import torch
|
| 99 |
lines = [f"torch={torch.__version__}", f"cuda={torch.cuda.is_available()}"]
|
| 100 |
if torch.cuda.is_available():
|
| 101 |
lines.append(f"gpu={torch.cuda.get_device_name()}")
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
try:
|
| 104 |
m = __import__(mod)
|
| 105 |
lines.append(f"{mod}: OK ({getattr(m, '__version__', '-')})")
|
| 106 |
except Exception as e:
|
| 107 |
lines.append(f"{mod}: FAIL - {e}")
|
|
|
|
| 108 |
try:
|
| 109 |
from sam2.automatic_mask_generator import SAM2AutomaticMaskGenerator
|
| 110 |
lines.append("sam2: OK")
|
| 111 |
except Exception as e:
|
| 112 |
lines.append(f"sam2: FAIL - {e}")
|
|
|
|
| 113 |
try:
|
| 114 |
from inference import Inference
|
| 115 |
lines.append("SAM3D Inference: importable")
|
|
@@ -140,7 +145,7 @@ def reconstruct_objects(image: np.ndarray):
|
|
| 140 |
|
| 141 |
preview = image_np.copy()
|
| 142 |
preview[best_mask] = (preview[best_mask] * 0.5 + np.array([0, 255, 0]) * 0.5).astype(np.uint8)
|
| 143 |
-
print(f" {len(masks)} masks
|
| 144 |
|
| 145 |
from inference import Inference
|
| 146 |
sam3d = Inference(CONFIG_PATH, compile=False)
|
|
@@ -183,9 +188,6 @@ def reconstruct_objects(image: np.ndarray):
|
|
| 183 |
m = result["mesh"]
|
| 184 |
if hasattr(m, "export"):
|
| 185 |
m.export(glb)
|
| 186 |
-
else:
|
| 187 |
-
import open3d as o3d
|
| 188 |
-
o3d.io.write_triangle_mesh(glb, m)
|
| 189 |
else:
|
| 190 |
keys = list(result.keys()) if isinstance(result, dict) else dir(result)
|
| 191 |
return None, preview, f"Cannot extract 3D. Keys: {keys}"
|
|
|
|
| 10 |
import numpy as np
|
| 11 |
from PIL import Image
|
| 12 |
from huggingface_hub import snapshot_download, login
|
| 13 |
+
import tempfile
|
| 14 |
from pathlib import Path
|
| 15 |
|
| 16 |
if os.environ.get("HF_TOKEN"):
|
|
|
|
| 34 |
print(f" pip OK: {tag}")
|
| 35 |
else:
|
| 36 |
print(f" pip FAIL: {tag}")
|
| 37 |
+
print(f" {r.stderr[-300:]}")
|
| 38 |
return ok
|
| 39 |
|
| 40 |
print("=== Runtime installs ===")
|
| 41 |
_pip("open3d>=0.18.0")
|
| 42 |
+
_pip("utils3d")
|
|
|
|
| 43 |
_pip("iopath")
|
| 44 |
_pip("--no-deps", "sam2>=1.1.0")
|
| 45 |
_pip("--no-deps", "git+https://github.com/microsoft/MoGe.git@a8c37341bc0325ca99b9d57981cc3bb2bd3e255b")
|
|
|
|
| 50 |
if _pip("--no-deps", f"--extra-index-url={idx}", "gsplat"):
|
| 51 |
break
|
| 52 |
|
| 53 |
+
# DO NOT import CUDA-dependent packages here - only in GPU functions!
|
| 54 |
+
# Importing gsplat/open3d at startup can init CUDA and break ZeroGPU.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
# --- Clone sam-3d-objects ---
|
| 57 |
SAM3D_PATH = Path("/home/user/app/sam-3d-objects")
|
|
|
|
| 83 |
local_ckpt.symlink_to(hf_ckpt)
|
| 84 |
CONFIG_PATH = str(local_ckpt / "pipeline.yaml")
|
| 85 |
print(f"Config exists: {Path(CONFIG_PATH).exists()}")
|
| 86 |
+
print("=== Startup complete (NO CUDA imports at startup) ===")
|
| 87 |
|
| 88 |
# --- Endpoints ---
|
| 89 |
|
| 90 |
@spaces.GPU(duration=60)
|
| 91 |
def diagnose():
|
| 92 |
+
"""Test GPU and modules in isolated manner."""
|
| 93 |
import torch
|
| 94 |
lines = [f"torch={torch.__version__}", f"cuda={torch.cuda.is_available()}"]
|
| 95 |
if torch.cuda.is_available():
|
| 96 |
lines.append(f"gpu={torch.cuda.get_device_name()}")
|
| 97 |
+
# Only test pure-python / safe imports
|
| 98 |
+
for mod in ["kaolin", "utils3d", "iopath", "pytorch3d"]:
|
| 99 |
+
try:
|
| 100 |
+
m = __import__(mod)
|
| 101 |
+
lines.append(f"{mod}: OK ({getattr(m, '__version__', '-')})")
|
| 102 |
+
except Exception as e:
|
| 103 |
+
lines.append(f"{mod}: FAIL - {e}")
|
| 104 |
+
# Test CUDA modules one at a time
|
| 105 |
+
for mod in ["open3d", "gsplat", "moge"]:
|
| 106 |
try:
|
| 107 |
m = __import__(mod)
|
| 108 |
lines.append(f"{mod}: OK ({getattr(m, '__version__', '-')})")
|
| 109 |
except Exception as e:
|
| 110 |
lines.append(f"{mod}: FAIL - {e}")
|
| 111 |
+
# sam2
|
| 112 |
try:
|
| 113 |
from sam2.automatic_mask_generator import SAM2AutomaticMaskGenerator
|
| 114 |
lines.append("sam2: OK")
|
| 115 |
except Exception as e:
|
| 116 |
lines.append(f"sam2: FAIL - {e}")
|
| 117 |
+
# SAM3D inference
|
| 118 |
try:
|
| 119 |
from inference import Inference
|
| 120 |
lines.append("SAM3D Inference: importable")
|
|
|
|
| 145 |
|
| 146 |
preview = image_np.copy()
|
| 147 |
preview[best_mask] = (preview[best_mask] * 0.5 + np.array([0, 255, 0]) * 0.5).astype(np.uint8)
|
| 148 |
+
print(f" {len(masks)} masks ({time.time()-t0:.0f}s)")
|
| 149 |
|
| 150 |
from inference import Inference
|
| 151 |
sam3d = Inference(CONFIG_PATH, compile=False)
|
|
|
|
| 188 |
m = result["mesh"]
|
| 189 |
if hasattr(m, "export"):
|
| 190 |
m.export(glb)
|
|
|
|
|
|
|
|
|
|
| 191 |
else:
|
| 192 |
keys = list(result.keys()) if isinstance(result, dict) else dir(result)
|
| 193 |
return None, preview, f"Cannot extract 3D. Keys: {keys}"
|