VeuReu commited on
Commit
55a2d5e
verified
1 Parent(s): 0420072

Update storage/embeddings_routers.py

Browse files
Files changed (1) hide show
  1. storage/embeddings_routers.py +75 -0
storage/embeddings_routers.py CHANGED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import io
3
+ import shutil
4
+
5
+ import sqlite3
6
+
7
+ from pathlib import Path
8
+
9
+ from fastapi import APIRouter, UploadFile, File, Query, HTTPException
10
+ from fastapi.responses import FileResponse, JSONResponse
11
+
12
+
13
+ from storage.files.file_manager import FileManager
14
+ from storage.common import validate_token
15
+
16
+ router = APIRouter(prefix="/embeddings", tags=["Embeddings Manager"])
17
+ EMBEDDINGS_ROOT = Path("/data/embeddings")
18
+ file_manager = FileManager(EMBEDDINGS_ROOT)
19
+ HF_TOKEN = os.getenv("HF_TOKEN")
20
+
21
+
22
+ @router.post("/upload_embeddings", tags=["Embeddings Manager"])
23
+ async def upload_embeddings(
24
+ file: UploadFile = File(...),
25
+ embedding_type: str = Query(..., description="faces or voices"),
26
+ video_hash: str = Query(..., description="Hash of the video"),
27
+ token: str = Query(..., description="Token required for authorization")
28
+ ):
29
+ """
30
+ Upload embeddings JSON for a given video and type (faces or voices).
31
+
32
+ Behavior:
33
+ - Validate the token.
34
+ - Validate embedding_type.
35
+ - Ensure directory structure: /data/embeddings/<video_hash>/<embedding_type>/
36
+ - Delete any existing .json file inside that folder.
37
+ - Save the uploaded embeddings as embeddings.json.
38
+ """
39
+ validate_token(token)
40
+
41
+ # Validaci贸n del tipo
42
+ if embedding_type not in ("faces", "voices"):
43
+ raise HTTPException(status_code=400, detail="embedding_type must be 'faces' or 'voices'")
44
+
45
+ # Rutas objetivo
46
+ video_path = EMBEDDINGS_ROOT / video_hash
47
+ type_path = video_path / embedding_type
48
+
49
+ # Crear carpetas si no existen
50
+ type_path.mkdir(parents=True, exist_ok=True)
51
+
52
+ # Eliminar JSONs previos
53
+ for existing in type_path.glob("*.json"):
54
+ try:
55
+ existing.unlink()
56
+ except Exception as exc:
57
+ raise HTTPException(status_code=500, detail=f"Failed to delete old embeddings: {exc}")
58
+
59
+ # Guardar como embeddings.json
60
+ final_path = type_path / "embeddings.json"
61
+
62
+ try:
63
+ file_bytes = await file.read()
64
+ with open(final_path, "wb") as f:
65
+ f.write(file_bytes)
66
+ except Exception as exc:
67
+ raise HTTPException(status_code=500, detail=f"Failed to save embeddings: {exc}")
68
+
69
+ return JSONResponse(
70
+ status_code=200,
71
+ content={
72
+ "status": "ok",
73
+ "saved_to": str(final_path)
74
+ }
75
+ )