gpt_process.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import os
  2. import time
  3. import requests
  4. import json
  5. import traceback
  6. from utils import download_video
  7. from audio_process import get_wav
  8. from xunfei_asr import call_asr
  9. from config import set_config
  10. from log import Log
  11. config_ = set_config()
  12. log_ = Log()
  13. def request_gpt(prompt):
  14. """
  15. headers = {
  16. 'Content-Type': 'application/json',
  17. 'Authorization': f'Bearer {config_.GPT_OPENAI_API_KEY}',
  18. }
  19. proxies = config_.PROXIES
  20. json_data = {
  21. 'model': 'gpt-3.5-turbo',
  22. 'messages': [
  23. {
  24. 'role': 'user',
  25. 'content': prompt,
  26. },
  27. ],
  28. }
  29. response = requests.post(url=config_.GPT_HOST, headers=headers, json=json_data, proxies=proxies)
  30. """
  31. retry_count = 0
  32. result_content = None
  33. while retry_count < config_.RETRY_MAX_COUNT:
  34. retry_count += 1
  35. try:
  36. response = requests.post(url=config_.GPT_URL, json={'content': prompt, 'auth': config_.GPT_OPENAI_API_KEY})
  37. # print(response.json())
  38. # print(response.json()['choices'][0]['message']['content'])
  39. # print('\n')
  40. # result_content = response.json()['choices'][0]['message']['content']
  41. # log_.info(f"response.text: {response.text}")
  42. res_data = json.loads(response.text)
  43. if res_data['code'] != 0:
  44. time.sleep(10)
  45. continue
  46. result_content = res_data['data']['choices'][0]['message']['content']
  47. except Exception:
  48. time.sleep(10)
  49. continue
  50. return result_content
  51. def title_generate(video_id, video_path):
  52. """
  53. 视频生成标题
  54. :param video_id: videoId
  55. :param video_path: videoPath
  56. :return:
  57. """
  58. generate_filepath = dict()
  59. # 1. 下载视频
  60. # log_.info(f"debug: title_generate 1")
  61. video_file_path = download_video(video_path=video_path, video_id=video_id, download_folder='videos')
  62. generate_filepath['video_file_path'] = video_file_path
  63. # log_.info({'videoId': video_id, 'video_file_path': video_file_path})
  64. # 2. 获取视频中的音频
  65. # log_.info(f"debug: title_generate 2")
  66. audio_path = get_wav(video_path=video_file_path)
  67. generate_filepath['audio_path'] = audio_path
  68. # log_.info({'videoId': video_id, 'audio_path': audio_path})
  69. # 3. asr
  70. # log_.info(f"debug: title_generate 3")
  71. dialogue_path, asr_res = call_asr(audio_path=audio_path)
  72. generate_filepath['dialogue_path'] = dialogue_path
  73. log_.info({
  74. 'asrResult': {'videoId': video_id, 'asrRes': asr_res}
  75. })
  76. # 4. gpt产出结果
  77. # log_.info(f"debug: title_generate 4")
  78. prompt = f"{config_.GPT_PROMPT['title']['prompt2']}{asr_res.strip()}"
  79. gpt_res = request_gpt(prompt=prompt)
  80. return gpt_res, generate_filepath
  81. # except ConnectionResetError:
  82. # log_.info(video_id)
  83. # except Exception as e:
  84. # log_.info(traceback.format_exc())
  85. # else:
  86. # print(gpt_res)
  87. if __name__ == '__main__':
  88. title_generate(video_id='001', video_path='')