Create a Robust AI Voice Assistant using Python with Access to Source Code

Posted by


Creating a powerful AI voice assistant in Python can be a fun and rewarding project that allows you to learn about artificial intelligence, speech recognition, and natural language processing. In this tutorial, we will walk through the process of building a simple voice assistant that can perform basic tasks such as sending emails, playing music, and providing weather updates.

To get started, we will need to install a few Python libraries that will help us with speech recognition and natural language processing. We will be using the following libraries for this tutorial:

  1. SpeechRecognition: A library that provides an easy way to access speech recognition capabilities.
  2. Pyttsx3: A text-to-speech library that allows our assistant to speak.
  3. Pywhatkit: A library that allows us to perform a variety of tasks such as playing music and sending emails.
  4. OpenWeatherMap API: An API that provides weather data.

You can install these libraries using pip by running the following commands:

pip install SpeechRecognition
pip install pyttsx3
pip install pywhatkit
pip install requests

Now that we have our libraries installed, let’s start building our AI voice assistant. We will create a Python script named "assistant.py" and start by importing the required libraries:

import speech_recognition as sr
import pyttsx3
import pywhatkit
import requests
import json

Next, we will define a function that allows our assistant to listen to our commands and convert them into text using speech recognition:

def take_command():
    recognizer = sr.Recognizer()
    microphone = sr.Microphone()

    with microphone as source:
        print("Listening...")
        recognizer.adjust_for_ambient_noise(source)
        audio = recognizer.listen(source)

    try:
        command = recognizer.recognize_google(audio)
        print(f"You said: {command}")
        return command
    except sr.UnknownValueError:
        print("Sorry, I could not understand your command.")
        return ""
    except sr.RequestError as e:
        print(f"Sorry, an error occurred: {e}")
        return ""

Now, we will define a function that allows our assistant to speak:

def speak(text):
    engine = pyttsx3.init()
    engine.say(text)
    engine.runAndWait()

Next, we will define a function that will handle the various commands our assistant can perform:

def process_command(command):
    if "play" in command:
        song = command.replace("play", "").strip()
        speak(f"Playing {song}")
        pywhatkit.playonyt(song)
    elif "send email" in command:
        speak("Whom should I send the email to?")
        recipient = take_command()
        speak("What should be the subject of the email?")
        subject = take_command()
        speak("What should be the body of the email?")
        body = take_command()
        # Add code here to send email using smtp library
    elif "weather" in command:
        speak("Which city's weather would you like to know?")
        city = take_command()
        url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid=<YOUR_API_KEY>"
        response = requests.get(url)
        data = json.loads(response.text)
        weather = data["weather"][0]["description"]
        temperature = data["main"]["temp"]
        speak(f"The weather in {city} is {weather} with a temperature of {temperature} Kelvin.")
    else:
        speak("Sorry, I could not recognize that command.")

Finally, we will create a loop that continuously listens for commands and processes them:

speak("Hello! I am your AI voice assistant. How can I help you today?")
while True:
    command = take_command()
    if command.lower() == "exit":
        speak("Goodbye!")
        break
    process_command(command)

That’s it! You have now created a simple AI voice assistant in Python that can perform basic tasks. Feel free to expand on this project by adding more functionality and customizing the assistant to suit your needs. Have fun experimenting with different commands and exploring the capabilities of your voice assistant!

0 0 votes
Article Rating
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@arun8135
2 months ago

bro full tutorial??

@huseyin3799
2 months ago

not working