Categories We Write About

Build a voice assistant with Python

Building a voice assistant with Python involves integrating several components like speech recognition, text-to-speech, and command handling. Here’s a step-by-step guide to create a simple yet functional voice assistant using popular Python libraries:


1. Install Required Libraries

You’ll need the following Python packages:

bash
pip install SpeechRecognition pyttsx3 pyaudio

If pyaudio fails to install, on Windows use:

bash
pip install pipwin pipwin install pyaudio

2. Basic Voice Assistant Code

python
import speech_recognition as sr import pyttsx3 import datetime import webbrowser import os # Initialize the text-to-speech engine engine = pyttsx3.init() engine.setProperty('rate', 150) # Speed percent (can go over 100) engine.setProperty('volume', 1) # Max volume # Function to convert text to speech def speak(text): engine.say(text) engine.runAndWait() # Function to take command from microphone def take_command(): recognizer = sr.Recognizer() with sr.Microphone() as source: print("Listening...") recognizer.pause_threshold = 1 audio = recognizer.listen(source) try: print("Recognizing...") command = recognizer.recognize_google(audio, language='en-in') print(f"User said: {command}n") except Exception as e: print("Sorry, could not recognize. Please say that again...") return "None" return command.lower() # Function to process the voice command def process_command(command): if 'time' in command: str_time = datetime.datetime.now().strftime("%H:%M:%S") speak(f"The current time is {str_time}") 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 'play music' in command: music_dir = 'C:\Users\Public\Music' songs = os.listdir(music_dir) os.startfile(os.path.join(music_dir, songs[0])) elif 'your name' in command: speak("I am your personal assistant.") elif 'exit' in command or 'quit' in command: speak("Goodbye!") exit() else: speak("Sorry, I can't perform that task yet.") # Main loop if __name__ == "__main__": speak("Hello! I am your voice assistant. How can I help you today?") while True: user_command = take_command() if user_command != "None": process_command(user_command)

3. Add More Features

You can expand the assistant with:

  • Weather Updates: Use an API like OpenWeatherMap.

  • News Headlines: Use requests with a news API.

  • Custom Commands: Add more elif conditions in process_command.

  • AI Integration: Add OpenAI’s API for chatbot features.

  • GUI: Use tkinter or PyQt for visual interface.


4. Tips for Improvement

  • Add error handling for each functionality.

  • Make it modular: split functions into separate files or classes.

  • Use threading to manage long-running tasks like music without blocking the assistant.

  • Implement wake word detection using libraries like snowboy or porcupine.


This voice assistant is basic but gives a solid foundation. You can progressively enhance it into a more intelligent virtual assistant by integrating NLP models and APIs.

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories We Write About