Neurone.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import importlib
  2. from jinja2 import Template
  3. import random
  4. from core import ConfigurationManager
  5. class NoTemplateException(Exception):
  6. pass
  7. class Neurone:
  8. def __init__(self, **kwargs):
  9. # get the name of the plugin
  10. # print self.__class__.__name__
  11. # load the tts from settings
  12. self.tts = ConfigurationManager.get_default_text_to_speech()
  13. # get tts args
  14. self.tts_args = ConfigurationManager.get_tts_args(self.tts)
  15. def say(self, message, kwargs):
  16. # get the tts if is specified otherwise use default
  17. tts = kwargs.get('tts', None)
  18. if tts is not None:
  19. self.tts = tts
  20. self.tts_args = ConfigurationManager.get_tts_args(self.tts)
  21. # check if it's a single message or multiple one
  22. if isinstance(message, list):
  23. # then we pick randomly one message
  24. message = random.choice(message)
  25. # Check if there is a template associate to the output message
  26. template = kwargs.get('say_template', None)
  27. if isinstance(message, dict):
  28. if template is not None:
  29. if isinstance(template, list):
  30. # then we pick randomly one template
  31. template = random.choice(template)
  32. t = Template(template)
  33. message = t.render(**message)
  34. else:
  35. raise NoTemplateException("You must specify a say_template to your Neurone for the entries ", message.keys())
  36. # here we use the tts to make jarvis talk
  37. # the module is imported on fly, depending on the selected tts from settings
  38. tts_backend = importlib.import_module("tts." + self.tts)
  39. tts_backend.say(words=message, **self.tts_args)