Spaces:
Build error
Build error
Vo Minh Vu
commited on
Commit
·
9077d48
1
Parent(s):
f299b62
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,58 @@
|
|
| 1 |
-
import io
|
| 2 |
import os
|
| 3 |
-
import shlex
|
| 4 |
-
import subprocess
|
| 5 |
-
import tempfile
|
| 6 |
-
import zipfile
|
| 7 |
-
from functools import partial
|
| 8 |
import sys
|
| 9 |
-
|
| 10 |
-
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
|
| 11 |
-
from fastapi.responses import StreamingResponse
|
| 12 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 13 |
-
|
| 14 |
-
import numpy as np
|
| 15 |
-
import rembg
|
| 16 |
-
import torch
|
| 17 |
-
from PIL import Image
|
| 18 |
-
|
| 19 |
-
from tsr.system import TSR
|
| 20 |
-
from tsr.utils import remove_background, resize_foreground, to_gradio_3d_orientation
|
| 21 |
|
| 22 |
# ----------------------------------------------------------------
|
| 23 |
# 0. Bootstrap install of torchmcubes wheel if needed
|
| 24 |
# ----------------------------------------------------------------
|
| 25 |
|
| 26 |
-
#
|
| 27 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 28 |
WHEELS_DIR = os.path.join(BASE_DIR, "wheels")
|
| 29 |
WHL_NAME = "torchmcubes-0.1.0-cp310-cp310-linux_x86_64.whl"
|
| 30 |
WHL_PATH = os.path.join(WHEELS_DIR, WHL_NAME)
|
| 31 |
|
| 32 |
-
def
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
try:
|
| 37 |
subprocess.check_call([
|
| 38 |
sys.executable, "-m", "pip", "install",
|
| 39 |
-
"--upgrade",
|
| 40 |
])
|
| 41 |
except subprocess.CalledProcessError as e:
|
| 42 |
print(f"[bootstrap][ERROR] pip install failed: {e}")
|
| 43 |
sys.exit(1)
|
| 44 |
|
| 45 |
-
# Try
|
| 46 |
try:
|
| 47 |
import torchmcubes
|
| 48 |
except ImportError:
|
| 49 |
-
|
| 50 |
import torchmcubes
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
# ------------------------------------------------------------
|
| 53 |
# 1. Model & utils initialization (runs at startup)
|
| 54 |
# ------------------------------------------------------------
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import sys
|
| 3 |
+
import subprocess
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# ----------------------------------------------------------------
|
| 6 |
# 0. Bootstrap install of torchmcubes wheel if needed
|
| 7 |
# ----------------------------------------------------------------
|
| 8 |
|
| 9 |
+
# 1) Locate the wheel relative to this file
|
| 10 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 11 |
WHEELS_DIR = os.path.join(BASE_DIR, "wheels")
|
| 12 |
WHL_NAME = "torchmcubes-0.1.0-cp310-cp310-linux_x86_64.whl"
|
| 13 |
WHL_PATH = os.path.join(WHEELS_DIR, WHL_NAME)
|
| 14 |
|
| 15 |
+
def install_torchmcubes_wheel(whl_path: str):
|
| 16 |
+
"""
|
| 17 |
+
Installs the given wheel via pip. Exits on failure.
|
| 18 |
+
"""
|
| 19 |
+
if not os.path.exists(whl_path):
|
| 20 |
+
raise FileNotFoundError(f"Cannot find torchmcubes wheel at {whl_path!r}")
|
| 21 |
+
print(f"[bootstrap] Installing torchmcubes from {whl_path!r} …")
|
| 22 |
try:
|
| 23 |
subprocess.check_call([
|
| 24 |
sys.executable, "-m", "pip", "install",
|
| 25 |
+
"--upgrade", whl_path
|
| 26 |
])
|
| 27 |
except subprocess.CalledProcessError as e:
|
| 28 |
print(f"[bootstrap][ERROR] pip install failed: {e}")
|
| 29 |
sys.exit(1)
|
| 30 |
|
| 31 |
+
# 2) Try import, install if missing
|
| 32 |
try:
|
| 33 |
import torchmcubes
|
| 34 |
except ImportError:
|
| 35 |
+
install_torchmcubes_wheel(WHL_PATH)
|
| 36 |
import torchmcubes
|
| 37 |
|
| 38 |
+
# ----------------------------------------------------------------
|
| 39 |
+
# 1. Now safe to import the rest of your dependencies
|
| 40 |
+
# ----------------------------------------------------------------
|
| 41 |
+
import io
|
| 42 |
+
import zipfile
|
| 43 |
+
import tempfile
|
| 44 |
+
import numpy as np
|
| 45 |
+
import rembg
|
| 46 |
+
import torch
|
| 47 |
+
from PIL import Image
|
| 48 |
+
|
| 49 |
+
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
|
| 50 |
+
from fastapi.responses import StreamingResponse
|
| 51 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 52 |
+
|
| 53 |
+
from tsr.system import TSR
|
| 54 |
+
from tsr.utils import remove_background, resize_foreground, to_gradio_3d_orientation
|
| 55 |
+
|
| 56 |
# ------------------------------------------------------------
|
| 57 |
# 1. Model & utils initialization (runs at startup)
|
| 58 |
# ------------------------------------------------------------
|