-
Notifications
You must be signed in to change notification settings - Fork 104
/
Basic_Voice_Assistant_using_PyQt5_V3.py
88 lines (66 loc) · 2.57 KB
/
Basic_Voice_Assistant_using_PyQt5_V3.py
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
88
import sys
import speech_recognition as sr
import pyttsx3
import webbrowser
import pywhatkit
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton
from PyQt5.QtCore import Qt, QThread, pyqtSignal
class ListenerThread(QThread):
signal = pyqtSignal(str)
def __init__(self):
super().__init__()
def run(self):
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)
try:
text = recognizer.recognize_google(audio)
self.signal.emit(text)
except sr.UnknownValueError:
self.signal.emit("")
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Voice Assistant")
self.setGeometry(100, 100, 300, 200)
self.label = QLabel("Assistant is ready.", self)
self.label.setAlignment(Qt.AlignCenter)
self.label.setGeometry(50, 50, 200, 30)
self.button = QPushButton("Start Listening", self)
self.button.setGeometry(100, 100, 100, 30)
self.button.clicked.connect(self.start_listening)
self.listener_thread = ListenerThread()
self.listener_thread.signal.connect(self.process_input)
self.synthesizer = pyttsx3.init()
def start_listening(self):
self.listener_thread.start()
def process_input(self, text):
if "open youtube" in text.lower():
self.speak("Opening YouTube...")
webbrowser.open("https://www.youtube.com")
elif "open facebook" in text.lower():
self.speak("Opening Facebook...")
webbrowser.open("https://www.facebook.com")
elif "play" in text.lower() and "on youtube" in text.lower():
query = text.lower().replace("play", "").replace("on youtube", "").strip()
self.speak(f"Playing {query} on YouTube...")
pywhatkit.playonyt(query)
elif text.lower() == "hello":
self.speak("Hello there!")
elif text.lower() == "what's your name":
self.speak("I am your creative voice assistant.")
elif text.lower() == "exit":
self.speak("Goodbye!")
sys.exit()
else:
self.speak("Sorry, I couldn't understand you.")
def speak(self, text):
self.synthesizer.say(text)
self.synthesizer.runAndWait()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())