Learn everything about Analytics

Home » How to create your AI Virtual Assistant using Python

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.

AI Virtual Assistant

Virtual Assistant using Python

Demo and Code YouTube Video-

Demo and Code

Content-

  1. What we are building-
  2. Code Explanation
  3. Complete code
  4. GitHub Repository
  5. How you can contribute
  6. 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.

  1. mic_input
  2. text2speech
  3. shutdown
  4. website_opener
  5. send_mail
  6. tell_me_date
  7. tell_me_time
  8. launch_any_app
  9. weather
  10. news
  11. 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)\Google\Chrome\Application\chrome.exe',
            'epic games': 'C:\Program Files (x86)\Epic Games\Launcher\Portal\Binaries\Win32\EpicGamesLauncher.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)\Google\Chrome\Application\chrome.exe',
            'epic games': 'C:\Program Files (x86)\Epic Games\Launcher\Portal\Binaries\Win32\EpicGamesLauncher.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-

About the Author

https://pypi.org/project/JarvisAI/
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 Get it on Google Play