|
| 1 | +"""Virtual assistant — speech I/O and command dispatch.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import datetime |
| 5 | +import webbrowser |
| 6 | + |
| 7 | +import pyttsx3 |
| 8 | +import speech_recognition as sr |
| 9 | +import wikipedia |
| 10 | + |
| 11 | + |
| 12 | +class VirtualAssistant: |
| 13 | + """Jarvis-style voice assistant with pluggable command handlers.""" |
| 14 | + |
| 15 | + def __init__(self, name: str = "Jarvis") -> None: |
| 16 | + self.name = name |
| 17 | + self._engine = pyttsx3.init() |
| 18 | + self._recognizer = sr.Recognizer() |
| 19 | + |
| 20 | + # ------------------------------------------------------------------ |
| 21 | + # Speech I/O |
| 22 | + # ------------------------------------------------------------------ |
| 23 | + |
| 24 | + def speak(self, text: str) -> None: |
| 25 | + """Convert text to speech.""" |
| 26 | + self._engine.say(text) |
| 27 | + self._engine.runAndWait() |
| 28 | + |
| 29 | + def listen(self) -> str | None: |
| 30 | + """Listen via microphone and return recognised text (or None).""" |
| 31 | + try: |
| 32 | + with sr.Microphone() as source: |
| 33 | + print("Listening...") |
| 34 | + self._recognizer.adjust_for_ambient_noise(source, duration=1) |
| 35 | + self._recognizer.pause_threshold = 1 |
| 36 | + audio = self._recognizer.listen(source) |
| 37 | + query = self._recognizer.recognize_google(audio, language="en-in") |
| 38 | + print(f"Command: {query}") |
| 39 | + return query |
| 40 | + except sr.UnknownValueError: |
| 41 | + self.speak("Sorry, I didn't catch that.") |
| 42 | + except sr.RequestError as e: |
| 43 | + self.speak("Speech service unavailable.") |
| 44 | + print(f"RequestError: {e}") |
| 45 | + except Exception as e: |
| 46 | + self.speak("An unexpected error occurred.") |
| 47 | + print(f"Error: {e}") |
| 48 | + return None |
| 49 | + |
| 50 | + # ------------------------------------------------------------------ |
| 51 | + # Command handlers |
| 52 | + # ------------------------------------------------------------------ |
| 53 | + |
| 54 | + def greet(self) -> None: |
| 55 | + self.speak(f"Hello! I'm {self.name}. How can I assist you?") |
| 56 | + |
| 57 | + def tell_day(self) -> None: |
| 58 | + days = {1: "Monday", 2: "Tuesday", 3: "Wednesday", 4: "Thursday", |
| 59 | + 5: "Friday", 6: "Saturday", 7: "Sunday"} |
| 60 | + day = datetime.datetime.today().weekday() + 1 |
| 61 | + self.speak(f"Today is {days[day]}") |
| 62 | + |
| 63 | + def tell_time(self) -> None: |
| 64 | + t = datetime.datetime.now().strftime("%H:%M") |
| 65 | + self.speak(f"The time is {t}") |
| 66 | + |
| 67 | + def open_google(self, search: str) -> None: |
| 68 | + webbrowser.open(f"https://www.google.com/search?q={search}") |
| 69 | + |
| 70 | + def open_url(self, url: str) -> None: |
| 71 | + webbrowser.open(url) |
| 72 | + |
| 73 | + def wikipedia_search(self, query: str) -> None: |
| 74 | + self.speak("Checking Wikipedia") |
| 75 | + try: |
| 76 | + result = wikipedia.summary(query, sentences=4) |
| 77 | + self.speak("According to Wikipedia") |
| 78 | + self.speak(result) |
| 79 | + except wikipedia.exceptions.DisambiguationError: |
| 80 | + self.speak("Multiple entries found. Please be more specific.") |
| 81 | + except wikipedia.exceptions.PageError: |
| 82 | + self.speak("No Wikipedia page found for that topic.") |
| 83 | + |
| 84 | + # ------------------------------------------------------------------ |
| 85 | + # Main loop |
| 86 | + # ------------------------------------------------------------------ |
| 87 | + |
| 88 | + def run(self) -> None: |
| 89 | + """Start the assistant and listen for commands indefinitely.""" |
| 90 | + self.greet() |
| 91 | + while True: |
| 92 | + query = self.listen() |
| 93 | + if query is None: |
| 94 | + continue |
| 95 | + query = query.lower() |
| 96 | + |
| 97 | + if "open google" in query: |
| 98 | + self.speak("What do you want to search for?") |
| 99 | + term = self.listen() |
| 100 | + if term: |
| 101 | + self.open_google(term) |
| 102 | + elif "open geeksforgeeks" in query: |
| 103 | + self.speak("Opening Geeks for Geeks") |
| 104 | + self.open_url("https://www.geeksforgeeks.com") |
| 105 | + elif "which day is it" in query: |
| 106 | + self.tell_day() |
| 107 | + elif "tell me the time" in query: |
| 108 | + self.tell_time() |
| 109 | + elif "from wikipedia" in query: |
| 110 | + self.wikipedia_search(query.replace("from wikipedia", "").strip()) |
| 111 | + elif "your name" in query: |
| 112 | + self.speak(f"I am {self.name}, your desktop assistant.") |
| 113 | + elif "exit" in query or "bye" in query: |
| 114 | + self.speak("Goodbye! Have a great day!") |
| 115 | + break |
| 116 | + else: |
| 117 | + self.speak("Sorry, I didn't understand that command.") |
0 commit comments