Spaces:
Sleeping
Sleeping
Commit ·
5b8b6b8
1
Parent(s): 121e46d
Fix file permission issues for Docker deployment
Browse files- Add tempfile.mkdtemp() for system temp directory creation
- Add file permission management with os.chmod()
- Update all file paths to use absolute paths
- Fix upload endpoint temp file handling
- Fix download endpoint to use OUTPUTS_DIR
- Resolve Permission denied errors in HF Spaces
- highlights_api.py +26 -10
highlights_api.py
CHANGED
|
@@ -72,9 +72,20 @@ class JobStatus(BaseModel):
|
|
| 72 |
active_jobs = {}
|
| 73 |
completed_jobs = {}
|
| 74 |
|
| 75 |
-
# Create output directories
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
@app.get("/")
|
| 80 |
async def root():
|
|
@@ -109,12 +120,17 @@ async def upload_video(
|
|
| 109 |
job_id = str(uuid.uuid4())
|
| 110 |
|
| 111 |
try:
|
| 112 |
-
# Save uploaded video
|
| 113 |
-
|
|
|
|
|
|
|
| 114 |
with open(temp_video_path, "wb") as f:
|
| 115 |
content = await video.read()
|
| 116 |
f.write(content)
|
| 117 |
|
|
|
|
|
|
|
|
|
|
| 118 |
# Store job info
|
| 119 |
active_jobs[job_id] = {
|
| 120 |
"status": "processing",
|
|
@@ -186,7 +202,7 @@ async def download_file(filename: str):
|
|
| 186 |
"""
|
| 187 |
Download generated files
|
| 188 |
"""
|
| 189 |
-
file_path =
|
| 190 |
if not os.path.exists(file_path):
|
| 191 |
raise HTTPException(status_code=404, detail="File not found")
|
| 192 |
|
|
@@ -231,7 +247,7 @@ async def process_video_highlights(
|
|
| 231 |
|
| 232 |
# Analyze segments
|
| 233 |
analyzed_segments = []
|
| 234 |
-
temp_frame_path = f"
|
| 235 |
|
| 236 |
for i, segment in enumerate(segments):
|
| 237 |
# Update progress
|
|
@@ -265,11 +281,11 @@ async def process_video_highlights(
|
|
| 265 |
active_jobs[job_id]["progress"] = 90
|
| 266 |
active_jobs[job_id]["message"] = f"Creating highlights video with {len(selected_segments)} segments..."
|
| 267 |
|
| 268 |
-
# Create output filenames
|
| 269 |
highlights_filename = f"{job_id}_highlights.mp4"
|
| 270 |
analysis_filename = f"{job_id}_analysis.json"
|
| 271 |
-
highlights_path =
|
| 272 |
-
analysis_path =
|
| 273 |
|
| 274 |
# Create highlights video
|
| 275 |
success = create_highlights_video(video_path, selected_segments, highlights_path)
|
|
|
|
| 72 |
active_jobs = {}
|
| 73 |
completed_jobs = {}
|
| 74 |
|
| 75 |
+
# Create output directories with proper permissions
|
| 76 |
+
import tempfile
|
| 77 |
+
import stat
|
| 78 |
+
|
| 79 |
+
# Use system temp directory for Hugging Face Spaces compatibility
|
| 80 |
+
TEMP_DIR = tempfile.mkdtemp(prefix="smolvlm2_")
|
| 81 |
+
OUTPUTS_DIR = os.path.join(os.getcwd(), "outputs")
|
| 82 |
+
|
| 83 |
+
os.makedirs(OUTPUTS_DIR, mode=0o755, exist_ok=True)
|
| 84 |
+
os.makedirs(TEMP_DIR, mode=0o755, exist_ok=True)
|
| 85 |
+
|
| 86 |
+
# Ensure directories are writable
|
| 87 |
+
os.chmod(TEMP_DIR, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
|
| 88 |
+
os.chmod(OUTPUTS_DIR, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
|
| 89 |
|
| 90 |
@app.get("/")
|
| 91 |
async def root():
|
|
|
|
| 120 |
job_id = str(uuid.uuid4())
|
| 121 |
|
| 122 |
try:
|
| 123 |
+
# Save uploaded video to system temp directory
|
| 124 |
+
safe_filename = "".join(c for c in video.filename if c.isalnum() or c in '._-')
|
| 125 |
+
temp_video_path = os.path.join(TEMP_DIR, f"{job_id}_{safe_filename}")
|
| 126 |
+
|
| 127 |
with open(temp_video_path, "wb") as f:
|
| 128 |
content = await video.read()
|
| 129 |
f.write(content)
|
| 130 |
|
| 131 |
+
# Ensure the file is readable
|
| 132 |
+
os.chmod(temp_video_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
|
| 133 |
+
|
| 134 |
# Store job info
|
| 135 |
active_jobs[job_id] = {
|
| 136 |
"status": "processing",
|
|
|
|
| 202 |
"""
|
| 203 |
Download generated files
|
| 204 |
"""
|
| 205 |
+
file_path = os.path.join(OUTPUTS_DIR, filename)
|
| 206 |
if not os.path.exists(file_path):
|
| 207 |
raise HTTPException(status_code=404, detail="File not found")
|
| 208 |
|
|
|
|
| 247 |
|
| 248 |
# Analyze segments
|
| 249 |
analyzed_segments = []
|
| 250 |
+
temp_frame_path = os.path.join(TEMP_DIR, f"{job_id}_frame.jpg")
|
| 251 |
|
| 252 |
for i, segment in enumerate(segments):
|
| 253 |
# Update progress
|
|
|
|
| 281 |
active_jobs[job_id]["progress"] = 90
|
| 282 |
active_jobs[job_id]["message"] = f"Creating highlights video with {len(selected_segments)} segments..."
|
| 283 |
|
| 284 |
+
# Create output filenames using absolute paths
|
| 285 |
highlights_filename = f"{job_id}_highlights.mp4"
|
| 286 |
analysis_filename = f"{job_id}_analysis.json"
|
| 287 |
+
highlights_path = os.path.join(OUTPUTS_DIR, highlights_filename)
|
| 288 |
+
analysis_path = os.path.join(OUTPUTS_DIR, analysis_filename)
|
| 289 |
|
| 290 |
# Create highlights video
|
| 291 |
success = create_highlights_video(video_path, selected_segments, highlights_path)
|