Spaces:
Running
Running
Update server.py
Browse files
server.py
CHANGED
|
@@ -172,6 +172,7 @@ def compress_video(input_path: str, output_path: str) -> Dict:
|
|
| 172 |
async def load_progress_file() -> Dict:
|
| 173 |
"""Load compression progress from JSON file in HF dataset."""
|
| 174 |
try:
|
|
|
|
| 175 |
# Try to download existing progress file
|
| 176 |
progress_path = hf_hub_download(
|
| 177 |
repo_id=HF_DATASET_REPO,
|
|
@@ -186,10 +187,12 @@ async def load_progress_file() -> Dict:
|
|
| 186 |
|
| 187 |
with open(progress_path, 'r') as f:
|
| 188 |
progress = json.load(f)
|
| 189 |
-
|
|
|
|
|
|
|
| 190 |
return progress
|
| 191 |
except Exception as e:
|
| 192 |
-
print(f"No existing progress file
|
| 193 |
return {"compressed": [], "failed": [], "last_updated": datetime.now().isoformat()}
|
| 194 |
|
| 195 |
|
|
@@ -233,16 +236,26 @@ async def scan_and_compress_videos():
|
|
| 233 |
compressed_files = {item["path"] for item in progress.get("compressed", [])}
|
| 234 |
failed_files = {item["path"] for item in progress.get("failed", [])}
|
| 235 |
|
|
|
|
|
|
|
| 236 |
print("\n" + "="*80)
|
| 237 |
print("SCANNING FOR LARGE VIDEOS")
|
| 238 |
print("="*80)
|
| 239 |
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
|
| 247 |
# Find unprocessed video files
|
| 248 |
video_files = []
|
|
@@ -272,7 +285,7 @@ async def scan_and_compress_videos():
|
|
| 272 |
continue
|
| 273 |
|
| 274 |
compression_state["total_found"] = len(video_files)
|
| 275 |
-
print(f"Found {len(video_files)} unprocessed video files")
|
| 276 |
|
| 277 |
if not video_files:
|
| 278 |
print("β All videos already processed!")
|
|
@@ -389,15 +402,26 @@ async def scan_and_compress_videos():
|
|
| 389 |
|
| 390 |
@app.on_event("startup")
|
| 391 |
async def startup_event():
|
| 392 |
-
"""Start
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 399 |
|
| 400 |
-
|
|
|
|
|
|
|
| 401 |
|
| 402 |
|
| 403 |
@app.get("/")
|
|
|
|
| 172 |
async def load_progress_file() -> Dict:
|
| 173 |
"""Load compression progress from JSON file in HF dataset."""
|
| 174 |
try:
|
| 175 |
+
print("Attempting to load progress file from dataset...")
|
| 176 |
# Try to download existing progress file
|
| 177 |
progress_path = hf_hub_download(
|
| 178 |
repo_id=HF_DATASET_REPO,
|
|
|
|
| 187 |
|
| 188 |
with open(progress_path, 'r') as f:
|
| 189 |
progress = json.load(f)
|
| 190 |
+
compressed_count = len(progress.get('compressed', []))
|
| 191 |
+
failed_count = len(progress.get('failed', []))
|
| 192 |
+
print(f"β Loaded progress: {compressed_count} compressed, {failed_count} failed")
|
| 193 |
return progress
|
| 194 |
except Exception as e:
|
| 195 |
+
print(f"β No existing progress file or load failed: {str(e)[:100]}")
|
| 196 |
return {"compressed": [], "failed": [], "last_updated": datetime.now().isoformat()}
|
| 197 |
|
| 198 |
|
|
|
|
| 236 |
compressed_files = {item["path"] for item in progress.get("compressed", [])}
|
| 237 |
failed_files = {item["path"] for item in progress.get("failed", [])}
|
| 238 |
|
| 239 |
+
print(f"Current state: {len(compressed_files)} compressed, {len(failed_files)} failed")
|
| 240 |
+
|
| 241 |
print("\n" + "="*80)
|
| 242 |
print("SCANNING FOR LARGE VIDEOS")
|
| 243 |
print("="*80)
|
| 244 |
|
| 245 |
+
try:
|
| 246 |
+
# List all files in ready_videos
|
| 247 |
+
print("Connecting to Hugging Face dataset...")
|
| 248 |
+
files = list_repo_files(
|
| 249 |
+
repo_id=HF_DATASET_REPO,
|
| 250 |
+
repo_type="dataset",
|
| 251 |
+
token=HF_TOKEN
|
| 252 |
+
)
|
| 253 |
+
print(f"β Found {len(files)} total files in dataset")
|
| 254 |
+
except Exception as e:
|
| 255 |
+
print(f"β Error listing files: {e}")
|
| 256 |
+
compression_state["last_error"] = str(e)
|
| 257 |
+
compression_state["is_polling"] = False
|
| 258 |
+
return
|
| 259 |
|
| 260 |
# Find unprocessed video files
|
| 261 |
video_files = []
|
|
|
|
| 285 |
continue
|
| 286 |
|
| 287 |
compression_state["total_found"] = len(video_files)
|
| 288 |
+
print(f"\nβ Found {len(video_files)} unprocessed video files")
|
| 289 |
|
| 290 |
if not video_files:
|
| 291 |
print("β All videos already processed!")
|
|
|
|
| 402 |
|
| 403 |
@app.on_event("startup")
|
| 404 |
async def startup_event():
|
| 405 |
+
"""Start compression scan on server startup."""
|
| 406 |
+
print("\n" + "="*80)
|
| 407 |
+
print("STARTUP EVENT TRIGGERED")
|
| 408 |
+
print("="*80)
|
| 409 |
+
|
| 410 |
+
# Check if ffmpeg/ffprobe are available
|
| 411 |
+
try:
|
| 412 |
+
result = subprocess.run(["ffmpeg", "-version"], capture_output=True, timeout=5)
|
| 413 |
+
if result.returncode == 0:
|
| 414 |
+
print("β ffmpeg is available")
|
| 415 |
+
else:
|
| 416 |
+
print("β ffmpeg check failed")
|
| 417 |
+
except FileNotFoundError:
|
| 418 |
+
print("β WARNING: ffmpeg not found in PATH - compression will fail")
|
| 419 |
+
except Exception as e:
|
| 420 |
+
print(f"β Error checking ffmpeg: {e}")
|
| 421 |
|
| 422 |
+
# Schedule scan immediately instead of with delay
|
| 423 |
+
print("Starting compression scan immediately...")
|
| 424 |
+
await scan_and_compress_videos()
|
| 425 |
|
| 426 |
|
| 427 |
@app.get("/")
|