banner



How To Create A Jarvis Like Ai Assistant

How to Build Your AI-Powered Virtual Assistant πŸŽ™

Have you wondered how to mimic popular virtual assistants such as Google, Siri, Alexa or even better, Iron Man's J.A.R.V.I.S?

Zaki Rangwala

Earlier this year, I stumbled open this fascinating article on how Facebook's Co-Founder and CEO, Mark Zuckerberg, built an AI assistant like Jarvis to virtually automate his home and become one step closer being the real-life Iron Man.

If you would like to learn more, check out this article from Mark :

So, that really inspired me to do the same, and although I had been holding off on it, I finally got started and let me tell you, it's incredible!!

See, the advantage of making your Virtual Assistant is that you can customize and personalize it to your liking making it tailored towards your hobbies and interests, resulting in a badass AI like J.A.R.V.I.S.

Next Step: Make the suit πŸš€ (Source: Giphy)

What is a Virtual Assistant?πŸ€”

If you are not already aware, a virtual assistant is essentially a software agent capable of performing tasks or services based on an individual's queries, commands or questions. It's a set of programs that can execute functions that the user would typically do. But if it can do what a human would typically do, doesn't it make it an AI? Not quite

Although Artificial Intelligence refers to the simulation of human intelligence and is programmed to mimic our actions and think like us, what we will be making can be considered as an AI, but many would disagree. So, let's not get too ahead of ourselves, as we'll return to this topic near the end of the article.

There are different kinds of assistants varying from industries like social media, marketing etc. Think like Google Assistant, Alexa, Siri or Cortona. It's not a new thing as these assistants have been on the market for years, but the rapid innovation in just a few recent years has been astounding.

Apple's HomePod, Amazon Echo and Google Home are some of the virtual assistant equipped speakers you might be used to be seeing (Source: New York Times)

This is since we are learning how to leverage machine learning algorithms, which, although make these machines more prone to making mistakes, allows the machine to understand and improve over time as the machine is continuously training itself to achieve the goal of becoming artificially intelligent eventually.

In simpler words, machine learning algorithms help cut down the code instead of explicitly programming each keyword a user would say that could trigger an action as it would just learn over time.

After reading all of this, you might think WOW! that's amazing and might go order one of those smart speakers. But despite the inherent "wow" factor that comes with using these assistants, you can find that these assistants don't cater to your specific needs.

So what can be done to make them more customizable and personalizable?

Well, you're in luck because it's relatively easy to learn how to build your own, and I'll be walking you right through it. So without further ado :

Building a Virtual Assistant πŸ”¨

For this project, we will be using Python, and so you will need a recent version installed (I recommend 3.7 or above). If not, you can download it from here.

When installing, make sure to add Python to PATH.

Before installing anything packages, I suggest making a python environment in the folder that we will be programming the assistant.

To do so, type the command py -m venv envin the terminal and wait for it to make the virtual environment. This is recommended, so any of the packages you install won't tamper with the python interpreter used globally on your machine but just the project.

To activate the virtual environment, type 'env\Scripts\activate.bat' and your screen should look like this.

Once we have done that, install the following packages using the 'pip install' command in your terminal:

          pip install pyttsx3            
pip install pypiwin32
pip install speechRecognition
pip install PyAudio

These libraries help you and the machine communicate with one another by converting text-to-speech and recognizing what the user is saying. If you have a problem with installing the last library, get it from here.

PyAudio Module

Make sure to get the .whl file built for the python version you have installed on your computer, which you can check by typing 'python' in the terminal.

Mine, for example, is Python 3.8.5 32 bit, so I would get the 'PyAudio‑0.2.11‑cp38‑cp38‑win32.whl' file.

Then you would navigate to where the .whl is installed use the 'pip install' command on the absolute location of the file as it would build it from source. For example : 'pip install C:\Users\USER\Downloads\PyAudio-0.2.11-cp38-cp38-win32.whl'

Once all those packages are installed, we will make the 'speak ' function like so

We are using the Speech API ('sapi5') from Microsoft, which will help the machine speak.

In this code fragment, we are initializing our Text-to-speech engine and calling in the 'speak' function to say any audio that is parsed in. VoiceID helps us determine what voice we would like our assistant to have, voices[0].id refers to male while voices[1].id refers to a female.

You can test it by calling the function like speak('Hello World') and running the code, this should result in the machine saying 'Hello World.'

If that doesn't work, then you need to use a different module. Do so by using the commands pip install gtts and pip install playsound in the terminal and make the new speech function.

We are using the Google Speech API to convert text-to-speech, test by calling speak("Hello World")

Once the speech function is ready, we will introduce the listen function, which allows the machine to recognize what you are saying by using the Google Speech API.

