Spaces:
Running
on
Zero
Running
on
Zero
linhaotong
commited on
Commit
Β·
ace4129
1
Parent(s):
e59f7b7
update
Browse files
app.py
CHANGED
|
@@ -68,9 +68,10 @@ if __name__ == "__main__":
|
|
| 68 |
print(f"π Workspace Directory: {workspace_dir}")
|
| 69 |
print(f"πΌοΈ Gallery Directory: {gallery_dir}")
|
| 70 |
|
|
|
|
|
|
|
| 71 |
app.launch(
|
| 72 |
host="0.0.0.0", # Required for Spaces
|
| 73 |
port=7860, # Standard Gradio port
|
| 74 |
-
share=False
|
| 75 |
-
debug=False
|
| 76 |
)
|
|
|
|
| 68 |
print(f"π Workspace Directory: {workspace_dir}")
|
| 69 |
print(f"πΌοΈ Gallery Directory: {gallery_dir}")
|
| 70 |
|
| 71 |
+
# Launch with minimal, Spaces-compatible configuration
|
| 72 |
+
# Some parameters may cause routing issues, so we use minimal config
|
| 73 |
app.launch(
|
| 74 |
host="0.0.0.0", # Required for Spaces
|
| 75 |
port=7860, # Standard Gradio port
|
| 76 |
+
share=False # Not needed on Spaces
|
|
|
|
| 77 |
)
|
depth_anything_3/app/gradio_app.py
CHANGED
|
@@ -619,8 +619,14 @@ class DepthAnything3App:
|
|
| 619 |
**kwargs: Additional arguments for demo.launch()
|
| 620 |
"""
|
| 621 |
demo = self.create_app()
|
|
|
|
|
|
|
| 622 |
demo.queue(max_size=20).launch(
|
| 623 |
-
show_error=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 624 |
)
|
| 625 |
|
| 626 |
|
|
|
|
| 619 |
**kwargs: Additional arguments for demo.launch()
|
| 620 |
"""
|
| 621 |
demo = self.create_app()
|
| 622 |
+
# Configure launch settings for Spaces compatibility
|
| 623 |
+
# Use minimal config to avoid routing issues
|
| 624 |
demo.queue(max_size=20).launch(
|
| 625 |
+
show_error=True,
|
| 626 |
+
ssr_mode=False,
|
| 627 |
+
server_name=host,
|
| 628 |
+
server_port=port,
|
| 629 |
+
**kwargs
|
| 630 |
)
|
| 631 |
|
| 632 |
|
depth_anything_3/app/modules/model_inference.py
CHANGED
|
@@ -54,32 +54,42 @@ class ModelInference:
|
|
| 54 |
"""
|
| 55 |
Initialize the DepthAnything3 model using global cache.
|
| 56 |
|
|
|
|
|
|
|
|
|
|
| 57 |
This uses a global variable which is safe because @spaces.GPU
|
| 58 |
runs in isolated subprocess, each with its own global namespace.
|
| 59 |
|
| 60 |
Args:
|
| 61 |
-
device: Device to
|
| 62 |
|
| 63 |
Returns:
|
| 64 |
-
Model instance ready for inference
|
| 65 |
"""
|
| 66 |
global _MODEL_CACHE
|
| 67 |
|
| 68 |
if _MODEL_CACHE is None:
|
| 69 |
# First time loading in this subprocess
|
|
|
|
| 70 |
model_dir = os.environ.get(
|
| 71 |
"DA3_MODEL_DIR", "depth-anything/DA3NESTED-GIANT-LARGE"
|
| 72 |
)
|
| 73 |
-
print(f"π Loading model from {model_dir}...")
|
|
|
|
| 74 |
_MODEL_CACHE = DepthAnything3.from_pretrained(model_dir)
|
| 75 |
-
|
|
|
|
| 76 |
_MODEL_CACHE.eval()
|
| 77 |
-
print("β
Model loaded
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
_MODEL_CACHE = _MODEL_CACHE.to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
return _MODEL_CACHE
|
| 85 |
|
|
|
|
| 54 |
"""
|
| 55 |
Initialize the DepthAnything3 model using global cache.
|
| 56 |
|
| 57 |
+
Optimization: Load model to CPU first, then move to GPU when needed.
|
| 58 |
+
This is faster than reloading from disk each time.
|
| 59 |
+
|
| 60 |
This uses a global variable which is safe because @spaces.GPU
|
| 61 |
runs in isolated subprocess, each with its own global namespace.
|
| 62 |
|
| 63 |
Args:
|
| 64 |
+
device: Device to run inference on (will move model to this device)
|
| 65 |
|
| 66 |
Returns:
|
| 67 |
+
Model instance ready for inference on specified device
|
| 68 |
"""
|
| 69 |
global _MODEL_CACHE
|
| 70 |
|
| 71 |
if _MODEL_CACHE is None:
|
| 72 |
# First time loading in this subprocess
|
| 73 |
+
# Load to CPU first (faster than loading directly to GPU)
|
| 74 |
model_dir = os.environ.get(
|
| 75 |
"DA3_MODEL_DIR", "depth-anything/DA3NESTED-GIANT-LARGE"
|
| 76 |
)
|
| 77 |
+
print(f"π Loading model from {model_dir} to CPU...")
|
| 78 |
+
print(" (Model files are cached on disk)")
|
| 79 |
_MODEL_CACHE = DepthAnything3.from_pretrained(model_dir)
|
| 80 |
+
# Load to CPU first (faster, and allows reuse)
|
| 81 |
+
_MODEL_CACHE = _MODEL_CACHE.to("cpu")
|
| 82 |
_MODEL_CACHE.eval()
|
| 83 |
+
print("β
Model loaded to CPU memory (cached in subprocess)")
|
| 84 |
+
|
| 85 |
+
# Move to target device for inference
|
| 86 |
+
if device != "cpu" and next(_MODEL_CACHE.parameters()).device.type != device:
|
| 87 |
+
print(f"π Moving model from {next(_MODEL_CACHE.parameters()).device} to {device}...")
|
| 88 |
_MODEL_CACHE = _MODEL_CACHE.to(device)
|
| 89 |
+
print(f"β
Model ready on {device}")
|
| 90 |
+
elif device == "cpu":
|
| 91 |
+
# Already on CPU or requested CPU
|
| 92 |
+
pass
|
| 93 |
|
| 94 |
return _MODEL_CACHE
|
| 95 |
|