-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrontend.py
More file actions
87 lines (70 loc) · 2.93 KB
/
frontend.py
File metadata and controls
87 lines (70 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QTextEdit
from PyQt5.QtCore import QThread, pyqtSignal
import speech_recognition as sr
from chat import Chatbot
import pyttsx3
engine = pyttsx3.init()
engine.setProperty('rate', 150)
engine.setProperty('volume', 0.8)
class CommandListener(QThread):
recognized_command = pyqtSignal(str)
response_received = pyqtSignal(str)
def run(self):
chatbot = Chatbot()
recognizer = sr.Recognizer()
microphone = sr.Microphone()
while True:
with microphone as source:
print('Listening...')
'''recognizer.adjust_for_ambient_noise(source)
recognizer.pause_threshold = 0.5'''
audio = recognizer.listen(source)
try:
command = recognizer.recognize_google(audio)
self.recognized_command.emit(command)
response = chatbot.process_command(command)
print("Response:", response)
engine.say(response)
engine.runAndWait()
self.response_received.emit(response)
except sr.UnknownValueError:
print('Sorry, I did not understand that.')
engine.say(response)
engine.runAndWait()
except sr.RequestError:
print('Sorry, my speech service is down.')
engine.say(response)
engine.runAndWait()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Voice Assistant")
self.dialog_box = QTextEdit(self)
self.dialog_box.setReadOnly(True)
self.dialog_box.setGeometry(20, 20, 360, 200)
self.listen_button = QPushButton("Listen", self)
self.listen_button.setGeometry(150, 230, 80, 30)
self.listen_button.clicked.connect(self.start_listening)
self.command_listener = CommandListener()
self.command_listener.recognized_command.connect(self.handle_command)
self.command_listener.response_received.connect(self.display_response)
self.introduce_chatbot()
def introduce_chatbot(self):
intro_message = "Hello, I am chatbot, your personal assistant. How can I assist you today?"
engine.say(intro_message)
engine.runAndWait()
def start_listening(self):
self.command_listener.start()
def handle_command(self, command):
# Process the recognized command
self.dialog_box.append("You: " + command)
def display_response(self, response):
# Display the response in the dialog box
self.dialog_box.append("Chatbot: " + response)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.setGeometry(100, 100, 400, 280)
window.show()
sys.exit(app.exec_())