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.

79 lines
3.2KB

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