aigc_decode_server.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from typing import Dict, List
  2. from app.infra.shared import AsyncHttpClient
  3. class AigcDecodeServer:
  4. base_url: str = "https://aigc-api.aiddit.com"
  5. @staticmethod
  6. def _sanitize_images(posts: List[Dict]) -> List[Dict]:
  7. for post in posts:
  8. images = post.get("images")
  9. if images:
  10. post["images"] = [
  11. img for img in images if img and img.startswith(("http://", "https://"))
  12. ]
  13. return posts
  14. async def submit_decode(
  15. self, config_id: int, posts: List[Dict], skip_completed: bool = False
  16. ) -> Dict:
  17. """批量提交帖子解构
  18. POST /aigc/api/task/decode
  19. """
  20. url = f"{self.base_url}/aigc/api/task/decode"
  21. headers = {"Content-Type": "application/json"}
  22. payload = {
  23. "params": {
  24. "configId": config_id,
  25. "skipCompleted": skip_completed,
  26. "posts": self._sanitize_images(posts),
  27. }
  28. }
  29. async with AsyncHttpClient() as client:
  30. return await client.post(url, json=payload, headers=headers)
  31. async def query_decode_results(
  32. self, config_id: int, channel_content_ids: List[str]
  33. ) -> Dict:
  34. """批量查询解构结果
  35. POST /aigc/api/task/decode/result
  36. """
  37. url = f"{self.base_url}/aigc/api/task/decode/result"
  38. headers = {"Content-Type": "application/json"}
  39. payload = {
  40. "params": {"configId": config_id, "channelContentIds": channel_content_ids}
  41. }
  42. async with AsyncHttpClient() as client:
  43. return await client.post(url, json=payload, headers=headers)
  44. async def cancel_decode_tasks(
  45. self, config_id: int, channel_content_ids: List[str]
  46. ) -> Dict:
  47. """取消待执行解构任务
  48. POST /aigc/api/task/decode/cancel
  49. """
  50. url = f"{self.base_url}/aigc/api/task/decode/cancel"
  51. headers = {"Content-Type": "application/json"}
  52. payload = {
  53. "params": {"configId": config_id, "channelContentIds": channel_content_ids}
  54. }
  55. async with AsyncHttpClient() as client:
  56. return await client.post(url, json=payload, headers=headers)
  57. __all__ = ["AigcDecodeServer"]