Build a voice assistant that listens to your voice, understands commands, and responds by speaking - open websites, tell the time, and more.
Open your terminal (Command Prompt on Windows) and run these commands one by one:
pip install SpeechRecognition pyttsx3 pyaudio
pip install pipwinpipwin install pyaudio
After installing, create a new file called voice_assistant.py in a folder of your choice.
Before writing code, understand what each library does:
Copy this complete code into your voice_assistant.py file:
# Voice Assistant - NKDevSpace Tutorial
import speech_recognition as sr
import pyttsx3
import datetime
import webbrowser
# --- Initialize the text-to-speech engine ---
engine = pyttsx3.init()
engine.setProperty('rate', 160) # Speed of speech (words per minute)
engine.setProperty('volume', 1.0) # Volume: 0.0 to 1.0
def speak(text):
"""Make the assistant speak out loud."""
print(f"Assistant: {text}")
engine.say(text)
engine.runAndWait()
def take_command():
"""Listen to the microphone and return the spoken text."""
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("\nListening... (speak now)")
recognizer.pause_threshold = 1 # Wait 1 second of silence before stopping
audio = recognizer.listen(source)
try:
print("Recognizing...")
command = recognizer.recognize_google(audio)
print(f"You said: {command}")
return command.lower()
except sr.UnknownValueError:
speak("Sorry, I could not understand. Please say that again.")
return ""
except sr.RequestError:
speak("Sorry, I am having trouble connecting. Check your internet.")
return ""
def run_assistant():
"""Process the voice command and perform the right action."""
command = take_command()
if not command:
return
if "time" in command:
current_time = datetime.datetime.now().strftime("%I:%M %p")
speak(f"The current time is {current_time}")
elif "date" in command:
today = datetime.datetime.now().strftime("%B %d, %Y")
speak(f"Today's date is {today}")
elif "open google" in command:
speak("Opening Google")
webbrowser.open("https://www.google.com")
elif "open youtube" in command:
speak("Opening YouTube")
webbrowser.open("https://www.youtube.com")
elif "open github" in command:
speak("Opening GitHub")
webbrowser.open("https://www.github.com")
elif "hello" in command or "hi" in command:
speak("Hello! I am your voice assistant. How can I help you?")
elif "your name" in command:
speak("I am NKBot, your personal voice assistant built with Python.")
elif "stop" in command or "exit" in command or "bye" in command:
speak("Goodbye! Have a great day.")
exit()
else:
speak(f"I heard you say: {command}. I don't know how to do that yet.")
# ── Main loop ──
speak("Voice Assistant is ready. Say a command.")
while True:
run_assistant()
engine = pyttsx3.init()engine.say(text) → engine.runAndWait()recognizer.recognize_google(audio).lower() so commands like "Open Google" and "open google" both work.except sr.UnknownValueErrorif "time" in commandwhile True: run_assistant()exit() to end the program.cd path\to\your\folder
cd C:\Users\YourName\Desktop
python voice_assistant.py