Hello everyone, today we are going to create our own AI personal assistant like the Iron Man’s Jarvis. Before we start, let me give you an insight about what is an AI.
What is an AI (Artificial Intelligence)?
There are number of definitions given to this question. So, simply we can say that artificial intelligence is an engineering and science of making machines and programs that can understand human intelligence and able to replicate human intelligence.
What is an AI personal assistant?
An AI personal assistant is an example of weak AI. A weak AI can only perform tasks that designed by the user. So, it can understand written or verbal commands that assigned by the user.
Now let’s create a personal voice assistant.
Here we using the following python packages to build our voice assistant
Speech recognition – We use ‘SpeechRecognition’ library to capture whatever we speak and convert it to text format. It is an essential feature required in artificial intelligence devices.
Pyttsx3 – We use this library to convert text to speech unlike other libraries, it works offline and compatible with Python 2 and 3. It also supports text to speech engines on popular operating systems like Windows and Linux.
Wikipedia – We don’t need much explanation about this library as its name says all. This python library is used to extracts data from Wikipedia.
ecapture – This python module is used to capture images from your camera.
datetime - We use this inbuilt module to get date and time.
os – We included this module to enable our AI program to provide function to interact with our operating system.
time - This module used to display time.
web browser – We use this inbuilt python library to extract data from the web.
subprocess – To process various system commands like shutdown and restart, we use this module.
Json – To storing and exchange data, we use this module.
request – We use this module to send all type of HTTP request.
wolfram alpha – This is an external API that helps to do geographical and computational questions.
Now it’s time to code
First, we have to import the libraries that required. See the code below
import datetime import subprocess import time import webbrowser import pyttsx3 import requests import speech_recognition as sr import wikipedia import wolframalpha from ecapture import ecapture as ec
Now we have to setting up the speech engine. We use Sapi5, Microsoft’s text to speech engine for voice recognition. You can set the voice id as 0 for male voice or 1 for female voice.
We will create a function which will converts text to speech. Here we will name it as speak.
def speak(text): engine.say(text) engine.runAndWait()
The following function will make our AI to greet the user. We abstract hour from current time and use if condition to greet the user according to the hour.
def wishMe(): hour = datetime.datetime.now().hour if hour >= 0 and hour < 12: speak("Hello,Good Morning") print("Hello,Good Morning") elif hour >= 12 and hour < 18: speak("Hello,Good Afternoon") print("Hello,Good Afternoon") else: speak("Hello,Good Evening") print("Hello,Good Evening")
Now we have to make a function for our AI assistant to understand human language. With the help of recognize_google function we can use google audio to recognize speech.
def takeCommand(): r = sr.Recognizer() with sr.Microphone() as source: print("Listening...") audio = r.listen(source) try: statement = r.recognize_google(audio, language='en-in') print(f"user said:{statement}\n") except Exception as e: speak("Pardon me, please say that again") return "None" return statement speak("Loading your AI personal assistant Alpha") wishMe()
Now it’s time to write our main function. Here we use the variable ‘statement’ to store the commands given by the user.
if __name__ == '__main__': while True: speak("Tell me how can I help you now?") statement = takeCommand().lower() if statement == 0: continue
Now it’s time to write codes to implement skills for our AI. Some of the skills and its code given below and you can analyse the complete code given at the end to learn more.
Skill 1: Fetching data from Wikipedia
if 'wikipedia' in statement: speak('Searching Wikipedia...') statement = statement.replace("wikipedia", "") results = wikipedia.summary(statement, sentences=3) speak("According to Wikipedia") print(results) speak(results)
Skill 2: Accessing web browsers, YouTube and Gmail
elif 'open youtube' in statement: webbrowser.open_new_tab("https://www.youtube.com") speak("youtube is open now") time.sleep(5) elif 'open google' in statement: webbrowser.open_new_tab("https://www.google.com") speak("Google chrome is open now") time.sleep(5) elif 'open gmail' in statement: webbrowser.open_new_tab("gmail.com") speak("Google Mail open now") time.sleep(5)
Skill 3: Predicting time
elif 'time' in statement: strTime = datetime.datetime.now().strftime("%H:%M:%S") speak(f"the time is {strTime}")
Skill 4: Setting up our AI assistant to answer computational and geographical questions using the WolframAlpha. Note that you have to add your own app id in the place of ‘wolfram app ID’
elif 'ask' in statement: speak('I can answer to computational and geographical questions and what question do you want to ask now') question = takeCommand() app_id = "your wolfram app ID" client = wolframalpha.Client(' your wolfram app ID ') res = client.query(question) answer = next(res.results).text speak(answer) print(answer)
To get WolframAlpha app ID, you have to visit their website www.wolframalpha.com. Create your account and from your account section click on ‘My Apps (API)’. In the following window click on ‘Get APP_ID’ button.
Now you have a basic idea of how to create your own AI personal assistant. You can download the complete code from the following link.
https://github.com/menofletter/-AI-Personal-Voice-assistant-using-Python
Leave a Reply