import json from pydantic import BaseModel, Field from APIs.aichat import generate import shared.config as config class DishSafetyResult(BaseModel): visible_objects: list[str] = Field( description="List EVERY distinct item visible in the frame (e.g., ['ceramic bowl', 'rice', 'metal spoon handle', 'stew'])." ) is_safe: bool = Field( description="Is the microwave dish safe to microwave? True if safe, False if unsafe." ) detected_hazards: list[str] = Field( description="List ONLY physical hazard items found. Empty list [] if safe." ) warning_message: str = Field( description="Warning statement if unsafe, otherwise empty string ''." ) def check_dish_safety(image_path: str) -> dict: prompt = ( "You are an expert microwave safety quality inspector analyzing a top-down camera frame.\n\n" "INSPECTION STEPS:\n" "Describe what you see in the image, identify any viewable hazards, and determine if the dish is safe to microwave.\n" "Alert only if the cooking of the dish **will cause damage** to the microwave or the dish itself.\n" "NOTES:\n" "The camera focus is wrongly setup, so the image may be blurry. Please do not take the blurriness or lack of view into a hazardn" ) response_raw = generate( prompt=prompt, images=[image_path], output_format=DishSafetyResult, should_think=False, ) print(f"[Debug] Raw response from AI generator: {response_raw}") if isinstance(response_raw, str): try: validated_result = DishSafetyResult.model_validate_json(response_raw) return validated_result.model_dump() except Exception: return json.loads(response_raw) if isinstance(response_raw, DishSafetyResult): return response_raw.model_dump() if isinstance(response_raw, dict): return response_raw raise ValueError(f"Unexpected response type from AI generator: {type(response_raw)}")