import io import time from PIL import Image from fastapi import FastAPI, UploadFile, File from fastapi.middleware.cors import CORSMiddleware from transformers import pipeline import uvicorn app = FastAPI() # Enable CORS so frontend on Vercel can access backend app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Load model once at startup detector = pipeline( "image-classification", model="google/vit-base-patch16-224" ) @app.get("/") def home(): return {"message": "DeepSentry backend running"} @app.post("/detect/image") async def detect_image(file: UploadFile = File(...)): start = time.time() contents = await file.read() image = Image.open(io.BytesIO(contents)).convert("RGB") result = detector(image)[0] label = result["label"] confidence = round(result["score"] * 100, 2) elapsed = int((time.time() - start) * 1000) verdict = "Real" if "real" in label.lower() else "Fake" return { "label": verdict, "confidence": confidence, "media_type": "image", "elapsed_ms": elapsed } @app.post("/detect/video") async def detect_video(file: UploadFile = File(...)): start = time.time() # placeholder video detection elapsed = int((time.time() - start) * 1000) return { "label": "Real", "confidence": 50.0, "media_type": "video", "elapsed_ms": elapsed } if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=7860)