from fastapi import FastAPI, UploadFile, File
from fastapi.responses import FileResponse
import shutil
import os
import uuid
import pandas as pd
from pipeline import analyze_video_pipeline
app = FastAPI(title="Worker API")
TEMP_DIR = "/tmp/worker"
os.makedirs(TEMP_DIR, exist_ok=True)
# Load ghost reference once
GHOST_DF = pd.read_csv("ollie_reference.csv")
@app.post("/process")
async def process_video(file: UploadFile = File(...)):
job_id = uuid.uuid4().hex[:8]
input_path = os.path.join(TEMP_DIR, f"{job_id}_in.mp4")
with open(input_path, "wb") as f:
shutil.copyfileobj(file.file, f)
try:
output_path = analyze_video_pipeline(input_path, GHOST_DF, work_dir=TEMP_DIR)
return FileResponse(output_path, media_type="video/mp4")
except Exception as e:
return {"error": str(e)}
finally:
if os.path.exists(input_path):
os.remove(input_path)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)