您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

linkai_voice.py 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.tech") + "/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.tech") + "/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. "app_code": conf().get("linkai_app_code")
  64. }
  65. res = requests.post(url, headers=headers, json=data, timeout=(5, 120))
  66. if res.status_code == 200:
  67. tmp_file_name = "tmp/" + datetime.datetime.now().strftime('%Y%m%d%H%M%S') + str(random.randint(0, 1000)) + ".mp3"
  68. with open(tmp_file_name, 'wb') as f:
  69. f.write(res.content)
  70. reply = Reply(ReplyType.VOICE, tmp_file_name)
  71. logger.info(f"[LinkVoice] textToVoice success, input={text}, model={model}, voice_id={data.get('voice')}")
  72. return reply
  73. else:
  74. res_json = res.json()
  75. logger.error(f"[LinkVoice] textToVoice error, status_code={res.status_code}, msg={res_json.get('message')}")
  76. return None
  77. except Exception as e:
  78. logger.error(e)
  79. # reply = Reply(ReplyType.ERROR, "遇到了一点小问题,请稍后再问我吧")
  80. return None