How to create your AI Virtual Assistant using Python

Introduction A virtual assistant, also called an AI assistant or digital assistant, is an application program that understands natural language voice commands and completes tasks for the user.

Virtual Assistant using Python We all know what is Virtual Assistant.

If you don’t, don’t worry, open your mobile and say “Ok Google” or “Hey Siri”.

Well, Google Assistant, Siri, Alexa all these are example of Virtual Assistant.

Demo and Code YouTube Video- Demo and Code Content- What we are building- Code Explanation Complete code GitHub Repository How you can contribute References   1.

What we are building- Our virtual assistant will able to do the followings things- Weather forecasting, Launch Games, Launch Windows Applications, Open Websites, tells you about almost everything you ask, tells you date and time, greetings, news, etc.

You can interact with your laptop’s microphone/console.

The response generated by the assistant will display on the console or as a speech via the speaker.

Future Updates: Selfie Click, Chat more like humans, etc.

You tell me what do you want.

Or contribute in https://pypi.

org/project/JarvisAI 2.

Code Explanation- So let’s create our own virtual assistant.

Notes- All the codes is available on my GitHub.

Demo YouTube video and code YouTube video is also available on my channel.

Required links and packages are mentioned below.

Sharing would be appreciated.

Lets code- 2.

1.

Required packages and libraries- pip install JarvisAI This is the latest virtual assistant module, created by me.

It provides the basic functionality of any virtual assistant.

The prerequisite is only Python (> 3.

6).

Usage and Features- After installing the library you can import the module- import JarvisAI obj = JarvisAI.

JarvisAssistant() response = obj.

mic_input() print(response) The functionality is cleared by the methods name.

You can check the code for example.

mic_input text2speech shutdown website_opener send_mail tell_me_date tell_me_time launch_any_app weather news tell_me Read more about it here and you can also contribute to this repo here.

2.

2.

Code- Imports- import JarvisAI import re import pprint import random Object creation of JarvisAI as per as documentation– obj = JarvisAI.

JarvisAssistant() We have created this ‘t2s(text)’ function.

This will convert any text to speech.

The entire program we will use (call) this function to produce speech from text.

def t2s(text): obj.

text2speech(text) We want to continuously listen to input from the user, so this ‘mic_input()’ will try to fetch audio from the computer’s microphone continuously.

It will process the audio and return text in ‘res’ variable.

We can use this ‘res’ variable to perform some action according to user input.

while True: res = obj.

mic_input() Weather Forecast: We are using a regular expression to match queries in user input.

If ‘weather’ or ‘temperature’ is found in user input ‘res’ then we want to do weather forecasting.

No need to write things from scratch, just call ‘obj.

weather(city=city)’.

You just need to fetch the city from user input and pass it to the weather function.

It will tell you the weather forecasting for your city.

We can pass this returned ‘weather_res’ to ‘ t2s(weather_res)’ to produce speech from ‘weather_res’ string.

while True: res = obj.

mic_input() if re.

search(weather|temperature, res): city = res.

split( )[-1] weather_res = obj.

weather(city=city) print(weather_res) t2s(weather_res) News: Similarly as above, match the ‘news’ word from user input ‘res’.

If matched then call ‘obj.

news’.

It will return 15 news as a list of strings.

So, we can fetch news as ‘news_res[0]’ and pass it to ‘t2s(news_res[0])’.

while True: res = obj.

mic_input() if re.

search(news, res): news_res = obj.

news() pprint.

pprint(news_res) t2s(f”I have found {len(news_res)} news.

You can read it.

Let me tell you first 2 of them”) t2s(news_res[0]) t2s(news_res[1]) Tells about almost everything: It will fetch the first 500 characters from Wikipedia and return them as a string.

You can use ‘obj.

tell_me(topic)’.

You need to pass ‘topic’ to ‘tell_me(topic=topic)’.

The topic is the keyword you want to know about.

while True: res = obj.

mic_input() if re.

search(tell me about, res): topic = res.

split( )[-1] wiki_res = obj.

tell_me(topic) print(wiki_res) t2s(wiki_res) Date and Time: It will tell you the current date and time of your system.

while True: res = obj.

mic_input() if re.

search(date, res): date = obj.

tell_me_date() print(date) print(t2s(date)) if re.

search(time, res): time = obj.

tell_me_time() print(time) t2s(time) Open Any Website: This ‘obj.

website_opener(domain)’ will open any website for you.

You just need to fetch the domain from user input then pass to ‘obj.

website_opener(domain)’.

It will open the website in your default browser.

while True: res = obj.

mic_input() if re.

search(open, res): domain = res.

split( )[-1] open_result = obj.

website_opener(domain) print(open_result) Launch any Applications, Games, etc: This is little tricky, in ‘obj.

launch_any_app(path_of_app=path)’ the function you need to pass the path of your ‘.

exe’ file.

So we have created ‘dict_app’ dictionary which is having an ‘app name’ as key and ‘path’ as value.

We can use this ‘dict_app’ for lookup.

If the user input app exists in the dictionary then we will open it by fetching the path.

The below example is only for Chrome and Epic Games.

while True: res = obj.

mic_input() if re.

search(launch, res): dict_app = { chrome: C:Program Files (x86)GoogleChromeApplicationchrome.

exe, epic games: C:Program Files (x86)Epic GamesLauncherPortalBinariesWin32EpicGamesLauncher.

exe } app = res.

