Upload api.py
Browse files
api.py
CHANGED
|
@@ -1,224 +1,226 @@
|
|
| 1 |
-
from
|
| 2 |
-
|
| 3 |
-
from
|
| 4 |
-
from fastapi
|
| 5 |
-
from
|
| 6 |
-
import
|
| 7 |
-
import
|
| 8 |
-
import
|
| 9 |
-
import
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
from
|
| 13 |
-
import
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
from
|
| 18 |
-
from
|
| 19 |
-
from
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
)
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
)
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
"
|
| 88 |
-
"
|
| 89 |
-
"
|
| 90 |
-
"
|
| 91 |
-
"
|
| 92 |
-
"
|
| 93 |
-
"
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
#
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
"
|
|
|
|
|
|
|
| 222 |
"base_dir": str(base)
|
| 223 |
}
|
| 224 |
job["status"] = JobStatus.DONE
|
|
@@ -257,6 +259,35 @@ def process_video_job(job_id: str):
|
|
| 257 |
jobs[job_id]["status"] = JobStatus.FAILED
|
| 258 |
jobs[job_id]["error"] = str(e)
|
| 259 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
@app.post("/load_casting")
|
| 261 |
async def load_casting(
|
| 262 |
faces_dir: str = Form("identities/faces"),
|
|
|
|
| 1 |
+
from pipelines.audiodescription import generate as ad_generate
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
from fastapi import FastAPI, UploadFile, File, Form, BackgroundTasks, HTTPException
|
| 5 |
+
from fastapi.responses import JSONResponse, FileResponse
|
| 6 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
import shutil
|
| 9 |
+
import uvicorn
|
| 10 |
+
import json
|
| 11 |
+
import uuid
|
| 12 |
+
from datetime import datetime
|
| 13 |
+
from typing import Dict
|
| 14 |
+
from enum import Enum
|
| 15 |
+
import os
|
| 16 |
+
|
| 17 |
+
from video_processing import process_video_pipeline
|
| 18 |
+
from casting_loader import ensure_chroma, build_faces_index, build_voices_index
|
| 19 |
+
from narration_system import NarrationSystem
|
| 20 |
+
from llm_router import load_yaml, LLMRouter
|
| 21 |
+
from character_detection import detect_characters_from_video
|
| 22 |
+
|
| 23 |
+
app = FastAPI(title="Veureu Engine API", version="0.2.0")
|
| 24 |
+
app.add_middleware(
|
| 25 |
+
CORSMiddleware,
|
| 26 |
+
allow_origins=["*"],
|
| 27 |
+
allow_credentials=True,
|
| 28 |
+
allow_methods=["*"],
|
| 29 |
+
allow_headers=["*"],
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
ROOT = Path("/tmp/veureu")
|
| 33 |
+
ROOT.mkdir(parents=True, exist_ok=True)
|
| 34 |
+
TEMP_ROOT = Path("/tmp/temp")
|
| 35 |
+
TEMP_ROOT.mkdir(parents=True, exist_ok=True)
|
| 36 |
+
VIDEOS_ROOT = Path("/tmp/data/videos")
|
| 37 |
+
VIDEOS_ROOT.mkdir(parents=True, exist_ok=True)
|
| 38 |
+
|
| 39 |
+
# Sistema de jobs asíncronos
|
| 40 |
+
class JobStatus(str, Enum):
|
| 41 |
+
QUEUED = "queued"
|
| 42 |
+
PROCESSING = "processing"
|
| 43 |
+
DONE = "done"
|
| 44 |
+
FAILED = "failed"
|
| 45 |
+
|
| 46 |
+
jobs: Dict[str, dict] = {}
|
| 47 |
+
|
| 48 |
+
@app.get("/")
|
| 49 |
+
def root():
|
| 50 |
+
return {"ok": True, "service": "veureu-engine"}
|
| 51 |
+
|
| 52 |
+
@app.post("/process_video")
|
| 53 |
+
async def process_video(
|
| 54 |
+
video_file: UploadFile = File(...),
|
| 55 |
+
config_path: str = Form("config.yaml"),
|
| 56 |
+
out_root: str = Form("results"),
|
| 57 |
+
db_dir: str = Form("chroma_db"),
|
| 58 |
+
):
|
| 59 |
+
tmp_video = ROOT / video_file.filename
|
| 60 |
+
with tmp_video.open("wb") as f:
|
| 61 |
+
shutil.copyfileobj(video_file.file, f)
|
| 62 |
+
result = process_video_pipeline(str(tmp_video), config_path=config_path, out_root=out_root, db_dir=db_dir)
|
| 63 |
+
return JSONResponse(result)
|
| 64 |
+
|
| 65 |
+
@app.post("/create_initial_casting")
|
| 66 |
+
async def create_initial_casting(
|
| 67 |
+
background_tasks: BackgroundTasks,
|
| 68 |
+
video: UploadFile = File(...),
|
| 69 |
+
epsilon: float = Form(...),
|
| 70 |
+
min_cluster_size: int = Form(...),
|
| 71 |
+
):
|
| 72 |
+
"""
|
| 73 |
+
Crea un job para procesar el vídeo de forma asíncrona.
|
| 74 |
+
Devuelve un job_id inmediatamente.
|
| 75 |
+
"""
|
| 76 |
+
# Guardar vídeo en carpeta de datos
|
| 77 |
+
video_name = Path(video.filename).stem
|
| 78 |
+
dst_video = VIDEOS_ROOT / f"{video_name}.mp4"
|
| 79 |
+
with dst_video.open("wb") as f:
|
| 80 |
+
shutil.copyfileobj(video.file, f)
|
| 81 |
+
|
| 82 |
+
# Crear job_id único
|
| 83 |
+
job_id = str(uuid.uuid4())
|
| 84 |
+
|
| 85 |
+
# Inicializar el job
|
| 86 |
+
jobs[job_id] = {
|
| 87 |
+
"id": job_id,
|
| 88 |
+
"status": JobStatus.QUEUED,
|
| 89 |
+
"video_path": str(dst_video),
|
| 90 |
+
"video_name": video_name,
|
| 91 |
+
"epsilon": float(epsilon),
|
| 92 |
+
"min_cluster_size": int(min_cluster_size),
|
| 93 |
+
"created_at": datetime.now().isoformat(),
|
| 94 |
+
"results": None,
|
| 95 |
+
"error": None
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
print(f"[{job_id}] Job creado para vídeo: {video_name}")
|
| 99 |
+
|
| 100 |
+
# Iniciar procesamiento en background
|
| 101 |
+
background_tasks.add_task(process_video_job, job_id)
|
| 102 |
+
|
| 103 |
+
# Devolver job_id inmediatamente
|
| 104 |
+
return {"job_id": job_id}
|
| 105 |
+
|
| 106 |
+
@app.get("/jobs/{job_id}/status")
|
| 107 |
+
def get_job_status(job_id: str):
|
| 108 |
+
"""
|
| 109 |
+
Devuelve el estado actual de un job.
|
| 110 |
+
El UI hace polling de este endpoint cada 5 segundos.
|
| 111 |
+
"""
|
| 112 |
+
if job_id not in jobs:
|
| 113 |
+
raise HTTPException(status_code=404, detail="Job not found")
|
| 114 |
+
|
| 115 |
+
job = jobs[job_id]
|
| 116 |
+
|
| 117 |
+
# Normalizar el estado a string
|
| 118 |
+
status_value = job["status"].value if isinstance(job["status"], JobStatus) else str(job["status"])
|
| 119 |
+
response = {"status": status_value}
|
| 120 |
+
|
| 121 |
+
# Incluir resultados si existen (evita condiciones de carrera)
|
| 122 |
+
if job.get("results") is not None:
|
| 123 |
+
response["results"] = job["results"]
|
| 124 |
+
|
| 125 |
+
# Incluir error si existe
|
| 126 |
+
if job.get("error"):
|
| 127 |
+
response["error"] = job["error"]
|
| 128 |
+
|
| 129 |
+
return response
|
| 130 |
+
|
| 131 |
+
@app.get("/files/{video_name}/{char_id}/{filename}")
|
| 132 |
+
def serve_character_file(video_name: str, char_id: str, filename: str):
|
| 133 |
+
"""
|
| 134 |
+
Sirve archivos estáticos de personajes (imágenes).
|
| 135 |
+
Ejemplo: /files/dif_catala_1/char1/representative.jpg
|
| 136 |
+
"""
|
| 137 |
+
file_path = TEMP_ROOT / video_name / char_id / filename
|
| 138 |
+
|
| 139 |
+
if not file_path.exists():
|
| 140 |
+
raise HTTPException(status_code=404, detail="File not found")
|
| 141 |
+
|
| 142 |
+
return FileResponse(file_path)
|
| 143 |
+
|
| 144 |
+
def process_video_job(job_id: str):
|
| 145 |
+
"""
|
| 146 |
+
Procesa el vídeo de forma asíncrona.
|
| 147 |
+
Esta función se ejecuta en background.
|
| 148 |
+
"""
|
| 149 |
+
try:
|
| 150 |
+
job = jobs[job_id]
|
| 151 |
+
print(f"[{job_id}] Iniciando procesamiento...")
|
| 152 |
+
|
| 153 |
+
# Cambiar estado a processing
|
| 154 |
+
job["status"] = JobStatus.PROCESSING
|
| 155 |
+
|
| 156 |
+
video_path = job["video_path"]
|
| 157 |
+
video_name = job["video_name"]
|
| 158 |
+
epsilon = job["epsilon"]
|
| 159 |
+
min_cluster_size = job["min_cluster_size"]
|
| 160 |
+
|
| 161 |
+
# Crear estructura de carpetas
|
| 162 |
+
base = TEMP_ROOT / video_name
|
| 163 |
+
base.mkdir(parents=True, exist_ok=True)
|
| 164 |
+
|
| 165 |
+
print(f"[{job_id}] Directorio base: {base}")
|
| 166 |
+
|
| 167 |
+
# Detección real de personajes usando el código de Ana
|
| 168 |
+
try:
|
| 169 |
+
print(f"[{job_id}] Iniciando detección de personajes...")
|
| 170 |
+
result = detect_characters_from_video(
|
| 171 |
+
video_path=video_path,
|
| 172 |
+
output_base=str(base),
|
| 173 |
+
epsilon=epsilon,
|
| 174 |
+
min_cluster_size=min_cluster_size,
|
| 175 |
+
video_name=video_name
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
print(f"[{job_id}] DEBUG - result completo: {result}")
|
| 179 |
+
|
| 180 |
+
characters = result.get("characters", [])
|
| 181 |
+
analysis_path = result.get("analysis_path", "")
|
| 182 |
+
|
| 183 |
+
print(f"[{job_id}] Personajes detectados: {len(characters)}")
|
| 184 |
+
for char in characters:
|
| 185 |
+
print(f"[{job_id}] - {char['name']}: {char['num_faces']} caras")
|
| 186 |
+
|
| 187 |
+
# Enriquecer info de personajes con listado real de imágenes disponibles
|
| 188 |
+
try:
|
| 189 |
+
import glob, os
|
| 190 |
+
for ch in characters:
|
| 191 |
+
folder = ch.get("folder")
|
| 192 |
+
face_files = []
|
| 193 |
+
if folder and os.path.isdir(folder):
|
| 194 |
+
# soportar patrones face_* y extensiones jpg/png
|
| 195 |
+
patterns = ["face_*.jpg", "face_*.png"]
|
| 196 |
+
files = []
|
| 197 |
+
for pat in patterns:
|
| 198 |
+
files.extend(glob.glob(os.path.join(folder, pat)))
|
| 199 |
+
# si no hay face_*, tomar cualquier jpg/png para no dejar vacío
|
| 200 |
+
if not files:
|
| 201 |
+
files.extend(glob.glob(os.path.join(folder, "*.jpg")))
|
| 202 |
+
files.extend(glob.glob(os.path.join(folder, "*.png")))
|
| 203 |
+
# normalizar nombres de fichero relativos
|
| 204 |
+
face_files = sorted({os.path.basename(p) for p in files})
|
| 205 |
+
# Garantizar que representative.(jpg|png) esté el primero si existe
|
| 206 |
+
for rep_name in ("representative.jpg", "representative.png"):
|
| 207 |
+
rep_path = os.path.join(folder, rep_name)
|
| 208 |
+
if os.path.exists(rep_path):
|
| 209 |
+
if rep_name in face_files:
|
| 210 |
+
face_files.remove(rep_name)
|
| 211 |
+
face_files.insert(0, rep_name)
|
| 212 |
+
ch["face_files"] = face_files
|
| 213 |
+
# Ajustar num_faces si hay discrepancia
|
| 214 |
+
if face_files:
|
| 215 |
+
ch["num_faces"] = len(face_files)
|
| 216 |
+
except Exception as _e:
|
| 217 |
+
print(f"[{job_id}] WARN - No se pudo enumerar face_files: {_e}")
|
| 218 |
+
|
| 219 |
+
# Guardar resultados primero y luego marcar como completado (evita carreras)
|
| 220 |
+
job["results"] = {
|
| 221 |
+
"characters": characters,
|
| 222 |
+
"num_characters": len(characters),
|
| 223 |
+
"analysis_path": analysis_path,
|
| 224 |
"base_dir": str(base)
|
| 225 |
}
|
| 226 |
job["status"] = JobStatus.DONE
|
|
|
|
| 259 |
jobs[job_id]["status"] = JobStatus.FAILED
|
| 260 |
jobs[job_id]["error"] = str(e)
|
| 261 |
|
| 262 |
+
@app.post("/generate_audiodescription")
|
| 263 |
+
async def generate_audiodescription(video: UploadFile = File(...)):
|
| 264 |
+
try:
|
| 265 |
+
import uuid
|
| 266 |
+
job_id = str(uuid.uuid4())
|
| 267 |
+
vid_name = video.filename or f"video_{job_id}.mp4"
|
| 268 |
+
base = BASE_TEMP_DIR / Path(vid_name).stem
|
| 269 |
+
base.mkdir(parents=True, exist_ok=True)
|
| 270 |
+
# Save temp mp4
|
| 271 |
+
video_path = base / vid_name
|
| 272 |
+
with open(video_path, "wb") as f:
|
| 273 |
+
f.write(await video.read())
|
| 274 |
+
|
| 275 |
+
# Run MVP pipeline
|
| 276 |
+
result = ad_generate(str(video_path), base)
|
| 277 |
+
|
| 278 |
+
return {
|
| 279 |
+
"status": "done",
|
| 280 |
+
"results": {
|
| 281 |
+
"une_srt": result.get("une_srt", ""),
|
| 282 |
+
"free_text": result.get("free_text", ""),
|
| 283 |
+
"artifacts": result.get("artifacts", {}),
|
| 284 |
+
},
|
| 285 |
+
}
|
| 286 |
+
except Exception as e:
|
| 287 |
+
import traceback
|
| 288 |
+
print(f"/generate_audiodescription error: {e}\n{traceback.format_exc()}")
|
| 289 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 290 |
+
|
| 291 |
@app.post("/load_casting")
|
| 292 |
async def load_casting(
|
| 293 |
faces_dir: str = Form("identities/faces"),
|