ShellGui.py 6.6 KB

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