_utils.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import json
  2. from typing import Dict, List
  3. from app.infra.internal import DecodeServer
  4. from ._const import DecodeTaskConst
  5. class DecodeTaskUtil(DecodeTaskConst):
  6. decode_server = DecodeServer()
  7. def prepare_extract_body(self, article: Dict) -> Dict:
  8. return {
  9. "scene": self.BusinessScene.POINT_PICK,
  10. "content_type": self.ContentType.LONG_ARTICLE,
  11. "content": {
  12. "channel_content_id": article.get("wx_sn", ""),
  13. "video_url": "",
  14. "images": article.get("article_images"),
  15. "body_text": article.get("article_text", ""),
  16. "title": article.get("article_title", ""),
  17. "channel_account_id": article.get("gh_id", ""),
  18. "channel_account_name": article.get("account_name", ""),
  19. },
  20. }
  21. @staticmethod
  22. def extract_decode_result(result: Dict) -> Dict:
  23. """
  24. 从结构的结果中,解析出灵感点、目的点、关键点;
  25. """
  26. final_result = result.get("final_normalization_rebuild")
  27. if not final_result:
  28. return {"error": "解构结果中无 final_normalization_rebuild 信息"}
  29. # 灵感点
  30. inspiration_list = final_result.get("inspiration_final_result", {}).get(
  31. "最终灵感点列表", []
  32. )
  33. # 目的
  34. purpose_list = final_result.get("purpose_final_result", {}).get(
  35. "最终目的点列表", []
  36. )
  37. # 关键点
  38. keypoint_list = final_result.get("keypoint_final", {}).get("最终关键点列表", [])
  39. topic_fusion = final_result.get("topic_fusion_result", {})
  40. # 选题
  41. topic_text = (
  42. topic_fusion.get("最终选题", {}).get("选题", "")
  43. if isinstance(topic_fusion.get("最终选题"), dict)
  44. else ""
  45. )
  46. def _join_points(items: list, key: str) -> str:
  47. parts = [str(p[key]) for p in items if isinstance(p, dict) and p.get(key)]
  48. return ",".join(parts)
  49. return {
  50. "inspiration": _join_points(inspiration_list, "灵感点"),
  51. "purpose": _join_points(purpose_list, "目的点"),
  52. "key_point": _join_points(keypoint_list, "关键点"),
  53. "topic": topic_text,
  54. }
  55. async def fetch_decode_result(self, task_id: str):
  56. return await self.decode_server.fetch_result(task_id)
  57. class AdPlatformArticlesDecodeUtils(DecodeTaskUtil):
  58. @staticmethod
  59. def format_images(images: str) -> List[str]:
  60. """
  61. 格式化图片字符串,空/非法 JSON 返回空列表。
  62. """
  63. if not images or not images.strip():
  64. return []
  65. try:
  66. image_list = json.loads(images)
  67. except (json.JSONDecodeError, TypeError):
  68. return []
  69. if not isinstance(image_list, list):
  70. return []
  71. return [
  72. i.get("image_url")
  73. for i in image_list
  74. if isinstance(i, dict) and i.get("image_url")
  75. ]
  76. async def create_decode_task(self, article: Dict):
  77. images = self.format_images(article.get("article_images") or "")
  78. article["article_images"] = images
  79. request_body = self.prepare_extract_body(article)
  80. return await self.decode_server.create_decode_task(request_body)
  81. class InnerArticlesDecodeUtils(DecodeTaskUtil):
  82. async def create_decode_task(self, article: Dict, article_produce_info: List[Dict]):
  83. images = [
  84. i["output"]
  85. for i in article_produce_info
  86. if i["produce_module_type"]
  87. in (self.ProduceModuleType.COVER, self.ProduceModuleType.IMAGE)
  88. ]
  89. article["article_images"] = images
  90. text = [
  91. i["output"]
  92. for i in article_produce_info
  93. if i["produce_module_type"] == self.ProduceModuleType.CONTENT
  94. ]
  95. article["article_text"] = "\n".join(text)
  96. request_body = self.prepare_extract_body(article)
  97. return await self.decode_server.create_decode_task(request_body)
  98. __all__ = [
  99. "AdPlatformArticlesDecodeUtils",
  100. "InnerArticlesDecodeUtils",
  101. "DecodeTaskUtil",
  102. ]