ArturoNereu commited on
Commit
6fbfc6a
·
1 Parent(s): 90d9dd9

Changed the way we serve static files on HF Spaces

Browse files
Files changed (2) hide show
  1. app.py +12 -2
  2. backend/storage.py +2 -1
app.py CHANGED
@@ -9,8 +9,14 @@ import threading
9
  import uvicorn
10
  import time
11
  import requests
 
12
  from backend.main import app as fastapi_app
13
 
 
 
 
 
 
14
  # Get base URLs from environment
15
  # Auto-detect HF Spaces environment
16
  FASTAPI_INTERNAL = "http://localhost:8000" # Always use localhost for internal calls
@@ -112,8 +118,12 @@ def get_viewer_html(scene_id="welcome"):
112
  return '<div style="color: red;">Scene not found</div>'
113
 
114
  # Add the static assets base URL for model loading
115
- # On local dev: http://localhost:8000, on HF Spaces: empty (relative)
116
- scene_data["static_base_url"] = FASTAPI_INTERNAL if not IS_HF_SPACES else ""
 
 
 
 
117
 
118
  # Read the viewer HTML template
119
  viewer_path = os.path.join(os.path.dirname(__file__), "frontend", "game_viewer.html")
 
9
  import uvicorn
10
  import time
11
  import requests
12
+ from pathlib import Path
13
  from backend.main import app as fastapi_app
14
 
15
+ # Set up static paths for Gradio to serve model files
16
+ # This makes files accessible at /gradio_api/file=<path>
17
+ MODELS_DIR = Path(__file__).parent / "models"
18
+ gr.set_static_paths(paths=[str(MODELS_DIR)])
19
+
20
  # Get base URLs from environment
21
  # Auto-detect HF Spaces environment
22
  FASTAPI_INTERNAL = "http://localhost:8000" # Always use localhost for internal calls
 
118
  return '<div style="color: red;">Scene not found</div>'
119
 
120
  # Add the static assets base URL for model loading
121
+ # On local dev: FastAPI serves at /static/models/
122
+ # On HF Spaces: Gradio serves via set_static_paths at /file/models/
123
+ if IS_HF_SPACES:
124
+ scene_data["static_base_url"] = f"/file={MODELS_DIR}/"
125
+ else:
126
+ scene_data["static_base_url"] = f"{FASTAPI_INTERNAL}/static/models/"
127
 
128
  # Read the viewer HTML template
129
  viewer_path = os.path.join(os.path.dirname(__file__), "frontend", "game_viewer.html")
backend/storage.py CHANGED
@@ -81,13 +81,14 @@ def initialize_default_scene():
81
  )
82
 
83
  # Hugging Face emoji model at center (animated)
 
84
  objects = [
85
  create_game_object(
86
  object_type="model",
87
  name="HuggingFace",
88
  position=create_vector3(0, 1.5, 0),
89
  scale=create_vector3(3, 3, 3),
90
- model_path="/static/models/Norod78/huggingface_emoji.glb",
91
  metadata={"animate": True, "baseY": 1.5},
92
  ),
93
  ]
 
81
  )
82
 
83
  # Hugging Face emoji model at center (animated)
84
+ # model_path is relative - will be resolved by viewer based on environment
85
  objects = [
86
  create_game_object(
87
  object_type="model",
88
  name="HuggingFace",
89
  position=create_vector3(0, 1.5, 0),
90
  scale=create_vector3(3, 3, 3),
91
+ model_path="Norod78/huggingface_emoji.glb",
92
  metadata={"animate": True, "baseY": 1.5},
93
  ),
94
  ]