matchArticle_deal.py 8.3 KB

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