|
|
from fastapi import FastAPI, UploadFile, File, HTTPException |
|
|
from fastapi.middleware.cors import CORSMiddleware |
|
|
from PIL import Image |
|
|
import io |
|
|
from transformers import pipeline |
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
|
|
|
app.add_middleware( |
|
|
CORSMiddleware, |
|
|
allow_origins=["*"], |
|
|
allow_credentials=True, |
|
|
allow_methods=["*"], |
|
|
allow_headers=["*"], |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("Loading AI Model...") |
|
|
classifier = pipeline("image-classification", model="Organika/sdxl-detector") |
|
|
print("Model Loaded!") |
|
|
|
|
|
@app.post("/analyze") |
|
|
async def analyze_media(file: UploadFile = File(...)): |
|
|
try: |
|
|
contents = await file.read() |
|
|
image = Image.open(io.BytesIO(contents)) |
|
|
|
|
|
|
|
|
results = classifier(image) |
|
|
|
|
|
|
|
|
|
|
|
ai_score = 0.0 |
|
|
for r in results: |
|
|
label = r['label'].lower() |
|
|
if 'artificial' in label or 'ai' in label: |
|
|
ai_score = r['score'] |
|
|
break |
|
|
if 'human' in label or 'real' in label: |
|
|
|
|
|
ai_score = 1.0 - r['score'] |
|
|
|
|
|
is_ai = ai_score > 0.5 |
|
|
|
|
|
return { |
|
|
"filename": file.filename, |
|
|
"is_ai": is_ai, |
|
|
"confidence": round(ai_score * 100, 2), |
|
|
"details": results |
|
|
} |
|
|
except Exception as e: |
|
|
print(f"Error: {e}") |
|
|
raise HTTPException(status_code=500, detail=str(e)) |
|
|
|
|
|
@app.get("/") |
|
|
def read_root(): |
|
|
return {"status": "AI Detector Neural Network is Running"} |
|
|
|