File size: 1,188 Bytes
4f394a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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.")

@app.get("/")
def root():
    return {"status": "ok"}

@app.post("/detect/image")
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),
    }