import json from APIs.aichat import generate DISH_SAFETY_SCHEMA = { "type": "object", "properties": { "is_safe": { "type": "boolean", "description": "True if no microwave hazards (metal, foil, sealed packaging) are present." }, "warning_message": { "type": "string", "description": "Explanation of any hazard found, or an empty string if safe." }, "detected_hazards": { "type": "array", "items": { "type": "string" }, "description": "List of specific hazard items detected." } }, "required": ["is_safe", "warning_message", "detected_hazards"] } def check_dish_safety(image_path: str) -> dict: prompt = ( "Analyze this top-down photo of a dish prepared for microwave cooking. " "Inspect the area for metal utensils, aluminum foil, metallic dish patterns, or unvented plastic wraps." ) # Pass the schema directly to the generation call response_raw = generate( prompt=prompt, images=[image_path], output_format=DISH_SAFETY_SCHEMA # Passed as Ollama's `format` parameter ) print(f"[Debug] Raw response from AI generator: {response_raw}") # 1. If response_raw is already a dict, return it directly if isinstance(response_raw, dict): return response_raw # 2. If it's a string or bytes, parse it if isinstance(response_raw, (str, bytes, bytearray)): return json.loads(response_raw) raise ValueError(f"Unexpected response type from AI generator: {type(response_raw)}")