Download api.py from Sik0213/shield-v-rtfs-faceswap-baseline: direct link, hf CLI and curl.
- Browser
- Download file 2.14 kB
-
https://huggingface.co/Sik0213/shield-v-rtfs-faceswap-baseline/resolve/main/api.py
- Command line
-
hf download hf://Sik0213/shield-v-rtfs-faceswap-baseline/api.py
-
curl -L -o api.py https://huggingface.co/Sik0213/shield-v-rtfs-faceswap-baseline/resolve/main/api.py
2.14 kB
| """Local REST API. Videos are removed from temporary storage after inference.""" | |
| import tempfile | |
| import threading | |
| from pathlib import Path | |
| import av | |
| import torch | |
| from fastapi import FastAPI, File, HTTPException, UploadFile | |
| from inference import CONFIG, load_model, predict_video | |
| app = FastAPI(title="SHIELD-V RTFS face-swap baseline", version="2026-11-06") | |
| _INFERENCE_LOCK = threading.Lock() | |
| _MAX_UPLOAD_BYTES = 200 * 1024 * 1024 | |
| def health(): | |
| try: | |
| load_model() | |
| status = "ready" | |
| except (RuntimeError, OSError, ImportError): | |
| status = "unavailable" | |
| return { | |
| "status": status, | |
| "cuda_available": torch.cuda.is_available(), | |
| } | |
| def model_information(): | |
| return { | |
| "model_version": CONFIG["model_id"], | |
| "task": CONFIG["task"], | |
| "threshold": CONFIG["threshold"], | |
| "audio_used": False, | |
| "score_is_calibrated_probability": False, | |
| } | |
| def create_prediction(file: UploadFile = File(...)): | |
| temporary_path = None | |
| try: | |
| with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as temporary: | |
| temporary_path = Path(temporary.name) | |
| total = 0 | |
| while chunk := file.file.read(1024 * 1024): | |
| total += len(chunk) | |
| if total > _MAX_UPLOAD_BYTES: | |
| raise HTTPException(status_code=413, detail="Video exceeds 200 MiB") | |
| temporary.write(chunk) | |
| if total == 0: | |
| raise HTTPException(status_code=422, detail="Video is empty") | |
| with _INFERENCE_LOCK: | |
| return predict_video(temporary_path) | |
| except ValueError as exc: | |
| raise HTTPException(status_code=422, detail=str(exc)) from exc | |
| except av.error.FFmpegError as exc: | |
| raise HTTPException(status_code=422, detail="Video decoding failed") from exc | |
| except RuntimeError as exc: | |
| raise HTTPException(status_code=503, detail=str(exc)) from exc | |
| finally: | |
| file.file.close() | |
| if temporary_path is not None: | |
| temporary_path.unlink(missing_ok=True) | |