gpt_process.py 2.1 KB

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