Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

71 lines
2.6KB

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