gpt_process.py 2.3 KB

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