matchArticle_deal.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. # encoding: utf-8
  2. """
  3. @author: luojunhui
  4. """
  5. import json
  6. import time
  7. import requests
  8. from uuid import uuid4
  9. from applications.config import db_config
  10. from applications.functions import whisper
  11. from applications.pipeline import question_fission, search_materials, summary_articles
  12. class MatchArticlesTask(object):
  13. """
  14. 视频匹配文章流程
  15. 流程
  16. 1. 拿视频id,标题等信息匹配账号
  17. 2. 账号匹配成功后,使用 AI Search 获取文章的生产资料
  18. 3. 通过GPT4, 腾讯元宝等AI 优化文章
  19. 4. 生成一篇文章,包含标题,文本,封面, 插图, 以及匹配到到小程序
  20. """
  21. def __init__(self, mysql_client):
  22. """
  23. :param mysql_client mysql服务池
  24. """
  25. self.mysql_client = mysql_client
  26. async def whisper_task(self):
  27. """
  28. 执行定时任务,把库里面的视频转文本
  29. :return:
  30. """
  31. select_sql = f"""SELECT video_id FROM {db_config} WHERE status_code = 0 ORDER BY id ASC limit 10;"""
  32. video_list = await self.mysql_client.select(select_sql)
  33. async def whisper_and_update(video_id, mysql_client):
  34. """
  35. whisper处理视频并且把信息更新到mysql表中
  36. :param video_id:
  37. :param mysql_client:
  38. :return:
  39. """
  40. w_response = whisper(video_id)
  41. print(w_response)
  42. text = w_response['text']
  43. update_sql = f"""
  44. UPDATE {db_config}
  45. SET
  46. video_text = '{text}',
  47. status_code = 1
  48. WHERE video_id = {video_id};
  49. """
  50. await mysql_client.async_insert(sql=update_sql)
  51. for vid in video_list:
  52. await whisper_and_update(video_id=vid[0], mysql_client=self.mysql_client)
  53. async def materials_task(self):
  54. """
  55. 获取task的材料
  56. :return:
  57. """
  58. select_sql = f"""SELECT task_id, video_title, video_text FROM {db_config} WHERE status_code = 1 ORDER BY id ASC limit 10;"""
  59. task_list = await self.mysql_client.select(select_sql)
  60. async def find_material(task_tuple, mysql_client):
  61. task_id, title, text = task_tuple
  62. # 先用视频标题作为query, 后续可逐步优化
  63. question_dict = question_fission(title)
  64. material_list = []
  65. for question_key in question_dict:
  66. material = search_materials(question=question_dict[question_key])
  67. material_list.append(material)
  68. material_result = json.dumps(material_list, ensure_ascii=False)
  69. update_sql = f"""
  70. UPDATE {db_config}
  71. SET materials = '{material_result}', status_code = 2
  72. WHERE task_id = '{task_id}'
  73. """
  74. await mysql_client.async_insert(sql=update_sql)
  75. for task in task_list:
  76. await find_material(task, self.mysql_client)
  77. async def ai_task(self):
  78. """
  79. 通过ai工具和材料来生成文章
  80. :return:
  81. """
  82. select_sql = f"""SELECT task_id, video_title, materials FROM '{db_config}' WHERE status_code = 2 ORDER BY id ASC limit 10;"""
  83. task_list = await self.mysql_client.select(sql=select_sql)
  84. async def ai_generate_text(task_tuple, mysql_client):
  85. task_id, video_title, materials = task_tuple
  86. imgs, ai_title, ai_text = summary_articles(materials)
  87. update_sql = f"""
  88. UPDATE {db_config}
  89. SET ai_text = '{ai_text}', ai_title = '{ai_title}', img_list = '{json.dumps(imgs, ensure_ascii=False)}',status_code = 3
  90. WHERE task_id = '{task_id}';
  91. """
  92. for task in task_list:
  93. await ai_generate_text(task, self.mysql_client)
  94. class MatchArticlesV1(object):
  95. """
  96. 接受请求,并且把数据存储到MySQL服务器中
  97. """
  98. def __init__(self, params, mysql_client):
  99. self.title = None
  100. self.video_id = None
  101. self.params = params
  102. self.mysql_client = mysql_client
  103. def check_params(self):
  104. """
  105. params check
  106. """
  107. try:
  108. self.video_id = self.params['videoId']
  109. self.title = self.params['title']
  110. return None
  111. except AttributeError as e:
  112. response = {
  113. "code": 0,
  114. "error": "Params Error",
  115. "msg": "Params: {} is not correct".format(e)
  116. }
  117. return response
  118. async def record(self):
  119. """
  120. 将数据存储到服务中
  121. :return:
  122. """
  123. request_id = "Article_{}_{}".format(uuid4(), int(time.time()))
  124. request_time = int(time.time())
  125. insert_sql = f"""
  126. INSERT INTO {db_config}
  127. (video_id, task_id, video_title, request_time)
  128. VALUES
  129. ({self.video_id}, '{request_id}', '{self.title}', {request_time})
  130. """
  131. await self.mysql_client.async_insert(sql=insert_sql)
  132. return request_id
  133. async def deal(self):
  134. """
  135. deal function
  136. :return:
  137. """
  138. params_error = self.check_params()
  139. if params_error:
  140. return params_error
  141. else:
  142. task_id = await self.record()
  143. res = {
  144. "status": "success",
  145. "task_id": task_id
  146. }
  147. return res
  148. class MatchArticlesV2(object):
  149. """
  150. 获取视频信息
  151. """
  152. def __init__(self, params, mysql_client):
  153. self.task_id = None
  154. self.params = params
  155. self.mysql_client = mysql_client
  156. def check_params(self):
  157. """
  158. params check
  159. """
  160. try:
  161. self.task_id = self.params['taskId']
  162. return None
  163. except AttributeError as e:
  164. response = {
  165. "code": 0,
  166. "error": "Params Error",
  167. "msg": "Params: {} is not correct".format(e)
  168. }
  169. return response
  170. @classmethod
  171. def get_basic_video_info(cls, video_id):
  172. """
  173. 获取视频信息
  174. :return:
  175. """
  176. url = "http://localhost:8888/singleVideo"
  177. body = {
  178. "videoId": video_id
  179. }
  180. headers = {
  181. "Content-Type": "application/json"
  182. }
  183. response = requests.post(url=url, json=body, headers=headers)
  184. return response.json()
  185. async def recall_articles(self):
  186. """
  187. 从表中召回视频
  188. :return:
  189. """
  190. select_sql = f"""
  191. SELECT video_id, cover, images, ai_text, ai_title, status_code
  192. FROM {db_config}
  193. WHERE task_id = '{self.task_id}';
  194. """
  195. result = await self.mysql_client.select(select_sql)
  196. video_id, cover, images, ai_text, ai_title, status_code = result[0]
  197. match status_code:
  198. case 0:
  199. return {
  200. "task_id": self.task_id,
  201. "code": 0,
  202. "msg": "未处理"
  203. }
  204. case 1:
  205. return {
  206. "task_id": self.task_id,
  207. "code": 1,
  208. "msg": "处理中, 已经用whisper生成视频文本"
  209. }
  210. case 2:
  211. return {
  212. "task_id": self.task_id,
  213. "code": 2,
  214. "msg": "处理中, 已经用AI搜索生成资料"
  215. }
  216. case 3:
  217. result = {
  218. "title": ai_title,
  219. "cover": cover,
  220. "content": ai_text,
  221. "images": images,
  222. "videos": [
  223. self.get_basic_video_info(video_id)
  224. ]
  225. }
  226. response = {
  227. "status": "success",
  228. "article": result
  229. }
  230. return response