"""Celery tasks for Azure AI validation.""" import logging from app.tasks.celery_app import celery_app logger = logging.getLogger(__name__) @celery_app.task(bind=True, name="app.tasks.ai_tasks.validate_item", queue="ai_validation") def validate_item(self, order_item_id: str): """Validate orientation of a rendered thumbnail via Azure GPT-4o Vision. Loads the order item's tenant config and passes it to validate_thumbnail() so that per-tenant Azure credentials are used when configured. """ logger.info(f"AI validation for item: {order_item_id}") try: from app.config import settings from sqlalchemy import create_engine from sqlalchemy.orm import Session from app.models.order_item import OrderItem from app.services.azure_ai import validate_thumbnail # Load tenant config for this order item tenant_config: dict | None = None try: engine = create_engine(settings.database_url_sync) with Session(engine) as session: item = session.get(OrderItem, __import__("uuid").UUID(order_item_id)) if item and hasattr(item, "order") and item.order and item.order.tenant_id: from app.domains.tenants.models import Tenant tenant = session.get(Tenant, item.order.tenant_id) if tenant: tenant_config = tenant.tenant_config or {} except Exception as exc: logger.warning(f"Could not load tenant config for {order_item_id}: {exc}") validate_thumbnail(order_item_id, tenant_config=tenant_config) except Exception as exc: logger.error(f"AI validation failed for {order_item_id}: {exc}") raise self.retry(exc=exc, countdown=30, max_retries=3)