| from fastapi import FastAPI
|
| from transformers import pipeline
|
| from PIL import Image
|
| import requests
|
| from io import BytesIO
|
|
|
| app = FastAPI()
|
|
|
|
|
| classifier = pipeline("image-classification", model="google/vit-base-patch16-224")
|
|
|
| @app.get("/")
|
| def read_root():
|
| return {"status": "✦ FFX ✦ AI Server is Running!"}
|
|
|
| @app.get("/analyze")
|
| def analyze(url: str):
|
| try:
|
|
|
| headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64 AppleWebKit/537.36)"}
|
| res = requests.get(url, headers=headers, timeout=15)
|
| img = Image.open(BytesIO(res.content)).convert("RGB")
|
|
|
|
|
| results = classifier(img)
|
| top_score = results[0]['score']
|
|
|
| if top_score >= 0.85:
|
| return {"success": True, "confidence": f"High ({int(top_score*100)}%)", "names": [results[0]['label'].title()]}
|
| else:
|
| top_3 = [r['label'].title() for r in results[:3]]
|
| return {"success": True, "confidence": "Low", "names": top_3}
|
|
|
| except Exception as e:
|
| return {"success": False, "error": str(e)} |