test4.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # encoding: utf-8
  2. """
  3. @author: luojunhui
  4. """
  5. import json
  6. import requests
  7. from applications.ai import tencent_ai
  8. def get_score_list(
  9. account_nickname_list,
  10. text_list,
  11. rate=0.1,
  12. min_time=None,
  13. max_time=None,
  14. interest_type="by_avg",
  15. sim_type="mean",
  16. keys=[
  17. "Title",
  18. "show_view_count",
  19. ],
  20. ):
  21. api_url = 'http://192.168.100.31:8179/score_list'
  22. payload = json.dumps({
  23. "account_nickname_list": account_nickname_list,
  24. "text_list": text_list,
  25. "max_time": max_time,
  26. "min_time": min_time,
  27. "interest_type": interest_type,
  28. "sim_type": sim_type,
  29. "rate": rate,
  30. })
  31. res = requests.request("POST", api_url, headers={}, data=payload).json()
  32. return res
  33. def send_to_aigc(task_name, obj):
  34. """
  35. 通过video_id获取文本
  36. :param
  37. :return:
  38. """
  39. url = "http://47.99.132.47:8888/publish"
  40. body = {
  41. "task_name": task_name,
  42. "article_list": [obj, obj, obj, obj]
  43. }
  44. header = {
  45. "Content-Type": "application/json"
  46. }
  47. response = requests.post(url, json=body, headers=header, timeout=500)
  48. return response.json()
  49. class AutoMatchMain:
  50. """
  51. auto match
  52. 47.99.132.47
  53. """
  54. ip = "47.99.132.47"
  55. @classmethod
  56. def get_video_list(cls, start_dt, end_dt):
  57. """
  58. 获取视频list
  59. """
  60. url = f"http://{cls.ip}:8888/videos"
  61. body = {
  62. "cate": "video_return",
  63. "start_date": start_dt,
  64. "end_date": end_dt,
  65. "topN": 2000
  66. }
  67. header = {
  68. "Content-Type": "application/json",
  69. }
  70. response = requests.post(url, json=body, headers=header, timeout=600)
  71. # print(json.dumps(response.json(), ensure_ascii=False, indent=4))
  72. return response.json()
  73. @classmethod
  74. def match_account(cls, account_list, article_list):
  75. """
  76. 匹配账号
  77. :param account_list:
  78. :param article_list:
  79. :return:
  80. """
  81. url = f"http://{cls.ip}:8888/match"
  82. body = {
  83. "accountList": account_list,
  84. "textList": article_list
  85. }
  86. header = {
  87. "Content-Type": "application/json"
  88. }
  89. response = requests.post(url, json=body, headers=header, timeout=500)
  90. print(response.text)
  91. return response.json()
  92. @classmethod
  93. def ask_whisper(cls, video_id, title):
  94. """
  95. :param title:
  96. :param video_id:
  97. :return:
  98. """
  99. url = f"http://{cls.ip}:8888/whisper"
  100. body = {
  101. "vid": video_id,
  102. "title": title
  103. }
  104. header = {
  105. "Content-Type": "application/json"
  106. }
  107. response = requests.post(url, json=body, headers=header, timeout=500)
  108. return response.json()
  109. @classmethod
  110. def get_text(cls, video_id):
  111. """
  112. 通过video_id获取文本
  113. :param video_id:
  114. :return:
  115. """
  116. url = f"http://{cls.ip}:8888/get_text"
  117. body = {
  118. "vid": video_id
  119. }
  120. header = {
  121. "Content-Type": "application/json"
  122. }
  123. response = requests.post(url, json=body, headers=header, timeout=500)
  124. return response.json()
  125. @classmethod
  126. def process(cls):
  127. """
  128. 处理
  129. :return:
  130. """
  131. video_list = cls.get_video_list(start_dt="2024-05-01", end_dt="2024-05-30")
  132. # 去重
  133. result = {}
  134. for video in video_list['data']:
  135. vid = video['video_id']
  136. if result.get(vid):
  137. continue
  138. else:
  139. result[vid] = video
  140. # 去重后的title_dict
  141. title_dict = {}
  142. for i in result:
  143. title_dict[result[i]['title']] = i
  144. title_list = [title for title in title_dict]
  145. account_list = ['生活良读'] # '票圈最新消息', '老友欢聚地'
  146. print("开始匹配账号")
  147. detail_score_obj = get_score_list(account_list, title_list)
  148. print("账号匹配完成")
  149. L = []
  150. for key in detail_score_obj:
  151. each_account = detail_score_obj[key]
  152. value_list = list(zip(title_list, each_account['score_list'], each_account['text_list_max']))
  153. top_5 = sorted(value_list, key=lambda x: x[1], reverse=True)[:5]
  154. for item in top_5:
  155. ort_title = item[0]
  156. generate_title = item[2]
  157. video_id = title_dict[ort_title]
  158. cls.ask_whisper(video_id=video_id, title=ort_title)
  159. video_text = cls.get_text(video_id=video_id)['text']
  160. video_url = result[video_id]['video_url']
  161. obj = {
  162. "account": key,
  163. "ori_title": ort_title,
  164. "generate_title": generate_title,
  165. "video_id": video_id,
  166. "video_text": video_text,
  167. "video_url": video_url
  168. }
  169. L.append(obj)
  170. return L
  171. if __name__ == '__main__':
  172. AM = AutoMatchMain()
  173. target_list = AM.process()
  174. for index, i in enumerate(target_list):
  175. # print(json.dumps(i, ensure_ascii=False, indent=4))
  176. m_text = i['video_text']
  177. title = i['ori_title']
  178. video_id = i['video_id']
  179. prompt = f"通过这个标题({title}) 和这些文本({m_text}), 生成一篇1000字以上的文章"
  180. ai_text = tencent_ai(prompt=prompt)
  181. ppp = {
  182. "title": i['ori_title'],
  183. "video_id": i['video_id'],
  184. "img_list": [],
  185. "cover": "",
  186. "text": ai_text
  187. }
  188. r = send_to_aigc("test_upload_by_luojunhui---{}".format(index), ppp)
  189. print(r)
  190. print(json.dumps(i, ensure_ascii=False, indent=4))