Hello, friends! In this video, we will demonstrate how to design a simple voice assistant using Python. You’ll learn how to integrate the Google API to capture and process user input through speech. The voice assistant listens to your spoken commands and takes action accordingly. For example, if you say "search," it will search the web for the text you’ve spoken. If you say "time," it will respond with the current system date and time.
We’ll guide you step-by-step through the process, so you’ll understand how the voice assistant interprets your commands and triggers the appropriate actions. This basic model can easily be expanded to create more advanced voice assistants with additional capabilities, such as setting reminders, controlling smart devices, or even having more complex interactions with users.
The code used in the video is given below:
import speech_recognition as sr
import pyttsx3
import datetime
import webbrowser
import sys # Import sys module
def speak(text):
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
def listen():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)
try:
command = recognizer.recognize_google(audio)
print("You said:", command)
return command.lower()
except sr.UnknownValueError:
print("Sorry, I could not understand the audio.")
return ""
except sr.RequestError:
print("Could not request results, please check your internet connection.")
return ""
def respond_to_command(command):
if "time" in command:
current_time = datetime.datetime.now().strftime("%H:%M:%S")
speak(f"The time is {current_time}")
elif "search" in command:
speak("What do you want to search for?")
query = listen()
if query:
url = f"https://www.google.com/search?q={query}"
webbrowser.open(url)
speak(f"Here are the search results for {query}")
elif "exit" in command or "stop" in command:
speak("Goodbye!")
sys.exit() # Use sys.exit() to stop the script
else:
speak("I'm sorry, I didn't understand that.")
if _name_ == "__main__":
speak("Hello! How can I assist you today?")
while True:
user_command = listen()
if "exit" in user_command or "stop" in user_command: # Double-check in main loop
speak("Goodbye!")
break # Break the loop safely
respond_to_command(user_command)
Watch video Building a Python Voice Assistant online without registration, duration hours minute second in high quality. This video was added by user Dr. Himani Mittal 15 February 2025, don't forget to share it with your friends and acquaintances, it has been viewed on our site 27 once and liked it 5 people.