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?
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.
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.
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.
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 env
in 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.
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.
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.
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
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.
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.
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.
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 beautifulsoup4
and pip install requests
to extract any information from a webpage, which is a potent tool.
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.
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 beautifulsoup
and pip install requests
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.
After that, you would make a new python file called config.py and store your API key there like so :
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.
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.
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.
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)
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.
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.
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 :
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.
#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.
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.
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