gpt_process.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import time
  2. import requests
  3. import traceback
  4. from utils import download_video
  5. from audio_process import get_wav
  6. from xunfei_asr import call_asr
  7. from config import set_config
  8. from log import Log
  9. config_ = set_config()
  10. log_ = Log()
  11. def request_gpt(prompt):
  12. headers = {
  13. 'Content-Type': 'application/json',
  14. 'Authorization': f'Bearer {config_.GPT_OPENAI_API_KEY}',
  15. }
  16. proxies = config_.PROXIES
  17. json_data = {
  18. 'model': 'gpt-3.5-turbo',
  19. 'messages': [
  20. {
  21. 'role': 'user',
  22. 'content': f'{prompt}',
  23. },
  24. ],
  25. }
  26. raise ConnectionResetError
  27. response = requests.post(url=config_.GPT_HOST, headers=headers, json=json_data, proxies=proxies)
  28. # print(response.json())
  29. # print(response.json()['choices'][0]['message']['content'])
  30. # print('\n')
  31. result_content = response.json()['choices'][0]['message']['content']
  32. return result_content
  33. def title_generate(video_id, video_path):
  34. """
  35. 视频生成标题
  36. :param video_id: videoId
  37. :param video_path: videoPath
  38. :return:
  39. """
  40. # 1. 下载视频
  41. video_file_path = download_video(video_path=video_path, video_id=video_id, download_folder='videos')
  42. log_.info(f"video_file_path = {video_file_path}")
  43. # 2. 获取视频中的音频
  44. audio_path = get_wav(video_path=video_file_path)
  45. log_.info(f"audio_path = {audio_path}")
  46. # 3. asr
  47. dialogue_path, asr_res = call_asr(audio_path=audio_path)
  48. log_.info(f"dialogue_path = {dialogue_path}, asr_res = {asr_res}")
  49. # 4. gpt产出结果
  50. prompt = f"{config_.GPT_PROMPT['title']['prompt2']}{asr_res.strip()}"
  51. gpt_res = request_gpt(prompt=prompt)
  52. return gpt_res
  53. # except ConnectionResetError:
  54. # log_.info(video_id)
  55. # except Exception as e:
  56. # log_.info(traceback.format_exc())
  57. # else:
  58. # print(gpt_res)
  59. # print(gpt_res)
  60. # log_.info(f"gpt_res = {gpt_res}")
  61. if __name__ == '__main__':
  62. title_generate(video_id='001', video_path='')