ShellGui.py 6.6 KB

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