Spaces:
Running
Running
- Create the models directory on demand
Browse files- Fallback to tmp if permission issues
- Keep models out of version control
- Handle both local and HF Space environments
- Prevent accidental model commits
- .gitignore +22 -3
- src/app.py +16 -3
.gitignore
CHANGED
@@ -1,4 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
venv/
|
2 |
-
.
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Model cache directories
|
2 |
+
models/
|
3 |
+
.cache/
|
4 |
+
|
5 |
+
# Python
|
6 |
+
__pycache__/
|
7 |
+
*.py[cod]
|
8 |
+
*$py.class
|
9 |
+
.Python
|
10 |
+
*.so
|
11 |
+
.env
|
12 |
+
.venv
|
13 |
+
env/
|
14 |
venv/
|
15 |
+
.python-version
|
16 |
+
|
17 |
+
# IDE
|
18 |
+
.vscode/
|
19 |
+
.idea/
|
20 |
+
|
21 |
+
# OS
|
22 |
+
.DS_Store
|
23 |
+
Thumbs.db
|
src/app.py
CHANGED
@@ -87,9 +87,22 @@ from typing import Optional
|
|
87 |
def ensure_model(model_name: Optional[str] = None, repo_id: Optional[str] = None) -> str:
|
88 |
"""Ensures model is available, downloading only if needed."""
|
89 |
|
90 |
-
#
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
|
94 |
# Get model details
|
95 |
if not model_name or not repo_id:
|
|
|
87 |
def ensure_model(model_name: Optional[str] = None, repo_id: Optional[str] = None) -> str:
|
88 |
"""Ensures model is available, downloading only if needed."""
|
89 |
|
90 |
+
# Determine environment and set cache directory
|
91 |
+
if os.path.exists("/home/user"):
|
92 |
+
# HF Space environment
|
93 |
+
cache_dir = "/home/user/.cache/models"
|
94 |
+
else:
|
95 |
+
# Local development environment
|
96 |
+
cache_dir = os.path.join(BASE_DIR, "models")
|
97 |
+
|
98 |
+
# Create cache directory if it doesn't exist
|
99 |
+
try:
|
100 |
+
os.makedirs(cache_dir, exist_ok=True)
|
101 |
+
except Exception as e:
|
102 |
+
print(f"Warning: Could not create cache directory {cache_dir}: {e}")
|
103 |
+
# Fall back to temporary directory if needed
|
104 |
+
cache_dir = os.path.join("/tmp", "models")
|
105 |
+
os.makedirs(cache_dir, exist_ok=True)
|
106 |
|
107 |
# Get model details
|
108 |
if not model_name or not repo_id:
|