Search
⌃K

Create Your Own Voice Assistant in Python

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. 1.
    Load in our imports
  2. 2.
    Configure the text to speech
  3. 3.
    Define our speech function
main.py
1
import speech_recognition as sr
2
import pyttsx3
3
import random
4
from datetime import datetime
5
import os
6
​
7
​
8
engine = pyttsx3.init()
9
voices = engine.getProperty('voices')
10
engine.setProperty('voice', voices[0].id)
11
​
12
def speak(audio):
13
engine.say(audio)
14
engine.runAndWait()
15
​
16
​
17
​
18
​
19
def takeCommand():
20
21
r = sr.Recognizer()
22
23
with sr.Microphone() as source:
24
25
print("Now listening")
26
r.pause_threshold = 1
27
audio = r.listen(source)
28
29
try:
30
print("Deciphering")
31
query = r.recognize_google(audio, language ='en-in')
32
print("You Said: " + query)
33
34
except Exception as e:
35
print(e)
36
print("Did not hear anything")
37
return "None"
38
39
return query
40
​
41
​
42
while True:
43
command = takeCommand()
44
#Conversational
45
if 'how are you' in command:
46
speak("I'm doing well")
47
if 'thank you' in command:
48
speak("Anytime")
49
​
50
#Tasks
51
if 'time' in command:
52
now = datetime.now()
53
current_time = now.strftime("%H:%M:%S")
54
speak("The Current Time is " + current_time)
55
if 'random number' in command:
56
randInt = random.randint(0, 10)
57
speak("A random number between 0 and 10 is " + str(randInt))
58
if 'open slack' in command:
59
os.system('open /Applications/Slack.app')
60
speak("Opening Slack")
61
if 'close slack' in command:
62
os.system('open /Applications/Slack.app')
63
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!