Create Your Own Voice Assistant in Python

Can you build your own voice assistant with Python and ditch your Alexa? Let's find out!

December 14, 2023 2 Min Read
Create Your Own Voice Assistant in Python
Create Your Own Voice Assistant in Python

Codesphere

From everyone in the Codesphere Team:)

Let’s be honest with ourselves, at some point in our lives we wanted our own Jarvis. There’s something incredibly awesome about having a personal voice assistant to be able to give us information, manage our schedules, and just help us out with our day to day lives.

In this article, we’ll build a foundation with python that you can use to build your own personal voice assistant.

How the Voice Assistant Works

The foundation of our program will be a speech recognition library and a Text to speech library(TTS) which will allow us to communicate with our script purely through audio.

The script will run on a loop of:

Waiting for a command from the user Understanding and fulfilling a command We’ll add some basic examples to do some small talk, tell the time, open apps and a few other things. This foundation can very easily be augmented to add whatever functionality you might want from your assistant. If you can do it in python, you can make the bot do it.

Setup

In a new python project, first install the speech recognition library with:

pip install SpeechRecognition

And install the TTS library with:

pip install pyttsx3

The Code

In a python file, we’re then going to:

  1. Load in our imports
  2. Configure the text to speech
  3. Define our speech function
import speech_recognition as sr
import pyttsx3
import random
from datetime import datetime
import os


engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)

def speak(audio):
    engine.say(audio)
    engine.runAndWait()




def takeCommand():
     
    r = sr.Recognizer()
     
    with sr.Microphone() as source:
         
        print("Now listening")
        r.pause_threshold = 1
        audio = r.listen(source)
  
    try:
        print("Deciphering")   
        query = r.recognize_google(audio, language ='en-in')
        print("You Said: " + query)
  
    except Exception as e:
        print(e)
        print("Did not hear anything") 
        return "None"
     
    return query


while True:
    command = takeCommand()
    #Conversational
    if 'how are you' in command:
        speak("I'm doing well")
    if 'thank you' in command:
        speak("Anytime")

    #Tasks
    if 'time' in command:
        now = datetime.now()
        current_time = now.strftime("%H:%M:%S")
        speak("The Current Time is " + current_time)  
    if 'random number' in command:
        randInt = random.randint(0, 10) 
        speak("A random number between 0 and 10 is " + str(randInt))
    if 'open slack' in command:
        os.system('open /Applications/Slack.app')
        speak("Opening Slack")
    if 'close slack' in command:
        os.system('open /Applications/Slack.app')
        speak("Closing Slack")

The bot above can fulfill the following commands:

  • Tell the time
  • Give a random number
  • Open and Close Slack

Building More

Whatever tasks you want your assistant to complete, you can now simply add another conditional to the list!

Of course, you can add a lot more complexity:

  • Power the assistant w/ Conversational AI via Rasa
  • Play music with the Spotify API
  • Pull information from wikipedia via Web Scraping That’s all for today!

About the Author

Create Your Own Voice Assistant in Python

Codesphere

From everyone in the Codesphere Team:)

We are building the next generation of Cloud, combining Infrastructure and IDE in one place, enabling a seamless DevEx and eliminating the need for DevOps specialists.

More Posts

Deploying Landscapes on Codesphere

Deploying Landscapes on Codesphere

Learn how to deploy and run multiple services that can independently scale vertically and horizontally within a single workspace. Suitable for hosting entire application landscapes.

Monitoring & Alerting

Monitoring & Alerting

Learn how to access Codesphere's built in resource monitoring and check the uptime of your applications.

Path-Based Routing

Path-Based Routing

Learn how to connect multiple independent applications to a single domain by associating different paths with distinct workspaces