matchArticle_deal.py 7.8 KB

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