Files
Smartwave/cloud/safety_checker.py
T
Ninluc 1f53e7e6c3
Build, push image, and notify Watchtower / build-image (push) Successful in 34s
Build, push image, and notify Watchtower / notify (push) Successful in 15s
Better prompt again ?
2026-08-23 16:51:27 +02:00

55 lines
2.4 KiB
Python

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'])."
)
spoon_or_utensil_present: bool = Field(
description="Set to True if ANY metal, plastic, or wooden spoon, fork, knife, or utensil is present anywhere in or around the container."
)
is_safe: bool = Field(
description="Set to False if spoon_or_utensil_present is True, or if aluminum foil or metal is present. Set to True ONLY if pure food/bowl with zero utensils."
)
detected_hazards: list[str] = Field(
description="List ONLY physical hazard items found (e.g., ['metal 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 = (
"You are an expert microwave safety quality inspector analyzing a top-down camera frame.\n\n"
"INSPECTION STEPS:\n"
"1. Carefully examine the perimeter, edges, and inner cavity of the bowl/container.\n"
"2. Identify any utensil handles (spoons, forks, knives) protruding from or resting in the dish, regardless of shadow or reflectivity.\n"
"3. List all visible items in `visible_objects` first.\n"
"4. If a utensil or metal object is present anywhere, `spoon_or_utensil_present` MUST be True and `is_safe` MUST be False.\n"
"5. `is_safe` is True ONLY if the container holds strictly food with no utensils or metal."
)
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)}")