Spaces:
No application file
No application file
Download main.py from underbroad/deepfake-detector-api: direct link, hf CLI and curl.
- Browser
- Download file 1.19 kB
-
https://huggingface.co/spaces/underbroad/deepfake-detector-api/resolve/main/main.py
- Command line
-
hf download hf://spaces/underbroad/deepfake-detector-api/main.py
-
curl -L -o main.py https://huggingface.co/spaces/underbroad/deepfake-detector-api/resolve/main/main.py
1.19 kB
| from fastapi import FastAPI, File, UploadFile, HTTPException | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from transformers import pipeline | |
| from PIL import Image | |
| import torch, io | |
| app = FastAPI() | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| print("Loading model...") | |
| detector = pipeline( | |
| "image-classification", | |
| model="Organika/sdxl-detector", | |
| device=-1, | |
| ) | |
| print("Model ready.") | |
| def root(): | |
| return {"status": "ok"} | |
| async def detect_image(file: UploadFile = File(...)): | |
| if not file.content_type.startswith("image/"): | |
| raise HTTPException(400, "Must be an image file") | |
| data = await file.read() | |
| img = Image.open(io.BytesIO(data)).convert("RGB") | |
| results = detector(img) | |
| scores = {r["label"].lower(): r["score"] for r in results} | |
| ai = scores.get("artificial", scores.get("fake", 0)) | |
| real = 1 - ai | |
| return { | |
| "verdict": "AI-Generated" if ai > real else "Likely Real", | |
| "ai_score": round(ai * 100, 1), | |
| "real_score": round(real * 100, 1), | |
| "confidence": round(max(ai, real) * 100, 1), | |
| } | |