ShellGui.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # coding: utf8
  2. import logging
  3. import sys
  4. import signal
  5. from dialog import Dialog
  6. import locale
  7. from core import OrderListener
  8. from core.ConfigurationManager.BrainLoader import BrainLoader
  9. from core.SynapseLauncher import SynapseLauncher
  10. from core.Utils import Utils
  11. from core.ConfigurationManager import SettingLoader
  12. from neurons import Say
  13. logging.basicConfig()
  14. logger = logging.getLogger("kalliope")
  15. def signal_handler(signal, frame):
  16. print "\n"
  17. Utils.print_info("Ctrl+C pressed. Killing Kalliope")
  18. sys.exit(0)
  19. signal.signal(signal.SIGINT, signal_handler)
  20. class ShellGui:
  21. def __init__(self, brain_file=None):
  22. # override brain
  23. self.brain_file = brain_file
  24. # get settings
  25. self.settings = SettingLoader.get_settings()
  26. locale.setlocale(locale.LC_ALL, '')
  27. self.d = Dialog(dialog="dialog")
  28. self.d.set_background_title("Kalliope shell UI")
  29. self.show_main_menu()
  30. def show_main_menu(self):
  31. """
  32. Main menu of the shell UI.
  33. Provide a list of action the user can select to test his settings
  34. :return:
  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. # we get STT from settings
  49. stt_list = self.settings.stts
  50. logger.debug("Loaded stt list: %s" % str(stt_list))
  51. choices = self._get_choices_tuple_from_list(stt_list)
  52. code, tag = self.d.menu("Select the STT to test:",
  53. choices=choices)
  54. if code == self.d.CANCEL:
  55. self.show_main_menu()
  56. if code == self.d.OK:
  57. self.d.infobox("Please talk now")
  58. order_listener = OrderListener(callback=self.callback_stt, stt=str(tag))
  59. order_listener.load_stt_plugin()
  60. def show_tts_test_menu(self, sentence_to_test=None):
  61. """
  62. A menu for testing text to speech
  63. :return:
  64. """
  65. continue_bool = True
  66. # if we don't have yet a sentence to test, we ask the user to type one
  67. if sentence_to_test is None:
  68. # First, we ask the user to type a sentence that will be passed in the TTS
  69. code, sentence_to_test = self.d.inputbox("Please type the sentence you want to test", height=20, width=50)
  70. if code == self.d.CANCEL:
  71. self.show_main_menu()
  72. continue_bool = False
  73. if code == self.d.OK:
  74. continue_bool = True
  75. if continue_bool:
  76. # we get TTS from settings
  77. tts_list = self.settings.ttss
  78. # create a list of tuple that can be used by the dialog menu
  79. choices = self._get_choices_tuple_from_list(tts_list)
  80. code, tag = self.d.menu("Sentence to test: %s" % sentence_to_test,
  81. choices=choices)
  82. if code == self.d.CANCEL:
  83. self.show_tts_test_menu()
  84. if code == self.d.OK:
  85. self._run_tts_test(tag, sentence_to_test)
  86. # then go back to this menu with the same sentence
  87. self.show_tts_test_menu(sentence_to_test=sentence_to_test)
  88. @staticmethod
  89. def _run_tts_test(tts_name, sentence_to_test):
  90. """
  91. Call the TTS
  92. :param tts_name: Name of the TTS module to launch
  93. :param sentence_to_test:
  94. :return:
  95. """
  96. sentence_to_test = sentence_to_test.encode('utf-8')
  97. tts_name = tts_name.encode('utf-8')
  98. Say(message=sentence_to_test, tts=tts_name)
  99. @staticmethod
  100. def _get_choices_tuple_from_list(list_to_convert):
  101. """
  102. Return a list of tup that can be used in Dialog menu
  103. :param list_to_convert: List of object to convert into tuple
  104. :return:
  105. """
  106. # create a list of tuple that can be used by the dialog menu
  107. choices = list()
  108. for el in list_to_convert:
  109. tup = (str(el.name), str(el.parameters))
  110. choices.append(tup)
  111. logger.debug("Add el to the list: %s with parameters: %s" % (str(el.name), str(el.parameters)))
  112. return choices
  113. def callback_stt(self, audio):
  114. """
  115. Callback function called after the STT has finish his job
  116. :param audio: Text from the translated audio
  117. """
  118. code = self.d.msgbox("The STT engine think you said:\n %s" % audio, width=50)
  119. if code == self.d.OK:
  120. self.show_stt_test_menu()
  121. def show_synapses_test_menu(self):
  122. """
  123. Show a list of available synapse in the brain to run it directly
  124. :return:
  125. """
  126. # get the list of synapse from the brain
  127. brain = BrainLoader.get_brain(file_path=self.brain_file)
  128. # create a tuple for the list menu
  129. choices = list()
  130. x = 0
  131. for el in brain.synapses:
  132. tup = (str(el.name), str(x))
  133. choices.append(tup)
  134. x += 1
  135. code, tag = self.d.menu("Select a synapse to run",
  136. choices=choices)
  137. if code == self.d.CANCEL:
  138. self.show_main_menu()
  139. if code == self.d.OK:
  140. logger.debug("Run synapse from GUI: %s" % tag)
  141. SynapseLauncher.start_synapse(tag, brain_file=self.brain_file)
  142. self.show_synapses_test_menu()