49 lines
2.1 KiB
Python
49 lines
2.1 KiB
Python
import json
|
|
from pydantic import BaseModel, Field
|
|
from APIs.aichat import generate
|
|
import shared.config as config
|
|
|
|
class DishSafetyResult(BaseModel):
|
|
is_safe: bool = Field(
|
|
description="Set to True if the dish contains ONLY normal food, rice, stew, and ceramic/glass/plastic bowls. Set to False ONLY if shiny metallic cutlery (spoon/fork/knife) or metallic foil is present."
|
|
)
|
|
detected_hazards: list[str] = Field(
|
|
description="List ONLY physical metal objects found (e.g., ['metal spoon']). If is_safe is True, this MUST be an empty list []."
|
|
)
|
|
warning_message: str = Field(
|
|
description="Clear warning if is_safe is False, otherwise empty string ''."
|
|
)
|
|
|
|
def check_dish_safety(image_path: str) -> dict:
|
|
prompt = (
|
|
"You are a microwave safety vision inspector.\n\n"
|
|
"TASK: Check if this dish contains any REAL METAL CUTLERY (metal spoons, forks, knives) or ALUMINUM FOIL.\n\n"
|
|
"CRITICAL RULES:\n"
|
|
"1. Rice, curry, stew, potatoes, herbs, dark sauce, and ceramic/plastic/glass bowls are SAFE food items.\n"
|
|
"2. Camera image noise, shadows, and food textures are NOT metal objects.\n"
|
|
"3. Unless a shiny, metallic silver/gold utensil or foil sheet is clearly visible, mark is_safe = True and detected_hazards = [].\n"
|
|
"4. Do NOT invent or guess utensils if none are clearly visible."
|
|
)
|
|
|
|
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)}") |