Install the module using pip install speechRecognition and create an object that will take the microphone input from the user. Adjust for ambient sound and give enough time to the user to speak and finally save the resulting text recognized by the machine into a variable.

Feel free to change the language by utilizing the two-letter code corresponding to your country

Now that we have the foundation set, the possibilities are endless; you can incorporate any APIs you would like, make a mini-program or even control your smart home. I'll walk you through some of them like retrieving game scores, getting weather forecasts, fetching movie ratings and even stock prices.

Greet Function ⌚

We will be creating a greet function where the program will greet you according to the time: Morning -> ''Good Morning''; Afternoon -> "Good Afternoon" like shown down below.

The Assistant will greet the user depending on the hour of the day

This function is relatively straightforward as we need to make some conditions by utilizing the built-in DateTime module that tells us the hour of the day

Real-Time Stock Price Function πŸ’²πŸ’²

We will be creating a function that will tell us the real-time stock price of any stock listed in any stock exchange. We can accomplish this feat by web-scraping Yahoo Finance stock price updates. All the user has to do is specify which stock's price they would like to retrieve. First, we need to install these modules that allow us to scrape the web pip install beautifulsoup4and pip install requests to extract any information from a webpage, which is a potent tool.

This function uses the CSS elements to find the price, title and currency of the stock. Your web headers might be different. Find out here: https://www.whatismybrowser.com/detect/what-is-my-user-agent

If you run this code, you should get an error as the URL variable is empty; therefore, you are scraping nothing. Now this function would work if I parsed in a URL like this: https://finance.yahoo.com/quote/MSFT?p=MSFT

But we want to be able to find the stock price of any stock that is in the various stock exchanges all around the world, and we don't want to hardcode each stock's URL because that could be more than half a million variables which is very inefficient least to say time-consuming.

A way to solve this would be to make a search query function that would return the Yahoo Finance URL corresponding to the stock from which we could scrape. First, install the google module by using the command : pip install google and then making the relatively easy function.

We will heavily be relying upon Google's PageRank algorithm for this function at we will only be using the 1st URL out of the list of 10 to scrape the stock price from

Then you can test, by first substituting something like 'Tesla stock price yahoo finance' into the google_query function and substituting the URL with the first link returned by the function. After that when you execute the check_price function you should get an output of the legal company name, stock price and currency of stock.

URL = google_query('Tesla stock price yahoo finance')[0]

If it doesn't work, don't worry. I will embed all the code we went over today near the conclusion of the article. Meanwhile, let's move onto the:

Weather Function 🌀

To create the weather function, we will need the location of the user. Now you can prompt the user to say which city they reside in, but I wanted the program to be seamless, so I find the user's area by using the user's IP address.

Keep in mind this won't work if the user is using a VPN as then it would get the IP address of the VPN Server.

Since there might be cases where this doesn't work, we can also find the weather readings of any city in the world by just using its name like 'Weather in Calabasas.'

First, to get the location using the user's IP Address, we will be using the function get_location, which scrapes the output from iplocation.com. We should already have the libraries needed from the previous web scraping function, but if you don't, then pip install beautifulsoupand pip install requests

Returns the city and country the user resides in as well as the latitude and longitude

Now to get the weather, we will be using the OpenWeatherMap API for which you would need to sign up to get your API key(s). I am using the Free plan where you get up to a million calls a month, which is plenty.

You should generate an API Key for the Virtual Assistant and make sure to keep it safe.

After that, you would make a new python file called config.py and store your API key there like so :

Make a config.py file that will hold all API keys, passwords and secrets to prevent it from getting exposed and exploited by anyone if you plan on uploading your code. (P.S: Make sure to add config.py in the .gitignore file)

Lastly, all we need to do is write a simple method to make an API call to the OpenWeatherMap API and receive the weather information.

Import config to get access to API Key and then make a request for the weather data by accessing the endpoint URL

Test your code by first getting the latitude and longitude and then parsing it in the weather function like so :

          city,country,latitude, longitude = get_location()
print(weather(latitude, longitude))

You should receive something like a dictionary with various values such as coordinates, weather, timezone etc.

Movie Rating Function 🎬

This is by far the most used function out of my virtual assistant and the most beneficial one as well as I always tend to need reviews of a Tv Show or Movie before committing to it. To use this function, we will need an important module that you can get like this : pip install IMDbPY

This package is helpful in retrieving any movie or tv show in the IMDB database.

Then we create a method to make a google query of the movie you want to search to see if it exists or not in the IMDB database. Then we return the right URL from where we can scrape the title and year of the movie for reference afterwards.

Retrieve the title and year of the movie

Then we would also like to scrape the rotten tomato score, which is a useful metric to determine what the critics and people think about a film.

Retrieve the rotten tomato score

