52 lines
2.0 KiB
Python
52 lines
2.0 KiB
Python
import json
|
|
from pydantic import BaseModel, Field
|
|
from APIs.aichat import generate
|
|
import shared.config as config
|
|
|
|
class DishSafetyResult(BaseModel):
|
|
utensil_or_foreign_object_detected: bool = Field(
|
|
description="Set to True if ANY spoon, fork, knife, handle, or foil is inside or touching the bowl. Set to False ONLY if the bowl contains ONLY 100% food."
|
|
)
|
|
is_safe: bool = Field(
|
|
description="MUST be False if utensil_or_foreign_object_detected is True. Set to True ONLY if pure food/bowl with ZERO utensils."
|
|
)
|
|
detected_hazards: list[str] = Field(
|
|
description="List any items found (e.g., ['spoon']). 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 = (
|
|
"Inspect this top-down photo of a food dish inside a microwave.\n\n"
|
|
"STRICT MICROWAVE RULES:\n"
|
|
"1. Check if ANY utensil (spoon, fork, knife, or handle) is present in or on the bowl.\n"
|
|
"2. If ANY spoon or utensil is present (metal, plastic, or ceramic), you MUST set `utensil_or_foreign_object_detected = True` and `is_safe = False`.\n"
|
|
"3. `is_safe` can ONLY be True if the dish contains strictly food and nothing else."
|
|
)
|
|
|
|
response_raw = generate(
|
|
prompt=prompt,
|
|
images=[image_path],
|
|
output_format=DishSafetyResult,
|
|
should_think=False,
|
|
)
|
|
|
|
if config.DEBUG_MESSAGES:
|
|
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)}") |