ShellGui.py 5.5 KB

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