Files
Smartwave/cloud/safety_checker.py
T
Ninluc 7c35b52dbd
Build, push image, and notify Watchtower / build-image (push) Successful in 38s
Build, push image, and notify Watchtower / notify (push) Successful in 11s
Precision in prompt + better camera
2026-08-23 16:48:30 +02:00

49 lines
2.3 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 strict microwave safety vision inspector.\n\n"
"TASK: Inspect the top-down photo of the dish for ANY metal or plastic utensils (spoons, forks, knives, metal handles) or aluminum foil.\n\n"
"CRITICAL RULES:\n"
"1. Look closely at the edges and interior of the bowl for utensil handles sticking out, even if they appear dark or shadowed.\n"
"2. If you see ANY spoon handle, fork, knife, or utensil body—whether shiny silver, grey, or dark metal—mark is_safe = False.\n"
"3. ONLY mark is_safe = True if the bowl contains 100% food and rice with NO protruding utensils or foreign objects.\n"
"4. If a spoon or utensil is detected, set detected_hazards = ['metal spoon'] and warning_message = 'Please remove the metal utensil before microwaving.'"
)
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)}")