-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.py
More file actions
51 lines (36 loc) · 1.32 KB
/
Copy pathapi.py
File metadata and controls
51 lines (36 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
import shutil
import tempfile
from fastapi import FastAPI, File, HTTPException, UploadFile
from fastapi.responses import JSONResponse
from DeepfakeDetection.pipeline.prediction import Prediction
app = FastAPI(
title="DeepDetect API",
description="Deepfake Detection API with Grad-CAM support",
version="1.0.0",
)
predictor = Prediction()
@app.get("/")
def health_check():
return {"status": "ok", "message": "DeepDetect API is running"}
@app.post("/predict")
async def predict_video(file: UploadFile = File(...)):
"""
Accepts a video file and returns deepfake prediction + confidence.
"""
if not file.filename.lower().endswith((".mp4", ".avi", ".mov", ".mkv")):
raise HTTPException(status_code=400, detail="Unsupported video format")
# Save uploaded video to temp file
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp:
shutil.copyfileobj(file.file, tmp)
tmp_path = tmp.name
try:
prediction, gradcam_image, class_details = predictor.predict(tmp_path)
response = {
"prediction": prediction,
"class_probabilities": class_details,
}
return JSONResponse(content=response)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
finally:
file.file.close()