from fastapi import FastAPI, File, UploadFile, Form from fastapi.responses import HTMLResponse from model import predict, get_advice from io import BytesIO from PIL import Image import base64 app = FastAPI() @app.get("/", response_class=HTMLResponse) async def index(): html_content = """ Détection de Race

Détection de Race à partir de l'Image





Aperçu de l'image
""" return HTMLResponse(content=html_content) @app.post("/predict/") async def predict_race(file: UploadFile = File(...)): image_data = await file.read() image = Image.open(BytesIO(image_data)).convert("RGB") # Encode image to base64 buffered = BytesIO() image.save(buffered, format="JPEG") img_str = base64.b64encode(buffered.getvalue()).decode("utf-8") label, confidence = predict(image) advice = get_advice(label) temperament = taille = activite = esperance = "" if isinstance(advice, str): lines = advice.split("\n") for line in lines: line_lower = line.lower() if line_lower.startswith("tempérament") or line_lower.startswith("temperament"): temperament = line elif line_lower.startswith("taille"): taille = line elif line_lower.startswith("activité") or line_lower.startswith("activity"): activite = line elif line_lower.startswith("espérance") or line_lower.startswith("life"): esperance = line advice = "" else: advice = "Aucune information disponible." return HTMLResponse(content=f""" Résultats

Résultat de la Détection

Race détectée : {label}

Confiance : {confidence * 100:.2f}%

Fiche descriptive :

{advice}

{temperament}

{taille}

{activite}

{esperance}

Image Uploadée
🔙 Retour
""")