You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 satır
3.4KB

  1. """
  2. google voice service
  3. """
  4. import random
  5. import requests
  6. from voice import audio_convert
  7. from bridge.reply import Reply, ReplyType
  8. from common.log import logger
  9. from config import conf
  10. from voice.voice import Voice
  11. from common import const
  12. import os
  13. import datetime
  14. class LinkAIVoice(Voice):
  15. def __init__(self):
  16. pass
  17. def voiceToText(self, voice_file):
  18. logger.debug("[LinkVoice] voice file name={}".format(voice_file))
  19. try:
  20. url = conf().get("linkai_api_base", "https://api.link-ai.chat") + "/v1/audio/transcriptions"
  21. headers = {"Authorization": "Bearer " + conf().get("linkai_api_key")}
  22. model = None
  23. if not conf().get("text_to_voice") or conf().get("voice_to_text") == "openai":
  24. model = const.WHISPER_1
  25. if voice_file.endswith(".amr"):
  26. mp3_file = os.path.splitext(voice_file)[0] + ".mp3"
  27. audio_convert.any_to_mp3(voice_file, mp3_file)
  28. voice_file = mp3_file
  29. file = open(voice_file, "rb")
  30. file_body = {
  31. "file": file
  32. }
  33. data = {
  34. "model": model
  35. }
  36. res = requests.post(url, files=file_body, headers=headers, data=data, timeout=(5, 60))
  37. if res.status_code == 200:
  38. text = res.json().get("text")
  39. else:
  40. res_json = res.json()
  41. logger.error(f"[LinkVoice] voiceToText error, status_code={res.status_code}, msg={res_json.get('message')}")
  42. return None
  43. reply = Reply(ReplyType.TEXT, text)
  44. logger.info(f"[LinkVoice] voiceToText success, text={text}, file name={voice_file}")
  45. except Exception as e:
  46. logger.error(e)
  47. reply = Reply(ReplyType.ERROR, "我暂时还无法听清您的语音,请稍后再试吧~")
  48. return reply
  49. def textToVoice(self, text):
  50. try:
  51. url = conf().get("linkai_api_base", "https://api.link-ai.chat") + "/v1/audio/speech"
  52. headers = {"Authorization": "Bearer " + conf().get("linkai_api_key")}
  53. model = const.TTS_1
  54. if not conf().get("text_to_voice") or conf().get("text_to_voice") in ["openai", const.TTS_1, const.TTS_1_HD]:
  55. model = conf().get("text_to_voice_model") or const.TTS_1
  56. data = {
  57. "model": model,
  58. "input": text,
  59. "voice": conf().get("tts_voice_id")
  60. }
  61. res = requests.post(url, headers=headers, json=data, timeout=(5, 120))
  62. if res.status_code == 200:
  63. tmp_file_name = "tmp/" + datetime.datetime.now().strftime('%Y%m%d%H%M%S') + str(random.randint(0, 1000)) + ".mp3"
  64. with open(tmp_file_name, 'wb') as f:
  65. f.write(res.content)
  66. reply = Reply(ReplyType.VOICE, tmp_file_name)
  67. logger.info(f"[LinkVoice] textToVoice success, input={text}, model={model}, voice_id={data.get('voice')}")
  68. return reply
  69. else:
  70. res_json = res.json()
  71. logger.error(f"[LinkVoice] textToVoice error, status_code={res.status_code}, msg={res_json.get('message')}")
  72. return None
  73. except Exception as e:
  74. logger.error(e)
  75. reply = Reply(ReplyType.ERROR, "遇到了一点小问题,请稍后再问我吧")
  76. return reply