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.

83 line
3.5KB

  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. try:
  27. mp3_file = os.path.splitext(voice_file)[0] + ".mp3"
  28. audio_convert.any_to_mp3(voice_file, mp3_file)
  29. voice_file = mp3_file
  30. except Exception as e:
  31. logger.warn(f"[LinkVoice] amr file transfer failed, directly send amr voice file: {format(e)}")
  32. file = open(voice_file, "rb")
  33. file_body = {
  34. "file": file
  35. }
  36. data = {
  37. "model": model
  38. }
  39. res = requests.post(url, files=file_body, headers=headers, data=data, timeout=(5, 60))
  40. if res.status_code == 200:
  41. text = res.json().get("text")
  42. else:
  43. res_json = res.json()
  44. logger.error(f"[LinkVoice] voiceToText error, status_code={res.status_code}, msg={res_json.get('message')}")
  45. return None
  46. reply = Reply(ReplyType.TEXT, text)
  47. logger.info(f"[LinkVoice] voiceToText success, text={text}, file name={voice_file}")
  48. except Exception as e:
  49. logger.error(e)
  50. return None
  51. return reply
  52. def textToVoice(self, text):
  53. try:
  54. url = conf().get("linkai_api_base", "https://api.link-ai.chat") + "/v1/audio/speech"
  55. headers = {"Authorization": "Bearer " + conf().get("linkai_api_key")}
  56. model = const.TTS_1
  57. if not conf().get("text_to_voice") or conf().get("text_to_voice") in ["openai", const.TTS_1, const.TTS_1_HD]:
  58. model = conf().get("text_to_voice_model") or const.TTS_1
  59. data = {
  60. "model": model,
  61. "input": text,
  62. "voice": conf().get("tts_voice_id")
  63. }
  64. res = requests.post(url, headers=headers, json=data, timeout=(5, 120))
  65. if res.status_code == 200:
  66. tmp_file_name = "tmp/" + datetime.datetime.now().strftime('%Y%m%d%H%M%S') + str(random.randint(0, 1000)) + ".mp3"
  67. with open(tmp_file_name, 'wb') as f:
  68. f.write(res.content)
  69. reply = Reply(ReplyType.VOICE, tmp_file_name)
  70. logger.info(f"[LinkVoice] textToVoice success, input={text}, model={model}, voice_id={data.get('voice')}")
  71. return reply
  72. else:
  73. res_json = res.json()
  74. logger.error(f"[LinkVoice] textToVoice error, status_code={res.status_code}, msg={res_json.get('message')}")
  75. return None
  76. except Exception as e:
  77. logger.error(e)
  78. # reply = Reply(ReplyType.ERROR, "遇到了一点小问题,请稍后再问我吧")
  79. return None