Files
Smartwave/cloud/safety_checker.py
T
Ninluc 36abfc8234
Build, push image, and notify Watchtower / build-image (push) Successful in 46s
Build, push image, and notify Watchtower / notify (push) Successful in 18s
Better prompt ?
2026-08-15 18:50:54 +02:00

57 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):
visible_objects: list[str] = Field(
description="List physical container items and utensils visible in the image."
)
material_analysis: str = Field(
description="Brief description of container material (e.g., standard plastic tray, glass bowl)."
)
detected_hazards: list[str] = Field(
description="List of visually confirmed metal or foil hazards. MUST be empty [] if no metal or foil is present."
)
is_safe: bool = Field(
description="Set to True if detected_hazards is empty. Set to False ONLY if metal or aluminum foil is present."
)
warning_message: str = Field(
description="Short warning if is_safe is False, otherwise an empty string."
)
def check_dish_safety(image_path: str) -> dict:
prompt = (
"Analyze this food image for microwave safety.\n\n"
"DEFAULT ASSUMPTION:\n"
"- Assume the dish is SAFE (is_safe = True, detected_hazards = []).\n"
"- Standard food (meat, vegetables, potatoes) and standard containers (black plastic meal trays, plastic bowls, ceramic, glass) are 100% SAFE for microwaves.\n\n"
"STRICT HAZARD RULE:\n"
"- Flag as UNSAFE ONLY if you can literally see actual metal cutlery (metal spoon/fork/knife), aluminum foil packaging, or metal foil trim in the image.\n"
"- Do NOT invent or hallucinate hazards. If you do not see shiny metal or aluminum foil, detected_hazards MUST be an empty list []."
)
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)}")