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.

1 年之前
12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from common.log import logger
  2. from bot import bot_factory
  3. from common.singleton import singleton
  4. from voice import voice_factory
  5. @singleton
  6. class Bridge(object):
  7. def __init__(self):
  8. self.btype={
  9. "chat": "chatGPT",
  10. "voice_to_text": "openai",
  11. "text_to_voice": "baidu"
  12. }
  13. self.bots={}
  14. def get_bot(self,typename):
  15. if self.bots.get(typename) is None:
  16. logger.info("create bot {} for {}".format(self.btype[typename],typename))
  17. if typename == "text_to_voice":
  18. self.bots[typename] = voice_factory.create_voice(self.btype[typename])
  19. elif typename == "voice_to_text":
  20. self.bots[typename] = voice_factory.create_voice(self.btype[typename])
  21. elif typename == "chat":
  22. self.bots[typename] = bot_factory.create_bot(self.btype[typename])
  23. return self.bots[typename]
  24. def get_bot_type(self,typename):
  25. return self.btype[typename]
  26. # 以下所有函数需要得到一个reply字典,格式如下:
  27. # reply["type"] = "ERROR" / "TEXT" / "VOICE" / ...
  28. # reply["content"] = reply的内容
  29. def fetch_reply_content(self, query, context):
  30. return self.get_bot("chat").reply(query, context)
  31. def fetch_voice_to_text(self, voiceFile):
  32. return self.get_bot("voice_to_text").voiceToText(voiceFile)
  33. def fetch_text_to_voice(self, text):
  34. return self.get_bot("text_to_voice").textToVoice(text)