import tkinter as tk import speech_recognition as sr import pyttsx3 import wikipedia import datetime # Initialize the speech synthesizer engine = pyttsx3.init() engine.setProperty('rate', 150) # Speech rate engine.setProperty('volume', 0.9) # Volume level # Function for the bot to speak def speak(text): engine.say(text) engine.runAndWait() # Function to listen to the user def listen(): recognizer = sr.Recognizer() with sr.Microphone() as source: print("I'm listening...") recognizer.adjust_for_ambient_noise(source) # Adjust for ambient noise try: audio = recognizer.listen(source, timeout=10) # Limit the waiting time command = recognizer.recognize_google(audio, language="en-US") # Process in English print(f"You said: {command}") return command.lower() except sr.UnknownValueError: speak("Sorry, I didn't understand. Can you repeat?") return None except sr.RequestError: speak("Error accessing the speech recognition service.") return None # Main function to process commands def process_command(command): if "time" in command: now = datetime.datetime.now().strftime("%H:%M") speak(f"The time is now {now}.") elif "wikipedia" in command: speak("What would you like to search on Wikipedia?") query = listen() if query: try: result = wikipedia.summary(query, sentences=2) speak(f"According to Wikipedia: {result}") except wikipedia.exceptions.PageError: speak("Sorry, I couldn't find any information on that.") except wikipedia.exceptions.DisambiguationError: speak("The term is ambiguous. Please be more specific.") elif "exit" in command: speak("Shutting down the system. Goodbye!") root.destroy() else: speak("Sorry, I can't process that command yet.") # Function to start the assistant def start_assistant(): speak("Hello, I am your virtual assistant. How can I help you?") command = listen() if command: process_command(command) # Create the GUI root = tk.Tk() root.title("Voice Assistant") root.geometry("300x200") root.configure(bg="#2B2B2B") # Add a button to start the assistant start_button = tk.Button(root, text="Start Assistant", command=start_assistant, bg="green", fg="white", font=("Helvetica", 14)) start_button.pack(pady=50) # Run the GUI root.mainloop()