| from fastapi import FastAPI, File, HTTPException, UploadFile | |
| from fastapi.responses import Response | |
| from modules.api_service import initialize_runtime, swap_face_from_uploads | |
| app = FastAPI(title="Deep Live Cam Face Swap API", version="1.0.0") | |
| def startup_event() -> None: | |
| initialize_runtime() | |
| def health() -> dict[str, str]: | |
| return {"status": "ok"} | |
| async def face_swap( | |
| source_image: UploadFile = File(...), | |
| target_image: UploadFile = File(...), | |
| ) -> Response: | |
| try: | |
| content, media_type = swap_face_from_uploads( | |
| await source_image.read(), | |
| await target_image.read(), | |
| ) | |
| except ValueError as exc: | |
| raise HTTPException(status_code=400, detail=str(exc)) from exc | |
| except Exception as exc: | |
| raise HTTPException(status_code=500, detail=str(exc)) from exc | |
| return Response(content=content, media_type=media_type) | |