Finally, we need to add a few lines of code that can search the IMDB database for all the information it has on the movie as well as fetch the rotten tomato score. For this example, I will be using the film: The Karate Kid (1984)

Test by substituting any movie in the query variable and execute the program. It should take a minute…

Sports Function πŸ€

For this function, we will be using sports.py, a package that provides up to date sports scores for several sports. To install : pip install sports.py

All we need to do now is create a straightforward method to retrieve game scores, as shown down below.

This method will return the game score if you specify a team name, i.e., parse in lakers into the function, and it should produce the score provided they played the most recent game in the NBA.

Final: The Send E-Mail Function ✉

This function might arguably be the most useful function on this list as it would be handy to talk out the email and have your assistant send it. Also, it is the only function on this list to my knowledge that only a few of the assistants support. To send emails, python has a built-in module called smtplib like the server Gmail uses. SMTP or (Simple Mail Transfer Protocol) is a protocol used to communicate with mail servers from external ones. It's a delivery protocol meaning you can't receive an email with it but can only deliver. For receiving, you can implement the IMAP or (Internet Message Access Protocol) yourself in your assistant script as I won't be going through it in this article.

In this script, we are opening a secure connection to the SMTP server.

If you try to run this method you will encounter an error as Gmail imposes restricitons on SMTP connections like this, so you have to authenticate yourself. Notice how the script above has fields such as config.email and config.password which we will be making right now

There are two steps: allowing access to less secure apps (Not recommended) and creating an app password (Recommended).

I prefer the latter. To create an app password, enable 2-Step Verification for your Google Account :

Enable 2-Step Verification to use App passwords

Then, click on App passwords and select what app you need the password for and what device is this password going to be used on. In our case, the app is Mail, and the device depends on if you are using a Mac or Windows laptop, in my case Windows. Then click generate, and you should be given an app password. Please make a note of it.

Please copy this password and paste it into config.py under a variable password.
          #Then Assign the email and password in config.py
email - 'your_email_for_which_you_made_the_password@gmail.com'
password - 'App_Password_that_you_copied'

Now to test, call the method and parse in the subject, body and the person you want to send the email to, try yourself first. You should then receive an email consisting of the same information.

Demo for how the mail function should work

Putting it all togetherπŸ’»

Now that we have all our methods, we need to put them together. First, let's create the main method and add keywords upon which each process will run. Then determine a keyword to break the loop and end the program.

Add a keyword for each function that can be called inside each condition and if the user says, stop or bye, then end the program.

Conditions for all methods are presented above. Feel free to add any logic needed to call functions inside the requirements.

The gist above contains the main method for the program that, once you run, should be indistinguishable from Alexa, Siri and Google Assistant. This is because we have officially created our very own Virtual Assistant that you can keep on adding features too and iterating on overtime.

If you would like to get the source code for all the methods and functions that were outlined in this article, you can get it here

There are many functions like the internet speed test and Spotify integration that I left out that I would've loved to include, but then again, we would we here forever. So if you would like to check out the main program consisting all the source code, check out the link below :

Can this be considered an AI?🧠

As we wrap up, I would like to touch on the fact if this virtual assistant can be classified as an AI or not as a lot of people would argue that it's not an AI as the machine when learning by itself is far off from following explicitly coded if-else statements which we did. But if we look at the fundamental level, the sole purpose of an AI is to perform humane tasks with some or even more significant as humans. Therefore, I would say that our virtual assistant is artificially intelligent and, consequently, an AI but is not a very good example of an AI. There is no learning taking place but rather just doing what it's being told, making it just 'intelligent.'

Next Steps πŸ‘£

I am currently working on implementing Natural Language Processing (NLP) to make my assistant smarter and understand and learn over time. Speech Recognition is not that different from Natural Language Processing. We used in this program is only a technology that enables the computer to identify and interpret words or phrases and is part of a field called computational linguistics.

Whereas, NLP is a branch of pure artificial intelligence that tries to understand what the user means instead of just converting what the user says to text. This will make speech recognition less time consuming and will exponentially reduce the amount of if-else statements we have in our program. It would understand what the user means from a large number of training examples.

After all, you could have a billion if-else statements, but leveraging this technology is much more efficient and opens up the gateway to limitless opportunities. How do you think Google Assistant started?🧐

If you enjoyed this article and it helped you out, please leave a clap, and if you have any questions, you could always leave a comment down below or even better, email me at zakirangwala@gmail.com.

If you would like to learn more about me and the work I do, visit my website at zakirangwala.com

Gain Access to Expert View — Subscribe to DDI Intel

How To Create A Jarvis Like Ai Assistant

Source: https://medium.datadriveninvestor.com/how-to-build-your-ai-powered-virtual-assistant-8ce474ab200e

Posted by: coxduccies.blogspot.com

0 Response to "How To Create A Jarvis Like Ai Assistant"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel