|
| 1 | +import sys |
| 2 | +import speech_recognition as sr |
| 3 | +import pyttsx3 |
| 4 | +import webbrowser |
| 5 | +import pywhatkit |
| 6 | +from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton |
| 7 | +from PyQt5.QtCore import Qt, QThread, pyqtSignal |
| 8 | + |
| 9 | +class ListenerThread(QThread): |
| 10 | + signal = pyqtSignal(str) |
| 11 | + |
| 12 | + def __init__(self): |
| 13 | + super().__init__() |
| 14 | + |
| 15 | + def run(self): |
| 16 | + recognizer = sr.Recognizer() |
| 17 | + with sr.Microphone() as source: |
| 18 | + print("Listening...") |
| 19 | + recognizer.adjust_for_ambient_noise(source) |
| 20 | + audio = recognizer.listen(source) |
| 21 | + |
| 22 | + try: |
| 23 | + text = recognizer.recognize_google(audio) |
| 24 | + self.signal.emit(text) |
| 25 | + except sr.UnknownValueError: |
| 26 | + self.signal.emit("") |
| 27 | + |
| 28 | +class MainWindow(QMainWindow): |
| 29 | + def __init__(self): |
| 30 | + super().__init__() |
| 31 | + |
| 32 | + self.setWindowTitle("Voice Assistant") |
| 33 | + self.setGeometry(100, 100, 300, 200) |
| 34 | + |
| 35 | + self.label = QLabel("Assistant is ready.", self) |
| 36 | + self.label.setAlignment(Qt.AlignCenter) |
| 37 | + self.label.setGeometry(50, 50, 200, 30) |
| 38 | + |
| 39 | + self.button = QPushButton("Start Listening", self) |
| 40 | + self.button.setGeometry(100, 100, 100, 30) |
| 41 | + self.button.clicked.connect(self.start_listening) |
| 42 | + |
| 43 | + self.listener_thread = ListenerThread() |
| 44 | + self.listener_thread.signal.connect(self.process_input) |
| 45 | + |
| 46 | + self.synthesizer = pyttsx3.init() |
| 47 | + |
| 48 | + def start_listening(self): |
| 49 | + self.listener_thread.start() |
| 50 | + |
| 51 | + def process_input(self, text): |
| 52 | + if "open youtube" in text.lower(): |
| 53 | + self.speak("Opening YouTube...") |
| 54 | + webbrowser.open("https://www.youtube.com") |
| 55 | + |
| 56 | + elif "open facebook" in text.lower(): |
| 57 | + self.speak("Opening Facebook...") |
| 58 | + webbrowser.open("https://www.facebook.com") |
| 59 | + |
| 60 | + elif "play" in text.lower() and "on youtube" in text.lower(): |
| 61 | + query = text.lower().replace("play", "").replace("on youtube", "").strip() |
| 62 | + self.speak(f"Playing {query} on YouTube...") |
| 63 | + pywhatkit.playonyt(query) |
| 64 | + |
| 65 | + elif text.lower() == "hello": |
| 66 | + self.speak("Hello there!") |
| 67 | + |
| 68 | + elif text.lower() == "what's your name": |
| 69 | + self.speak("I am your creative voice assistant.") |
| 70 | + |
| 71 | + elif text.lower() == "exit": |
| 72 | + self.speak("Goodbye!") |
| 73 | + sys.exit() |
| 74 | + |
| 75 | + else: |
| 76 | + self.speak("Sorry, I couldn't understand you.") |
| 77 | + |
| 78 | + def speak(self, text): |
| 79 | + self.synthesizer.say(text) |
| 80 | + self.synthesizer.runAndWait() |
| 81 | + |
| 82 | +if __name__ == '__main__': |
| 83 | + app = QApplication(sys.argv) |
| 84 | + |
| 85 | + window = MainWindow() |
| 86 | + window.show() |
| 87 | + |
| 88 | + sys.exit(app.exec_()) |
0 commit comments