Update storage/embeddings_routers.py
Browse files
storage/embeddings_routers.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import io
|
|
|
|
| 3 |
import shutil
|
| 4 |
|
| 5 |
import sqlite3
|
|
@@ -113,3 +114,49 @@ async def upload_embeddings(
|
|
| 113 |
"saved_to": str(final_path)
|
| 114 |
}
|
| 115 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import io
|
| 3 |
+
import json
|
| 4 |
import shutil
|
| 5 |
|
| 6 |
import sqlite3
|
|
|
|
| 114 |
"saved_to": str(final_path)
|
| 115 |
}
|
| 116 |
)
|
| 117 |
+
|
| 118 |
+
def get_embeddings_json(video_hash: str, embedding_type: str):
|
| 119 |
+
"""
|
| 120 |
+
Returns the parsed embeddings.json for a given video and type.
|
| 121 |
+
|
| 122 |
+
Behavior:
|
| 123 |
+
- Validate embedding_type.
|
| 124 |
+
- Build the file path: /data/embeddings/<video_hash>/<embedding_type>/embeddings.json
|
| 125 |
+
- Raise HTTPException if missing.
|
| 126 |
+
- Load and return parsed JSON.
|
| 127 |
+
"""
|
| 128 |
+
|
| 129 |
+
if embedding_type not in ("faces", "voices"):
|
| 130 |
+
raise HTTPException(status_code=400, detail="embedding_type must be 'faces' or 'voices'")
|
| 131 |
+
|
| 132 |
+
target_file = EMBEDDINGS_ROOT / video_hash / embedding_type / "embeddings.json"
|
| 133 |
+
|
| 134 |
+
if not target_file.exists():
|
| 135 |
+
raise HTTPException(
|
| 136 |
+
status_code=404,
|
| 137 |
+
detail=f"embeddings.json not found for video={video_hash}, type={embedding_type}"
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
try:
|
| 141 |
+
with open(target_file, "r", encoding="utf-8") as f:
|
| 142 |
+
data = json.load(f)
|
| 143 |
+
except Exception as exc:
|
| 144 |
+
raise HTTPException(status_code=500, detail=f"Failed to read embeddings: {exc}")
|
| 145 |
+
|
| 146 |
+
return data
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
@router.get("/get_embedding", tags=["Embeddings Manager"])
|
| 150 |
+
def get_embeddings(
|
| 151 |
+
video_hash: str = Query(..., description="Hash of the video"),
|
| 152 |
+
embedding_type: str = Query(..., description="faces or voices"),
|
| 153 |
+
token: str = Query(..., description="Token required for authorization")
|
| 154 |
+
):
|
| 155 |
+
"""
|
| 156 |
+
Endpoint to retrieve embeddings.json for a given video hash and type.
|
| 157 |
+
"""
|
| 158 |
+
validate_token(token)
|
| 159 |
+
|
| 160 |
+
data = get_embeddings_json(video_hash, embedding_type)
|
| 161 |
+
|
| 162 |
+
return data
|