Spaces:
Runtime error
Runtime error
Main init
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
import requests
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
import torch
|
| 6 |
+
from transformers import AutoModelForImageClassification, AutoFeatureExtractor
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
# Load the Hugging Face deepfake model
|
| 11 |
+
model_name = "yuvalkirstain/DeepFakeDetection"
|
| 12 |
+
model = AutoModelForImageClassification.from_pretrained(model_name)
|
| 13 |
+
extractor = AutoFeatureExtractor.from_pretrained(model_name)
|
| 14 |
+
|
| 15 |
+
@app.post("/analyze")
|
| 16 |
+
async def analyze(request: Request):
|
| 17 |
+
data = await request.json()
|
| 18 |
+
image_url = data.get("mediaUrl")
|
| 19 |
+
if not image_url:
|
| 20 |
+
return {"error": "Missing 'mediaUrl'"}
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
img_bytes = requests.get(image_url).content
|
| 24 |
+
img = Image.open(BytesIO(img_bytes)).convert("RGB")
|
| 25 |
+
inputs = extractor(images=img, return_tensors="pt")
|
| 26 |
+
with torch.no_grad():
|
| 27 |
+
outputs = model(**inputs)
|
| 28 |
+
scores = torch.nn.functional.softmax(outputs.logits, dim=1)[0]
|
| 29 |
+
confidence, pred_idx = torch.max(scores, dim=0)
|
| 30 |
+
label = model.config.id2label[pred_idx.item()]
|
| 31 |
+
|
| 32 |
+
return {
|
| 33 |
+
"label": label.lower(),
|
| 34 |
+
"score": round(confidence.item(), 3)
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return {"error": str(e)}
|