openai_voice.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """
  2. google voice service
  3. """
  4. import datetime
  5. import random
  6. import requests
  7. from bridge.reply import Reply, ReplyType
  8. from common import const
  9. from common.log import logger
  10. from config import conf
  11. from voice.voice import Voice
  12. class OpenaiVoice(Voice):
  13. def voiceToText(self, voice_file):
  14. logger.debug("[Openai] voice file name={}".format(voice_file))
  15. try:
  16. file = open(voice_file, "rb")
  17. api_base = conf().get("open_ai_api_base") or "https://api.openai.com/v1"
  18. url = f'{api_base}/audio/transcriptions'
  19. headers = {
  20. 'Authorization': 'Bearer ' + conf().get("open_ai_api_key"),
  21. # 'Content-Type': 'multipart/form-data' # 加了会报错,不知道什么原因
  22. }
  23. files = {
  24. "file": file,
  25. }
  26. data = {
  27. "model": "whisper-1",
  28. }
  29. response = requests.post(url, headers=headers, files=files, data=data)
  30. response_data = response.json()
  31. text = response_data['text']
  32. reply = Reply(ReplyType.TEXT, text)
  33. logger.info("[Openai] voiceToText text={} voice file name={}".format(text, voice_file))
  34. except Exception as e:
  35. reply = Reply(ReplyType.ERROR, "我暂时还无法听清您的语音,请稍后再试吧~")
  36. finally:
  37. return reply
  38. def textToVoice(self, text):
  39. try:
  40. api_base = conf().get("open_ai_api_base") or "https://api.openai.com/v1"
  41. url = f'{api_base}/audio/speech'
  42. headers = {
  43. 'Authorization': 'Bearer ' + conf().get("open_ai_api_key"),
  44. 'Content-Type': 'application/json'
  45. }
  46. data = {
  47. 'model': conf().get("text_to_voice_model") or const.TTS_1,
  48. 'input': text,
  49. 'voice': conf().get("tts_voice_id") or "alloy"
  50. }
  51. response = requests.post(url, headers=headers, json=data)
  52. file_name = "tmp/" + datetime.datetime.now().strftime('%Y%m%d%H%M%S') + str(
  53. random.randint(0, 1000)) + ".mp3"
  54. logger.debug(f"[OPENAI] text_to_Voice file_name={file_name}, input={text}")
  55. with open(file_name, 'wb') as f:
  56. f.write(response.content)
  57. logger.info(f"[OPENAI] text_to_Voice success")
  58. reply = Reply(ReplyType.VOICE, file_name)
  59. except Exception as e:
  60. logger.error(e)
  61. reply = Reply(ReplyType.ERROR, "遇到了一点小问题,请稍后再问我吧")
  62. return reply