split( , 1)[1] path = dict_app.

get(app) if path is None: t2s(Application path not found) print(Application path not found) else: t2s(Launching: + app) obj.

launch_any_app(path_of_app=path) Greetings and Chat, you can create greetings and chat like this for now.

I am working on https://pypi.

org/project/JarvisAI/ to add chat kind of functionality using Tensorflow.

You can contribute to making it better.

while True: res = obj.

mic_input() if re.

search(hello, res): print(Hi) t2s(Hi) if re.

search(how are you, res): li = [good, fine, great] response = random.

choice(li) print(f”I am {response}”) t2s(f”I am {response}”) if re.

search(your name|who are you, res): print(“My name is Jarvis, I am your personal assistant”) t2s(“My name is Jarvis, I am your personal assistant”) Ask- ‘What can you do?’: Here simply we are using ‘obj.

t2s()’to produce some speech.

If you know python, you will easily understand the code below- while True: res = obj.

mic_input() if re.

search(what can you do, res): li_commands = { “open websites”: “Example: open youtube.

com”, “time”: “Example: what time it is?”, “date”: “Example: what date it is?”, “launch applications”: “Example: launch chrome”, “tell me”: “Example: tell me about India”, “weather”: “Example: what weather/temperature in Mumbai?”, “news”: “Example: news for today “, } ans = “””I can do lots of things, for example you can ask me time, date, weather in your city, I can open websites for you, launch application and more.

See the list of commands-“”” print(ans) pprint.

pprint(li_commands) t2s(ans)   3.

Complete code- import JarvisAI import re import pprint import random obj = JarvisAI.

JarvisAssistant() def t2s(text): obj.

text2speech(text) while True: res = obj.

mic_input() if re.

search(weather|temperature, res): city = res.

split( )[-1] weather_res = obj.

weather(city=city) print(weather_res) t2s(weather_res) if re.

search(news, res): news_res = obj.

news() pprint.

pprint(news_res) t2s(f”I have found {len(news_res)} news.

You can read it.

Let me tell you first 2 of them”) t2s(news_res[0]) t2s(news_res[1]) if re.

search(tell me about, res): topic = res.

split( )[-1] wiki_res = obj.

tell_me(topic) print(wiki_res) t2s(wiki_res) if re.

search(date, res): date = obj.

tell_me_date() print(date) print(t2s(date)) if re.

search(time, res): time = obj.

tell_me_time() print(time) t2s(time) if re.

search(open, res): domain = res.

split( )[-1] open_result = obj.

website_opener(domain) print(open_result) if re.

search(launch, res): dict_app = { chrome: C:Program Files (x86)GoogleChromeApplicationchrome.

exe, epic games: C:Program Files (x86)Epic GamesLauncherPortalBinariesWin32EpicGamesLauncher.

exe } app = res.

split( , 1)[1] path = dict_app.

get(app) if path is None: t2s(Application path not found) print(Application path not found) else: t2s(Launching: + app) obj.

launch_any_app(path_of_app=path) if re.

search(hello, res): print(Hi) t2s(Hi) if re.

search(how are you, res): li = [good, fine, great] response = random.

choice(li) print(f”I am {response}”) t2s(f”I am {response}”) if re.

search(your name|who are you, res): print(“My name is Jarvis, I am your personal assistant”) t2s(“My name is Jarvis, I am your personal assistant”) if re.

search(what can you do, res): li_commands = { “open websites”: “Example: open youtube.

com”, “time”: “Example: what time it is?”, “date”: “Example: what date it is?”, “launch applications”: “Example: launch chrome”, “tell me”: “Example: tell me about India”, “weather”: “Example: what weather/temperature in Mumbai?”, “news”: “Example: news for today “, } ans = “””I can do lots of things, for example you can ask me time, date, weather in your city, I can open websites for you, launch application and more.

See the list of commands-“”” print(ans) pprint.

pprint(li_commands) t2s(ans) 4.

Github Repository You can feel free to use my code.

Star it if you like my work, subscribe on YouTube if you love.

Just clone the repository- https://github.

com/Dipeshpal/Jarvis-Assisant.

git Then run pip install -r requirements.

txt It will automatically install everything.

5.

How you can contribute- Just open this GitHub repository, read it you will understand how you can contribute.

Your contribution will reflect on this project.

6.

References- GitHub repository and Code: https://github.

com/Dipeshpal/Jarvis-Assisant GitHub Pypi repository to contribute: https://github.

com/Dipeshpal/Jarvis_AI JarvisAI Library: https://pypi.

org/project/JarvisAI/ YouTube Channel: https://www.

youtube.

com/DIPESHPAL17 Demo and Code (YouTube): https://youtu.

be/LliTjuxDw_o About the Author Dipesh Pal I am currently working as a Data Scientist at Calsoft Pvt.

Ltd.

and before that, I was a Data Scientist Intern for the Calsoft itself.

I pursued my Bachelors’s Degree in Engineering with a specialisation in Computer Science.

I am also a YouTuber who upload videos related to Technology on my channel www.

youtube.

com/dipeshpal17 , a blogger at www.

dipeshpal.

com You can also read this article on our Mobile APP Related Articles (adsbygoogle = window.

adsbygoogle || []).

push({});.

Leave a Reply