您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import sys
  2. from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QPushButton, QLabel, QVBoxLayout, QWidget
  3. from PyQt5.QtMultimediaWidgets import QVideoWidget
  4. from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent
  5. from PyQt5.QtCore import QUrl
  6. import speech_recognition as sr
  7. import question
  8. import os
  9. current_directory = os.path.dirname(os.path.realpath(__file__))
  10. main_video_path = os.path.join(current_directory, 'Friendly_new.mp4')
  11. class HospitalChatbotGUI(QMainWindow):
  12. def __init__(self):
  13. super().__init__()
  14. self.setWindowTitle("Hospital Chatbot")
  15. self.setGeometry(100, 100, 800, 600)
  16. self.central_widget = QWidget(self)
  17. self.setCentralWidget(self.central_widget)
  18. self.layout = QVBoxLayout()
  19. #self.submit_button = QPushButton("Submit", self)
  20. #self.submit_button.clicked.connect(self.get_response)
  21. #self.layout.addWidget(self.submit_button)
  22. self.speak_button = QPushButton("Speak", self)
  23. self.speak_button.clicked.connect(self.audio_to_text)
  24. self.layout.addWidget(self.speak_button)
  25. self.video_widget = QVideoWidget(self)
  26. self.layout.addWidget(self.video_widget)
  27. #self.video_widget.setFixedWidth(500)
  28. self.central_widget.setLayout(self.layout)
  29. self.media_player = QMediaPlayer()
  30. self.media_player.setVideoOutput(self.video_widget)
  31. self.media_player.mediaStatusChanged.connect(self.handle_media_status_changed)
  32. #self.sample_video_url = "/home/knight/ChatBot_UI/Friendly_new.mp4"
  33. self.sample_video_url = main_video_path
  34. self.answer_video_url = None
  35. self.play_video(self.sample_video_url)
  36. def audio_to_text(self):
  37. init_rec = sr.Recognizer()
  38. print("Let's speak!!")
  39. with sr.Microphone(3) as source:
  40. audio_data = init_rec.record(source, duration=2)
  41. print("Recognizing your text.............")
  42. text = init_rec.recognize_google(audio_data)
  43. self.get_response(text.strip())
  44. print(text)
  45. def get_response(self,text):
  46. video_url = None
  47. print("Response Text: ",text)
  48. user_input = text.strip()
  49. lowercase_text = user_input.lower()
  50. matching_intent = [intent for intent, questions in question.intents_and_question.items() if any(q in lowercase_text for q in questions)]
  51. if matching_intent:
  52. print("Intent: ", matching_intent[0])
  53. video_url = question.get_video_response(matching_intent[0])
  54. else:
  55. print("No matching intent found.")
  56. if video_url:
  57. self.play_video(video_url)
  58. return
  59. matching_intent = None
  60. #self.play_video("/home/knight/ChatBot_UI/Friendly_new.mp4")
  61. self.play_video(main_video_path)
  62. def play_video(self, video_url):
  63. media_content = QMediaContent(QUrl.fromLocalFile(video_url))
  64. self.media_player.setMedia(media_content)
  65. self.media_player.play()
  66. def handle_media_status_changed(self, status):
  67. if status == QMediaPlayer.EndOfMedia:
  68. print("Video finished playing.")
  69. #self.play_video("/home/knight/ChatBot_UI/Friendly_new.mp4")
  70. self.play_video(main_video_path)
  71. if __name__ == '__main__':
  72. app = QApplication(sys.argv)
  73. window = HospitalChatbotGUI()
  74. window.show()
  75. sys.exit(app.exec_())