52 lines
1.9 KiB
Python
52 lines
1.9 KiB
Python
import json
|
|
from pydantic import BaseModel, Field
|
|
from APIs.aichat import generate
|
|
import shared.config as config
|
|
|
|
class DishSafetyResult(BaseModel):
|
|
image_description: str = Field(
|
|
description="Describe the food and container in 1 simple sentence."
|
|
)
|
|
is_safe: bool = Field(
|
|
description="Set to True if safe. Set to False ONLY if a physical metallic utensil or aluminum foil is clearly visible."
|
|
)
|
|
detected_hazards: list[str] = Field(
|
|
description="List any metallic objects found (e.g. ['metal utensil']). If is_safe is True, this MUST be empty []."
|
|
)
|
|
warning_message: str = Field(
|
|
description="Short warning if unsafe, otherwise empty string ''."
|
|
)
|
|
|
|
def check_dish_safety(image_path: str) -> dict:
|
|
prompt = (
|
|
"Analyze this top-down photo inside a microwave.\n\n"
|
|
"TASK:\n"
|
|
"1. Describe what is visible in `image_description`.\n"
|
|
"2. If the container holds strictly food with no physical metal objects, set `is_safe` to True and `detected_hazards` to [].\n"
|
|
"3. Mark `is_safe` as False ONLY if an actual metal object or metallic handle is present in the container."
|
|
)
|
|
|
|
response_raw = generate(
|
|
prompt=prompt,
|
|
images=[image_path],
|
|
output_format=DishSafetyResult,
|
|
should_think=False,
|
|
)
|
|
|
|
if config.DEBUG_MESSAGES:
|
|
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)}") |