File size: 2,988 Bytes
4845d25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e59f7b7
 
 
4845d25
 
 
 
 
 
 
e59f7b7
 
 
 
4845d25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ace4129
 
4845d25
 
 
ace4129
4845d25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Copyright (c) 2025 ByteDance Ltd. and/or its affiliates
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Hugging Face Spaces App for Depth Anything 3.

This app uses the @spaces.GPU decorator to dynamically allocate GPU resources
for model inference on Hugging Face Spaces.
"""

import os
import spaces
from depth_anything_3.app.gradio_app import DepthAnything3App
from depth_anything_3.app.modules.model_inference import ModelInference

# Apply @spaces.GPU decorator to run_inference method
# This ensures GPU operations happen in isolated subprocess
# Model loading and inference will occur in GPU subprocess, not main process
original_run_inference = ModelInference.run_inference

@spaces.GPU(duration=120)  # Request GPU for up to 120 seconds per inference
def gpu_run_inference(self, *args, **kwargs):
    """
    GPU-accelerated inference with Spaces decorator.
    
    This function runs in a GPU subprocess where:
    - Model is loaded and moved to GPU (safe)
    - CUDA operations are allowed
    - All CUDA tensors are moved to CPU before return (for pickle safety)
    """
    return original_run_inference(self, *args, **kwargs)

# Replace the original method with the GPU-decorated version
ModelInference.run_inference = gpu_run_inference

# Initialize and launch the app
if __name__ == "__main__":
    # Configure directories for Hugging Face Spaces
    model_dir = os.environ.get("DA3_MODEL_DIR", "depth-anything/DA3NESTED-GIANT-LARGE")
    workspace_dir = os.environ.get("DA3_WORKSPACE_DIR", "workspace/gradio")
    gallery_dir = os.environ.get("DA3_GALLERY_DIR", "workspace/gallery")
    
    # Create directories if they don't exist
    os.makedirs(workspace_dir, exist_ok=True)
    os.makedirs(gallery_dir, exist_ok=True)
    
    # Initialize the app
    app = DepthAnything3App(
        model_dir=model_dir,
        workspace_dir=workspace_dir,
        gallery_dir=gallery_dir
    )
    
    # Launch with Spaces-friendly settings
    print("🚀 Launching Depth Anything 3 on Hugging Face Spaces...")
    print(f"📦 Model Directory: {model_dir}")
    print(f"📁 Workspace Directory: {workspace_dir}")
    print(f"🖼️  Gallery Directory: {gallery_dir}")
    
    # Launch with minimal, Spaces-compatible configuration
    # Some parameters may cause routing issues, so we use minimal config
    app.launch(
        host="0.0.0.0",  # Required for Spaces
        port=7860,       # Standard Gradio port
        share=False      # Not needed on Spaces
    )