diff --git a/cloud/safety_checker.py b/cloud/safety_checker.py index c3d388e..f2c5a47 100644 --- a/cloud/safety_checker.py +++ b/cloud/safety_checker.py @@ -4,45 +4,44 @@ from APIs.aichat import generate import shared.config as config -# Output schema as a Pydantic model +# 1. Output schema with strict anti-hallucination descriptions class DishSafetyResult(BaseModel): visible_objects: list[str] = Field( - description="List all distinct physical objects visible in or around the dish (e.g., bowl, plastic tray, liquid, spoon, cover)." + description="Objective list of physical items visible (e.g., 'black plastic tray', 'stew', 'plastic fork'). Do NOT categorize them as safe or hazards here." ) material_analysis: str = Field( - description="Analyze the physical material of each visible object (e.g., ceramic, polypropylene plastic, stainless steel, aluminum foil)." + description="Describe physical materials based strictly on visual cues. Note: Smooth, rigid black plastic meal trays are Polypropylene/CPET plastic (safe), NOT foam/styrofoam." ) detected_hazards: list[str] = Field( description=( - "List ONLY explicit, high-risk microwave hazards: metal items, cutlery, aluminum foil, " - "metallic trim/gilding, Styrofoam (expanded polystyrene), or tightly sealed/unvented foil lids. " - "Do NOT include standard plastic containers, TV dinner trays, polypropylene (#5), or Tupperware as hazards. Empty if none." + "List ONLY explicit physical hazards: metal utensils/cutlery, aluminum foil/wrappers, " + "metallic foil trim, spongy white foam/styrofoam containers, or airtight unvented plastic wrap. " + "Rigid black or clear plastic meal trays are NOT hazards. Empty if none." ) ) is_safe: bool = Field( - description="Must be set to True if detected_hazards is empty. Otherwise False." + description="Set to True if detected_hazards is completely empty. Otherwise set to False." ) warning_message: str = Field( - description="One short sentence explaining the hazard if detected_hazards is not empty, otherwise an empty string." + description="One brief sentence warning if detected_hazards is non-empty, otherwise an empty string." ) def check_dish_safety(image_path: str) -> dict: prompt = ( - "Examine this photo of a meal intended for microwave heating.\n\n" - "Inspect all objects to determine if any CRITICAL microwave hazards exist.\n\n" - "STRICT HAZARDS TO DETECT:\n" - "- Metal utensils, cutlery, or metal objects.\n" - "- Aluminum foil, metallic packaging, or decorative metallic trim.\n" - "- Expanded Polystyrene / Styrofoam containers.\n" - "- Completely sealed non-vented foil or plastic film lids (explosion risk).\n\n" - "SAFE MATERIALS (DO NOT FLAG AS HAZARDS):\n" - "- Standard microwavable food containers, plastic meal prep trays, black plastic TV dinner trays, and polypropylene (PP / #5) plastics.\n" - "- Glass, ceramic, or paper containers.\n\n" - "Only mark is_safe as False if a clear, high-risk hazard from the strictly dangerous list above is present." + "Analyze this food image for critical microwave safety hazards.\n\n" + "MATERIAL IDENTIFICATION RULES:\n" + "- Black plastic meal trays / TV dinner containers = Smooth, rigid Polypropylene (PP) or CPET plastic. These are STANDARD MICROWAVE MEAL TRAYS and are SAFE.\n" + "- Styrofoam / EPS = White or light-colored spongy, cellular foam. (Do NOT misclassify smooth black plastic as styrofoam!).\n\n" + "STRICT HAZARDS TO LOOK FOR:\n" + "1. Metal objects (metal forks, spoons, knives, metal bowls).\n" + "2. Aluminum foil or foil packaging.\n" + "3. Metallic gold/silver decorative trim on ceramic dishes.\n" + "4. White spongy foam / Styrofoam packaging.\n" + "5. Tightly sealed, unvented plastic film or foil lids.\n\n" + "If the meal is simply inside a standard black plastic tray or ceramic/glass dish without any metal/foil/foam present, flag it as SAFE (is_safe = True)." ) - # 2. Pass the Pydantic class directly to generate() response_raw = generate( prompt=prompt, images=[image_path], @@ -51,14 +50,11 @@ def check_dish_safety(image_path: str) -> dict: ) print(f"[Debug] Raw response from AI generator: {response_raw}") - # 3. Handle response parsing if isinstance(response_raw, str): - # Parse and validate the JSON string into the Pydantic model, then return as a dict try: validated_result = DishSafetyResult.model_validate_json(response_raw) return validated_result.model_dump() except Exception: - # Fallback to standard json.loads if raw parsing is needed return json.loads(response_raw) if isinstance(response_raw, DishSafetyResult):