Spaces:
Running
Running
Update server.py
Browse files
server.py
CHANGED
|
@@ -12,6 +12,7 @@ from typing import List, Dict, Optional, Tuple
|
|
| 12 |
|
| 13 |
from fastapi import FastAPI, HTTPException
|
| 14 |
from fastapi.responses import JSONResponse
|
|
|
|
| 15 |
import uvicorn
|
| 16 |
|
| 17 |
try:
|
|
@@ -32,7 +33,15 @@ if not HF_TOKEN:
|
|
| 32 |
print("Error: Missing HF_TOKEN in .env")
|
| 33 |
exit(1)
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# Global state
|
| 38 |
processing_state = {
|
|
@@ -573,15 +582,6 @@ async def scan_and_process_videos():
|
|
| 573 |
processing_state["current_file"] = None
|
| 574 |
|
| 575 |
|
| 576 |
-
@app.on_event("startup")
|
| 577 |
-
async def startup_event():
|
| 578 |
-
"""Load Whisper in background, then kick off video processing."""
|
| 579 |
-
loop = asyncio.get_event_loop()
|
| 580 |
-
# Load Whisper model in thread so it doesn't block the event loop / health check
|
| 581 |
-
await loop.run_in_executor(None, _load_whisper_model)
|
| 582 |
-
# Kick off processing task (has its own startup delay inside)
|
| 583 |
-
asyncio.create_task(scan_and_process_videos())
|
| 584 |
-
|
| 585 |
|
| 586 |
@app.get("/")
|
| 587 |
async def health():
|
|
|
|
| 12 |
|
| 13 |
from fastapi import FastAPI, HTTPException
|
| 14 |
from fastapi.responses import JSONResponse
|
| 15 |
+
from contextlib import asynccontextmanager
|
| 16 |
import uvicorn
|
| 17 |
|
| 18 |
try:
|
|
|
|
| 33 |
print("Error: Missing HF_TOKEN in .env")
|
| 34 |
exit(1)
|
| 35 |
|
| 36 |
+
@asynccontextmanager
|
| 37 |
+
async def lifespan(app: FastAPI):
|
| 38 |
+
"""Load Whisper in background, then kick off video processing."""
|
| 39 |
+
loop = asyncio.get_event_loop()
|
| 40 |
+
await loop.run_in_executor(None, _load_whisper_model)
|
| 41 |
+
asyncio.create_task(scan_and_process_videos())
|
| 42 |
+
yield
|
| 43 |
+
|
| 44 |
+
app = FastAPI(title="Video Processing Service", lifespan=lifespan)
|
| 45 |
|
| 46 |
# Global state
|
| 47 |
processing_state = {
|
|
|
|
| 582 |
processing_state["current_file"] = None
|
| 583 |
|
| 584 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 585 |
|
| 586 |
@app.get("/")
|
| 587 |
async def health():
|