Files
Smartwave/cloud/safety_checker.py
T
Ninluc 547c2df1c9
Build, push image, and notify Watchtower / build-image (push) Successful in 53s
Build, push image, and notify Watchtower / notify (push) Successful in 14s
Better prompt
2026-08-12 12:15:51 +02:00

57 lines
2.2 KiB
Python

import json
from pydantic import BaseModel, Field
from APIs.aichat import generate
import shared.config as config
# 1. Define the output schema as a Pydantic model
class DishSafetyResult(BaseModel):
visible_objects: list[str] = Field(
description="List all distinct physical objects visible in or around the dish (e.g., bowl, liquid, spoon, cover)."
)
material_analysis: str = Field(
description="Analyze the physical material of each visible object (e.g., ceramic, stainless steel, glass, flexible film)."
)
detected_hazards: list[str] = Field(
description="List only the items from visible_objects made of metal, metallic foil/trim, or sealed plastic. Empty if none."
)
is_safe: bool = Field(
description="Must be set to True ONLY if detected_hazards is empty. Otherwise False."
)
warning_message: str = Field(
description="One short sentence explaining the hazard if detected_hazards is not empty, otherwise an empty string."
)
def check_dish_safety(image_path: str) -> dict:
prompt = (
"Examine this top-down photo of a dish intended for a microwave. "
"Carefully inspect all visible objects and their surface materials to determine if any microwave safety hazards exist."
)
# 2. Pass the Pydantic class directly to generate()
response_raw = generate(
prompt=prompt,
images=[image_path],
output_format=DishSafetyResult,
should_think=False,
)
print(f"[Debug] Raw response from AI generator: {response_raw}")
# 3. Handle response parsing
if isinstance(response_raw, str):
# Parse and validate the JSON string into the Pydantic model, then return as a dict
try:
validated_result = DishSafetyResult.model_validate_json(response_raw)
return validated_result.model_dump()
except Exception:
# Fallback to standard json.loads if raw parsing is needed
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)}")