Curious Python Voice Assistant

Your Immagination is the only limit what you can create.

Zulqai

Python Voice Assistant: Free Code to Build Your Own

Are you interested in building your own voice assistant like Siri or Alexa, but using Python? You're in the right place! With Python's powerful libraries and tools, you can create a simple voice assistant that listens to your commands, speaks back, and performs various tasks like opening websites, telling the time, or even cracking a joke. In this post, we’ll guide you through the process and provide free, working code to get you started.

What is a Voice Assistant?

A voice assistant is a software program that can understand spoken commands and provide responses or perform actions. Some popular voice assistants include Amazon’s Alexa, Google’s Assistant, and Apple’s Siri. While these commercial assistants are sophisticated, creating a basic voice assistant in Python is a great way to understand how they work and have a fun, interactive project to build on.

Why Use Python for a Voice Assistant?

Python is a great choice for building a voice assistant due to:

  • Simplicity: Python is known for being beginner-friendly, making it easier to get started.
  • Powerful Libraries: Python has libraries like pyttsx3 for text-to-speech and SpeechRecognition for speech-to-text, which simplifies the process of building a voice assistant.
  • Flexibility: You can customize the assistant to perform any task you want, such as opening websites, checking the time, or even telling jokes using pyjokes.

Features of This Python Voice Assistant

In this guide, you’ll learn how to build a basic voice assistant with these features:

  • Speech Recognition: The assistant listens for voice commands.
  • Text-to-Speech: The assistant responds with spoken feedback.
  • Web Browsing: Open YouTube or play music with just a voice command.
  • Time Announcement: Get the current time.
  • Joke Telling: Lighten up your day with a joke.

Getting Started

Before we dive into the code, make sure you have the following Python libraries installed. You can install them using pip:

pip install pyttsx3 SpeechRecognition pyjokes

Python Voice Assistant Code

Here's the free code to build your Python voice assistant, named Curious:

import pyttsx3
import speech_recognition as sr
import webbrowser
import datetime
import pyjokes

def st():
    recognizer = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening---------")
        recognizer.adjust_for_ambient_noise(source, duration=0.5)
        audio = recognizer.listen(source)
        try:
            print("Recognizing-----")
            data = recognizer.recognize_google(audio)
            print(data)
            return data
        except sr.UnknownValueError:
            print("Not Understanding")
            return ""  # Return empty string when recognition fails

def tts(text):
    engine = pyttsx3.init()
    voices = engine.getProperty("voices")
    engine.setProperty('voice', voices[0].id)
    engine.setProperty('rate', 150)
    engine.say(text)
    engine.runAndWait()

if __name__ == '__main__':
    if "curious" in st().lower():
        tts("Curious is here, how can I help you today?")
        while True:
            data1 = st().lower()

            if "your name" in data1:
                name = "My name is Curious"
                tts(name)

            elif "time" in data1:
                current_time = datetime.datetime.now().strftime("%I:%M %p")
                tts(f'The current time is {current_time}, anything else you want from me?')

            elif "youtube" in data1:
                webbrowser.open("https://www.youtube.com/")
                tts('YouTube has been opened!')

            elif "music" in data1:
                webbrowser.open("https://www.youtube.com/watch?v=_iF7lkXKHlA")
                tts('Music has been played for you.')

            elif "joke" in data1:
                joke1 = pyjokes.get_joke(language="en", category="neutral")
                print(joke1)
                tts(joke1)

            elif 'stop' in data1:
                tts('Thank you')
                break

How the Code Works

  1. Speech Recognition:
  • The function st() listens for the user’s voice using the SpeechRecognition library and converts it into text using Google’s speech recognition API.
  1. Text-to-Speech:
  • The tts() function allows the assistant to speak back to you using the pyttsx3 library.
  1. Voice Commands:
  • The assistant listens for specific commands like asking for the time, opening YouTube, playing music, or even telling a joke using the pyjokes library.
  1. Control:
  • You can stop the assistant by saying “stop,” and it will thank you and exit the program.

Customizing Your Assistant

You can easily extend this voice assistant by adding more functionalities. For example:

  • Search the web: Add a command that opens a Google search for a user’s query.
  • Weather updates: Integrate a weather API to provide current weather information.
  • Reminders or timers: Implement a simple reminder system.

Conclusion

With just a few lines of code, you can create a powerful voice assistant in Python that listens to your commands, talks back, and helps you with everyday tasks. The possibilities for customization are endless, so feel free to add more commands or features that suit your needs.

Download this free Python voice assistant code and start building your own personalized assistant today!

import pyttsx3

import speech_recognition as sr

import webbrowser

import datetime

import pyjokes

def st():

    recognizer = sr.Recognizer()

    with sr.Microphone() as source:

        print("Listening---------")

        recognizer.adjust_for_ambient_noise(source, duration=0.5)

        audio = recognizer.listen(source)

        try:

            print("Recognizing-----")

            data = recognizer.recognize_google(audio)

            print(data)

            return data

        except sr.UnknownValueError:

            print("Not Understanding")

            return ""  # Return empty string when recognition fails

def tts(text):

    engine = pyttsx3.init()

    voices = engine.getProperty("voices")

    engine.setProperty('voice', voices[0].id)

    engine.setProperty('rate', 150)

    engine.say(text)

    engine.runAndWait()

if __name__ == '__main__':

    if "curious" in st().lower():

        tts("Curious is here, how can I help you today?")

        while True:

            data1 = st().lower()

            if "your name" in data1:

                name = "My name is Curious"

                tts(name)

            elif "time" in data1:

                current_time = datetime.datetime.now().strftime("%I:%M %p")

                tts(f'The current time is {current_time}, anything else you want from me?')

            elif "youtube" in data1:

                webbrowser.open("https://www.youtube.com/")

                tts('YouTube has been opened!')

            elif "music" in data1:

                webbrowser.open("https://www.youtube.com/watch?v=_iF7lkXKHlA")

                tts('Music has been played for you.')

            elif "joke" in data1:

                joke1 = pyjokes.get_joke(language="en", category="neutral")

                print(joke1)

                tts(joke1)

            elif 'stop' in data1:

                tts('Thank you')

                break

You can follow me on social media to get latest projects codes, As an AI Enthusiasit I will highly recommend you to understand and write the code as your self to get the maximum value from any online tutorial either in the form of video, blog or book.

If you are passionate about Machine learning and ai, you can reach out to me, we can callobratily built intresting projects.

Leave a Reply

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