import os from typing import List import httpx AI_SERVICE_URL = os.environ.get("AI_SERVICE_URL", "http://localhost:8070") # Mapping class_name YOLO → nom commun français (partiel) _NOMS_FR = { "Tomato___healthy": "Tomate (saine)", "Tomato___Early_blight": "Tomate (mildiou précoce)", "Tomato___Late_blight": "Tomate (mildiou tardif)", "Pepper__bell___healthy": "Poivron (sain)", "Apple___healthy": "Pommier (sain)", "Potato___healthy": "Pomme de terre (saine)", "Grape___healthy": "Vigne (saine)", "Corn_(maize)___healthy": "Maïs (sain)", "Strawberry___healthy": "Fraisier (sain)", "Peach___healthy": "Pêcher (sain)", } async def identify(image_bytes: bytes) -> List[dict]: """Appelle l'ai-service interne et retourne les détections YOLO.""" try: async with httpx.AsyncClient(timeout=30.0) as client: resp = await client.post( f"{AI_SERVICE_URL}/detect", files={"file": ("photo.jpg", image_bytes, "image/jpeg")}, ) resp.raise_for_status() data = resp.json() except Exception: return [] results = [] for det in data[:3]: cls = det.get("class_name", "") results.append({ "species": cls.replace("___", " — ").replace("_", " "), "common_name": _NOMS_FR.get(cls, cls.split("___")[0].replace("_", " ")), "confidence": det.get("confidence", 0.0), "image_url": "", }) return results