The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Create a voice assistant with Python

Creating a simple voice assistant in Python involves integrating several libraries for speech recognition, text-to-speech, and possibly natural language processing. Below is a complete example of a basic voice assistant using Python that can recognize speech, respond with voice, and perform some basic commands like web searches, time queries, and opening websites.

python
import speech_recognition as sr import pyttsx3 import datetime import webbrowser import os # Initialize the speech engine engine = pyttsx3.init() voices = engine.getProperty('voices') engine.setProperty('voice', voices[1].id) # Use female voice, use voices[0] for male engine.setProperty('rate', 150) # Speed of speech def speak(text): engine.say(text) engine.runAndWait() def greet_user(): hour = datetime.datetime.now().hour if 5 <= hour < 12: speak("Good morning!") elif 12 <= hour < 18: speak("Good afternoon!") else: speak("Good evening!") speak("I am your assistant. How can I help you today?") def take_command(): recognizer = sr.Recognizer() with sr.Microphone() as source: print("Listening...") recognizer.pause_threshold = 1 recognizer.adjust_for_ambient_noise(source) audio = recognizer.listen(source) try: print("Recognizing...") query = recognizer.recognize_google(audio, language='en-US') print(f"User said: {query}") except sr.UnknownValueError: speak("Sorry, I did not catch that. Could you please repeat?") return "None" except sr.RequestError: speak("Sorry, I'm having trouble connecting to the speech service.") return "None" return query.lower() def perform_task(command): if "your name" in command: speak("I am your Python voice assistant.") elif "time" in command: time = datetime.datetime.now().strftime("%I:%M %p") speak(f"The current time is {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 "search" in command: search_term = command.replace("search", "").strip() speak(f"Searching for {search_term}") webbrowser.open(f"https://www.google.com/search?q={search_term}") elif "open notepad" in command: speak("Opening Notepad") os.system("notepad") elif "exit" in command or "quit" in command or "stop" in command: speak("Goodbye! Have a great day.") exit() else: speak("I'm sorry, I can't help with that yet.") if __name__ == "__main__": greet_user() while True: command = take_command() if command != "None": perform_task(command)

Required Python Packages

Install the necessary libraries using pip:

bash
pip install SpeechRecognition pyttsx3 pyaudio

Note: If pyaudio fails to install, on Windows use:

bash
pip install pipwin pipwin install pyaudio

Features in This Assistant

  • Speech Recognition: Converts your voice to text using Google Speech Recognition.

  • Text to Speech (TTS): Speaks back to the user using pyttsx3.

  • Command Execution: Recognizes and performs tasks like telling the time, opening websites, and searching the web.

This code offers a foundational voice assistant you can expand with more sophisticated capabilities like NLP, app control, or home automation.

Share this Page your favorite way: Click any app below to share.

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

We respect your email privacy

Categories We Write About