Files
Smartwave/cloud/safety_checker.py
T
Ninluc 07ce941950
Build, push image, and notify Watchtower / build-image (push) Successful in 47s
Build, push image, and notify Watchtower / notify (push) Successful in 25s
Better prompt
2026-08-15 18:47:46 +02:00

66 lines
3.0 KiB
Python

import json
from pydantic import BaseModel, Field
from APIs.aichat import generate
import shared.config as config
# 1. Output schema with strict anti-hallucination descriptions
class DishSafetyResult(BaseModel):
visible_objects: list[str] = Field(
description="Objective list of physical items visible (e.g., 'black plastic tray', 'stew', 'plastic fork'). Do NOT categorize them as safe or hazards here."
)
material_analysis: str = Field(
description="Describe physical materials based strictly on visual cues. Note: Smooth, rigid black plastic meal trays are Polypropylene/CPET plastic (safe), NOT foam/styrofoam."
)
detected_hazards: list[str] = Field(
description=(
"List ONLY explicit physical hazards: metal utensils/cutlery, aluminum foil/wrappers, "
"metallic foil trim, spongy white foam/styrofoam containers, or airtight unvented plastic wrap. "
"Rigid black or clear plastic meal trays are NOT hazards. Empty if none."
)
)
is_safe: bool = Field(
description="Set to True if detected_hazards is completely empty. Otherwise set to False."
)
warning_message: str = Field(
description="One brief sentence warning if detected_hazards is non-empty, otherwise an empty string."
)
def check_dish_safety(image_path: str) -> dict:
prompt = (
"Analyze this food image for critical microwave safety hazards.\n\n"
"MATERIAL IDENTIFICATION RULES:\n"
"- Black plastic meal trays / TV dinner containers = Smooth, rigid Polypropylene (PP) or CPET plastic. These are STANDARD MICROWAVE MEAL TRAYS and are SAFE.\n"
"- Styrofoam / EPS = White or light-colored spongy, cellular foam. (Do NOT misclassify smooth black plastic as styrofoam!).\n\n"
"STRICT HAZARDS TO LOOK FOR:\n"
"1. Metal objects (metal forks, spoons, knives, metal bowls).\n"
"2. Aluminum foil or foil packaging.\n"
"3. Metallic gold/silver decorative trim on ceramic dishes.\n"
"4. White spongy foam / Styrofoam packaging.\n"
"5. Tightly sealed, unvented plastic film or foil lids.\n\n"
"If the meal is simply inside a standard black plastic tray or ceramic/glass dish without any metal/foil/foam present, flag it as SAFE (is_safe = True)."
)
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)}")