My Trials and Tribulations with Machine Learning: Part Three

AI-Powered Virtual Assistants & How to Make One Of Your Own

--

This is the third of a four-part machine learning series: the subtopics I’ve learned, projects where I’ve used machine learning, the mistakes I’ve made along the way — and how you can learn machine learning, too. Read the series from the beginning here.

Cortana from the Halo games.
Cortana, circa 2001.

What is an Intelligent Virtual Assistant (IVA)?

Put simply from Wikipedia, IVAs perform tasks or services for an individual based on commands or questions. Sometimes the term “chatbot” is used to refer to virtual assistants generally or specifically accessed by online chat.

When you say, “Alexa, play Mambo No. Five,” Alexa will respond by saying, “Mambo Number Five (A Little Bit Of…), by Lou Bega. Starting now on Amazon Music.” After this, you get a little bit of Monica in your life as Alexa plays the song you requested.

This is all very complex for a program. Let’s break down the request and response.

Picture of Lou Bega.
Ladies and gentlemen, this is Lou Bega.

How do IVAs work?

Request:

  • “Alexa…” Alexa recognizes the beginning of a query and listens for more input.
  • “…play…” Alexa knows to listen for music, either an artist, a radio, a song or even a stand up bit.
  • “…Mambo No. Five.” Alexa fetched the song most closely related to the request, searching on Amazon Music unless stated otherwise.

Response:

  • “Mambo Number Five, (A Little Bit Of…)…” With this specific song (and a few others with second titles), Alexa is cognizant of its inflection and timing, where it knows to pause and group the words in parentheses together. Don’t believe me? If you have an Alexa, go ahead and try it out. We will briefly touch on these inflections and timing more a little later.
  • “Starting now, on Amazon Music. ‘Ladies and gentlemen, this is Mambo Number Five…’” Alexa understands that the request is to play the song in the query, and since it found “Mambo No. Five (A Little Bit Of…)” on Amazon Music, it returns the song.

How can I create one of my own?

Creating your own virtual assistant is a lot easier than you might think. Obviously, to build an ultra complex one such as Siri, Alexa or Google Assistant, I doubt any Medium article would cover it all, but this is just to get you started.

In this example of building our own IVA, we’re going to hook up some chat functions for companionship purposes, check Wikipedia for a brief summary of something, and allow you to send an email.

We are going to use Python, which is the generally preferred language for AI, mostly because of its stellar packages and strength. Python is also a generally simple language in terms of readability, so it can make these complex systems and algorithms pretty concise.

Package Checklist:

Make sure you pip3 install packageName for the following packages, if they aren’t installed on your device already:

  • SpeechRecognition: Library for performing speech recognition, with support for several engines and APIs, online and offline.
  • Pyttsx3: A text-to-speech conversion library in Python. Unlike alternative libraries, it works offline ̶,̶ ̶a̶n̶d̶ ̶i̶s̶ ̶c̶o̶m̶p̶a̶t̶i̶b̶l̶e̶ ̶w̶i̶t̶h̶ ̶b̶o̶t̶h̶ ̶P̶y̶t̶h̶o̶n̶ ̶2̶ ̶a̶n̶d̶ ̶3̶. (Rest in peace, Python 2. You were good to us all.)
  • Wolframalpha: Python Client built against the Wolfram|Alpha v2.0 API.
  • PyAudio: Python Bindings for PortAudio.
If you didn’t know the Club Penguin meme before this article and this is the one thing you learned from this article, I won’t even be mad. And, if you didn’t know this already, Python 2 was deprecated starting Jan 1, 2020.

Getting started after installing the packages

Once everything is imported, we can now begin writing our Python code. I am naming my virtual assistant Mímir, the Norse god of knowledge and wisdom. Mímir was beheaded during the Æsir-Vanir War, and afterward, Odin carried around his head to gain knowledge and counsel from it.

Now you, too, can have your very own Norse god head to carry around for knowledge!

A GItHub Gist of the .py file I created. Check out my GitHub to see the full code!

With all of these libraries Python provides, it is very simple to create commands and personalize your queries.

Hard mode: Get a Raspberry Pi and code that to be your virtual assistant! You can do so much with this basic understanding of IVAs, so don’t be afraid to get creative with it!

Creating your own intelligent virtual assistant is not only simple if you keep organized and install the right packages, but can be a lot of fun to play around with the voice, tempo, volume and more.

I hope you enjoyed learning about intelligent virtual assistants with me. If you like what you read or have any questions, comments or would like to collaborate with me on an article (or speak with me about job opportunities at your workplace), feel free to tweet me @mackied0g or connect with me on LinkedIn — don’t be shy, I love feedback and collaboration. Be sure to come back to check out my Part Four of my Machine Learning series: Building a Recommendation Engine with Amazon Personalize!

Resources:

Don’t forget to give us your 👏 !

--

--

NY-based techie. Passionate about STE(a)M, security, AI/ML, memes and omitting the Oxford comma.