import asyncio import logging from app.core.config import settings from app.service.sketch2garment.callback import notify_callback import httpx from app.service.sketch2garment.celery_app import celery_app logger = logging.getLogger(__name__) @celery_app.task(bind=True, queue="sketch_to_garment_queue", max_retries=3, name='app.service.sketch2garment.tasks.sketch_to_garment') def sketch_to_garment(self, task_id: str, callback_url: str, bucket_name: str, input_image_path: str, user_id: str, category: str = None): payload = { "bucket_name": bucket_name, "category": category or settings.DEFAULT_CATEGORY, "input_image_path": input_image_path, "user_id": user_id } logger.info(f"payload: {payload}") try: with httpx.Client(timeout=300.0) as client: # 注意这里用 AsyncClient 配合 Celery # 如果你的 LitServe 是同步 endpoint,也可以用 httpx.Client() response = client.post(settings.SKETCH_TO_GARMENT_URL, json=payload) if response.status_code == 200: result = response.json() result_json = { "pattern": result[1], "texture": result[2], "glb": result[3], "texture_fabric": result[4] } asyncio.run( notify_callback(callback_url=callback_url, task_id=task_id, result=result_json, status="success") ) else: asyncio.run( notify_callback( callback_url=callback_url, task_id=task_id, result={ "status": "fail", "task_id": task_id, "message": "fail", "error": "fail" }, status="fail") ) except Exception as e: return { "status": "failed", "task_id": task_id, "input": payload, "error": str(e) }