Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

bridge.py 2.1KB

il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from bot.bot_factory import create_bot
  2. from bridge.context import Context
  3. from bridge.reply import Reply
  4. from common import const
  5. from common.log import logger
  6. from common.singleton import singleton
  7. from config import conf
  8. from translate.factory import create_translator
  9. from voice.factory import create_voice
  10. @singleton
  11. class Bridge(object):
  12. def __init__(self):
  13. self.btype = {
  14. "chat": const.CHATGPT,
  15. "voice_to_text": conf().get("voice_to_text", "openai"),
  16. "text_to_voice": conf().get("text_to_voice", "google"),
  17. "translate": conf().get("translate", "baidu"),
  18. }
  19. model_type = conf().get("model")
  20. if model_type in ["text-davinci-003"]:
  21. self.btype["chat"] = const.OPEN_AI
  22. if conf().get("use_azure_chatgpt", False):
  23. self.btype["chat"] = const.CHATGPTONAZURE
  24. self.bots = {}
  25. def get_bot(self, typename):
  26. if self.bots.get(typename) is None:
  27. logger.info("create bot {} for {}".format(self.btype[typename], typename))
  28. if typename == "text_to_voice":
  29. self.bots[typename] = create_voice(self.btype[typename])
  30. elif typename == "voice_to_text":
  31. self.bots[typename] = create_voice(self.btype[typename])
  32. elif typename == "chat":
  33. self.bots[typename] = create_bot(self.btype[typename])
  34. elif typename == "translate":
  35. self.bots[typename] = create_translator(self.btype[typename])
  36. return self.bots[typename]
  37. def get_bot_type(self, typename):
  38. return self.btype[typename]
  39. def fetch_reply_content(self, query, context: Context) -> Reply:
  40. return self.get_bot("chat").reply(query, context)
  41. def fetch_voice_to_text(self, voiceFile) -> Reply:
  42. return self.get_bot("voice_to_text").voiceToText(voiceFile)
  43. def fetch_text_to_voice(self, text) -> Reply:
  44. return self.get_bot("text_to_voice").textToVoice(text)
  45. def fetch_translate(self, text, from_lang="", to_lang="en") -> Reply:
  46. return self.get_bot("translate").translate(text, from_lang, to_lang)