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), }