We all have heard about AI assistants such as Jarvis from Iron Man and the real-life versions such as Amazon Alexa and Google Home. But what if you could make your own? Well, with python and some speech recognition magic, you can!
Making your own personal assistant allows you to add customized features specific to your needs allowing you to live a more efficient lifestyle!
Virtual assistants are programmed applications that recognize human language and commands which then result in the program performing tasks. Virtual assistants typically perform simple jobs for users, such as adding tasks to a calendar; providing information that would normally be searched in a web browser; or controlling and checking the status of smart home devices, including lights, cameras and thermostats.
Virtual Assistants are part of the Internet of Things. The Internet of Things is a network of ‘things’ that are able to collect and transfer data over a wireless network without human intervention. You can read more about them here:
The core of these assistants is made of speech recognition programs and an audio engine. Once you have this core, the possibilities are endless and you can add as many features as you like.
For this project, we will be using Python, I will be using 3.6. I will also be using the Pycharm IDE (which you can download here), though you can use anything.
To get started, let’s make sure we are working in a virtual environment to make sure the packages we install don’t affect the interpreter that runs globally.
Once the virtual environment is set up, we can start to install packages. For the core code, I will be using the following commands:
pip install pyttsx3
pip install speechRecognition
pip install PyAudio
These packages will allow the machine to speak and understand words and convert text to speech.
Once you have installed the packages, we can initialize the audio engine.
import pyttsx3engine = pyttsx3.init()
Yes, it’s that simple. Additionally, if you want to change the voice, you can use the following code.
import pyttsx3engine = pyttsx3.init()
voices = engine.getProperty('voices')engine.setProperty('voice', voices[ENTER VOICE NUMBER].id)
You can change the ‘ENTER VOICE NUMBER’ to change the voice.
This code only initialized the engine and allowed you to choose the voice type. But for the machine to output sound, we need to code the following function:
def assistantResponse(text):
print(text)engine.say(text)
engine.runAndWait()
With this code, we are now able to make our machine say anything. To test it out, run the following code:
assistantResponse("Hello World")
Here is the combined code for the speech function:
import pyttsx3engine = pyttsx3.init()def assistantResponse(text):
print(text)engine.say(text)
engine.runAndWait()
Now with the speech function complete, we now need to address programming the machine to recognize words.
import speech_recognition as srdef recordAudio():
# Record The Audio
r = sr.Recognizer() # Creating a recognizer object# Open the microphone and start recording
with sr.Microphone() as source:
print('Say something!')
audio = r.listen(source)# Use google's speech recognition
data = ''
try:
data = r.recognize_google(audio)
print('You said: ' + data)
except sr.UnknownValueError: # Checking for unknown errors
print('Google Speech Recognition could not understand the audio')
except sr.RequestError as c:
print('Request results from Google Speech Recognition service error')return data
This code starts off by recognizing when something is being said which then opens the microphone. It then saves the recognized text. If audio isn’t recognized, then the code prints put: ‘Google Speech Recognition could not understand the audio’. If it encounters an error, it then prints: ‘Request results from Google Speech Recognition service error’.
We now have the core of our assistant. We can now start adding features to expand the capabilities of this assistant.
Let’s start with something simple. This greeting function recognizes if the user has said a greeting. If true, then it will output a responding greeting word
def greeting(text):
Greeting_Inputs = ['hi', 'hey', 'hola', 'bonjour', 'hello']
Greeting_Response = ['hi', 'hey', 'hola', 'greetings', 'bonjour', 'hello']for word in text.split():
if word.lower() in Greeting_Inputs:
return random.choice(Greeting_Response)
return ''
Many assistants have wake words such as “Hey Alexa” and “Hey Google”.
def wakeWord(text):
WAKE_WORD = ['ENTER NAME HERE']text = text.lower()
for phrase in WAKE_WORD:
if phrase in text:
return True
return False
Now we can take a while loop in which the system is constantly looking for the wake word. Once it recognizes the wake word, it will look for any keywords that can trigger further action.
while True:
text = recordAudio()
responses = ''if wakeWord(text.lower()) == True:
Once we have this setup, we can add the following lines of code under the if statement to allow the assistant to recognize when a greeting has been said.
`try:
responses = responses + greeting(text)
except Exception as e:
responses = responses + ''
As we add more and more functions, we will continue to add lines of code to activate the functions.
Adding a date function is very useful and is implemented in most assistants. You will need to start by installing the DateTime package
pip install DateTime
We will also need the Calender package but it comes pre-installed with Python. Once installed, make the following function:
import detetime
import calenderdef getDate():
now = datetime.datetime.now()
my_date = datetime.datetime.today()
weekday = calendar.day_name[my_date.weekday()]
monthNum = now.month
dayNum = now.daymonth_names = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October',
'November', 'December']ordinalNumbers = ['1st', '2nd', '3rd', '4th', '5th', '6th', '7th', '8th', '9th', '10th', '11th', '12th', '13th','14th', '15th', '16th', '17th', '18th', '19th', '20th', '21st', '22nd', '23rd', '24th', '25th', '26th', '27th', '28th', '29th', '30th', '31st']
return 'Today is ' + weekday + ', ' + month_names[monthNum - 1] + ' the ' + ordinalNumbers[dayNum - 1]
We will also need to set this up under the if statement that looks for keywords
if 'date' in text:
get_date = getDate()
responses = responses + ' ' + get_date
We can also add an if statement that tells the user if a feature hasn’t been added yet.
if responses == '':
responses = responses + "I'm Sorry I Can't Do That Yet"
I have included the compiled code below. Note: the features I mentioned here are only the basics. You can enter as many APIs as you want or need. I ended up adding many more. There are great tutorials on Youtube explaining how you can use different APIs.
Here is the code:
#Importing the Modules
import calendar
import datetime
import pyttsx3
import speech_recognition as sr# Initializing the audio engine
engine = pyttsx3.init()# Record audio and return as a string
def recordAudio():
# Record The Audio
r = sr.Recognizer() # Creating a recognizer object# Open the microphone and start recording
with sr.Microphone() as source:
print('Say something!')
audio = r.listen(source)# Use google's speech recognition
# Function for response
data = ''
try:
data = r.recognize_google(audio)
print('You said: ' + data)
except sr.UnknownValueError: # Checking for unknown errors
print('Google Speech Recognition could not understand the audio')
except sr.RequestError as c:
print('Request results from Google Speech Recognition service error')
def assistantResponse(text):
print(text)engine.say(text)
engine.runAndWait()return data
# Random Greeting Function
def greeting(text):
Greeting_Inputs = ['hi', 'hey', 'hola', 'bonjour', 'hello']
Greeting_Response = ['hi', 'hey', 'hola', 'greetings', 'bonjour', 'hello']for word in text.split():
# Wake word function
if word.lower() in Greeting_Inputs:
return random.choice(Greeting_Response)
return ''
def wakeWord(text):
WAKE_WORD = ['ENTER NAME HERE']text = text.lower()
for phrase in WAKE_WORD:
# Function to get date
if phrase in text:
return True
return False
def getDate():
now = datetime.datetime.now()
my_date = datetime.datetime.today()
weekday = calendar.day_name[my_date.weekday()]
monthNum = now.month
dayNum = now.daymonth_names = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October',
'November', 'December']ordinalNumbers = ['1st', '2nd', '3rd', '4th', '5th', '6th', '7th', '8th', '9th', '10th', '11th', '12th', '13th','14th', '15th', '16th', '17th', '18th', '19th', '20th', '21st', '22nd', '23rd', '24th', '25th', '26th', '27th', '28th', '29th', '30th', '31st']
return 'Today is ' + weekday + ', ' + month_names[monthNum - 1] + ' the ' + ordinalNumbers[dayNum - 1]
# The Brain
while True:
text = recordAudio()
responses = ''if wakeWord(text.lower()) == True:
try:
responses = responses + greeting(text)
except Exception as e:
responses = responses + ''if 'date' in text:
if responses == '':
get_date = getDate()
responses = responses + ' ' + get_date
responses = responses + "I'm Sorry I Can't Do That Yet"# Response
assistantResponse(responses)
While this assistant is very basic and only is a small demonstration, it represents the beginning of something big. As technology continues to advance, the very basic stage we are at now will evolve into a more complex network of assistants and smart objects working together to make the world more efficient, safe and sustainable.
As mentioned before, AI assistants are part of the internet of things (IoT). With continued advances in this field, humanity will be able to accomplish more in the same amount of time. IoT is being used in many fields ranging from residential to commercial. Although this technology has a long way to go, once it is ready, it will revolutionize the world.