ShellGui.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. # coding: utf8
  2. import locale
  3. import logging
  4. import signal
  5. import sys
  6. from dialog import Dialog
  7. from kalliope.core import OrderListener
  8. from kalliope.core.ConfigurationManager import SettingLoader
  9. from kalliope.core.SynapseLauncher import SynapseLauncher
  10. from kalliope.core.Utils.Utils import Utils
  11. from kalliope.neurons.say.say import Say
  12. logging.basicConfig()
  13. logger = logging.getLogger("kalliope")
  14. class ShellGui:
  15. def __init__(self, brain=None):
  16. """
  17. Load a GUI in a shell console for testing TTS, STT and brain configuration
  18. :param brain: The Brain object provided by the brain.yml
  19. :type brain: Brain
  20. .. seealso:: Brain
  21. """
  22. # override brain
  23. self.brain = brain
  24. # get settings
  25. sl = SettingLoader()
  26. self.settings = sl.settings
  27. locale.setlocale(locale.LC_ALL, '')
  28. self.d = Dialog(dialog="dialog")
  29. self.d.set_background_title("Kalliope shell UI")
  30. self.show_main_menu()
  31. def show_main_menu(self):
  32. """
  33. Main menu of the shell UI.
  34. Provide a list of action the user can select to test his settings
  35. """
  36. code, tag = self.d.menu("Test your Kalliope settings from this menu",
  37. choices=[("TTS", "Text to Speech"),
  38. ("STT", "Speech to text"),
  39. ("Synapses", "Run a synapse")])
  40. if code == self.d.OK:
  41. if tag == "STT":
  42. self.show_stt_test_menu()
  43. if tag == "TTS":
  44. self.show_tts_test_menu()
  45. if tag == "Synapses":
  46. self.show_synapses_test_menu()
  47. def show_stt_test_menu(self):
  48. """
  49. Show the list of available STT.
  50. Clicking on a STT will load the engine to catch the user audio and return a text
  51. """
  52. # we get STT from settings
  53. stt_list = self.settings.stts
  54. logger.debug("Loaded stt list: %s" % str(stt_list))
  55. choices = self._get_choices_tuple_from_list(stt_list)
  56. code, tag = self.d.menu("Select the STT to test:",
  57. choices=choices)
  58. # go back to the main menu if we choose "cancel"
  59. if code == self.d.CANCEL:
  60. self.show_main_menu()
  61. # if ok, call the target TTS engine and catch audio
  62. if code == self.d.OK:
  63. self.d.infobox("Please talk now")
  64. # the callback funtion will print the translated audio into text on the screen
  65. order_listener = OrderListener(callback=self.callback_stt, stt=str(tag))
  66. order_listener.load_stt_plugin()
  67. def show_tts_test_menu(self, sentence_to_test=None):
  68. """
  69. A menu for testing text to speech
  70. - select a TTS engine to test
  71. - type a sentence
  72. - press ok and listen the generated audio from the typed text
  73. :param sentence_to_test: the screen written sentence to test
  74. """
  75. continue_bool = True
  76. # if we don't have yet a sentence to test, we ask the user to type one
  77. if sentence_to_test is None:
  78. # First, we ask the user to type a sentence that will be passed in the TTS
  79. code, sentence_to_test = self.d.inputbox("Please type the sentence you want to test", height=20, width=50)
  80. if code == self.d.CANCEL:
  81. self.show_main_menu()
  82. continue_bool = False
  83. if code == self.d.OK:
  84. continue_bool = True
  85. if continue_bool:
  86. # we get TTS from settings
  87. tts_list = self.settings.ttss
  88. # create a list of tuple that can be used by the dialog menu
  89. choices = self._get_choices_tuple_from_list(tts_list)
  90. code, tag = self.d.menu("Sentence to test: %s" % sentence_to_test,
  91. choices=choices)
  92. if code == self.d.CANCEL:
  93. self.show_tts_test_menu()
  94. if code == self.d.OK:
  95. self._run_tts_test(tag, sentence_to_test)
  96. # then go back to this menu with the same sentence
  97. # if the user want to test the same text with another TTS
  98. self.show_tts_test_menu(sentence_to_test=sentence_to_test)
  99. @staticmethod
  100. def _run_tts_test(tts_name, sentence_to_test):
  101. """
  102. Call the TTS
  103. :param tts_name: Name of the TTS module to launch
  104. :param sentence_to_test: String text to send to the TTS engine
  105. """
  106. sentence_to_test = sentence_to_test.encode('utf-8')
  107. tts_name = tts_name.encode('utf-8')
  108. Say(message=sentence_to_test, tts=tts_name)
  109. @staticmethod
  110. def _get_choices_tuple_from_list(list_to_convert):
  111. """
  112. Return a list of tup that can be used in Dialog menu
  113. :param list_to_convert: List of object to convert into tuple
  114. :return: List of choices
  115. :rtype: List
  116. """
  117. # create a list of tuple that can be used by the dialog menu
  118. choices = list()
  119. for el in list_to_convert:
  120. tup = (str(el.name), str(el.parameters))
  121. choices.append(tup)
  122. logger.debug("Add el to the list: %s with parameters: %s" % (str(el.name), str(el.parameters)))
  123. return choices
  124. def callback_stt(self, audio):
  125. """
  126. Callback function called after the STT has finish his job
  127. Print the text of what the STT engine think we said on the screen
  128. :param audio: Text from the translated audio
  129. """
  130. code = self.d.msgbox("The STT engine think you said:\n %s" % audio, width=50)
  131. if code == self.d.OK:
  132. self.show_stt_test_menu()
  133. def show_synapses_test_menu(self):
  134. """
  135. Show a list of available synapse in the brain to run it directly
  136. """
  137. # create a tuple for the list menu
  138. choices = list()
  139. x = 0
  140. for el in self.brain.synapses:
  141. tup = (str(el.name), str(x))
  142. choices.append(tup)
  143. x += 1
  144. code, tag = self.d.menu("Select a synapse to run",
  145. choices=choices)
  146. if code == self.d.CANCEL:
  147. self.show_main_menu()
  148. if code == self.d.OK:
  149. logger.debug("Run synapse from GUI: %s" % tag)
  150. SynapseLauncher.start_synapse_by_name(tag, brain=self.brain)
  151. self.show_synapses_test_menu()