gpt_process.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. response = requests.post(url=config_.GPT_URL, json={'content': prompt})
  32. # print(response.json())
  33. # print(response.json()['choices'][0]['message']['content'])
  34. # print('\n')
  35. # result_content = response.json()['choices'][0]['message']['content']
  36. log_.info(f"response.text: {response.text}")
  37. res_data = json.loads(response.text)
  38. result_content = res_data['data']['choices'][0]['message']['content']
  39. return result_content
  40. def title_generate(video_id, video_path):
  41. """
  42. 视频生成标题
  43. :param video_id: videoId
  44. :param video_path: videoPath
  45. :return:
  46. """
  47. # 1. 下载视频
  48. log_.info(f"debug: title_generate 1")
  49. video_file_path = download_video(video_path=video_path, video_id=video_id, download_folder='videos')
  50. log_.info(f"video_file_path = {video_file_path}")
  51. # 2. 获取视频中的音频
  52. log_.info(f"debug: title_generate 2")
  53. audio_path = get_wav(video_path=video_file_path)
  54. log_.info(f"audio_path = {audio_path}")
  55. # 3. asr
  56. log_.info(f"debug: title_generate 3")
  57. dialogue_path, asr_res = call_asr(audio_path=audio_path)
  58. log_.info({
  59. 'asrResult': {'videoId': video_id, 'asrRes': asr_res}
  60. })
  61. # 4. gpt产出结果
  62. log_.info(f"debug: title_generate 4")
  63. prompt = f"{config_.GPT_PROMPT['title']['prompt2']}{asr_res.strip()}"
  64. gpt_res = request_gpt(prompt=prompt)
  65. # 5. 删除相关文件
  66. for file in [video_file_path, audio_path, dialogue_path]:
  67. os.remove(file)
  68. return gpt_res
  69. # except ConnectionResetError:
  70. # log_.info(video_id)
  71. # except Exception as e:
  72. # log_.info(traceback.format_exc())
  73. # else:
  74. # print(gpt_res)
  75. if __name__ == '__main__':
  76. title_generate(video_id='001', video_path='')