AI virtual assistant using Python

Contents

2. Explanation of the code

Therefore we create our own virtual assistant.

Notes

  • All the codes are available on my GitHub.
  • YouTube demo video and YouTube video code are also available on my channel.
  • The necessary links and packages are mentioned below.
  • It would be appreciated to share.

Let's code

2.1. Required packages and libraries

pip install JarvisAI

This is the latest virtual assistant module, created by me. Provides the basic functionality of any virtual assistant. The prerequisite is just 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 with the method name. You can check the code, as an example.

  1. microphone_in
  2. text2speech
  3. delete
  4. website_opener
  5. send mail
  6. tell_me_date
  7. tell_me_time
  8. launch_any_app
  9. climate
  10. News
  11. tell me

Read more about this here and you can also contribute to this repository here.

2.2. Code-

Imports

import JarvisAI
import re
import pprint
import random

Object creation by JarvisAI according to documentation

obj = JarvisAI.JarvisAssistant()

We have created this function 't2s (text)’. This will convert any text to speech. The whole program we will use (we will call) to this function to produce speech from text.

def t2s(text):
    obj.text2speech(text)

We want to continuously listen to the contributions of the user, so this' mic_input ()’ will try to get audio from the computer's microphone continuously. It will process the audio and return the text in the variable 'res'. We can use this variable 'res’ to perform some action according to user input.

while True:
    res = obj.mic_input()

Weather forecast: We are using a regex to match queries on user input. If ‘weather is found’ the ‘temperature’ in the user input 'res', so we want to make a weather forecast. No need to write things from scratch, just call ‘obj.weather (city = city)’.

You just need to get the city from the user input and pass it to the weather function. It will tell you the weather forecast for your city.

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

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: In a similar way to the previous, match the word ‘news’ from the user input ‘res’. It coincides, call 'obj.news'.

Will return 15 news as a list of chains. Then, we can search for news like ‘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])

It counts almost everything: You will get the first 500 Wikipedia characters and will return them as a string. You can use 'obj.tell_me (topic)’.

Must pass ‘topic’ a ‘tell_me (theme = theme)’. The topic is the keyword you want to know.

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 web portal: This ‘obj.website_opener (domain)’ will open any web portal for you. You just need to get the domain from the user input and then go to 'obj.website_opener (domain)’. It will open the web portal 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 application, play, etc.

This is a bit tricky, en ‘obj.launch_any_app (path_of_app = path)’ the function you need to pass the path of your ‘.exe’ file.

Therefore we have created the dictionary ‘dict_app’ which has an 'application name’ as a key and a 'path’ with the value. We can use this' dict_app’ to search. If the user input application exists in the dictionary, we will open it obtaining the route.

The following example is only for Chrome y 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 type of chat functionality using Tensorflow. You can contribute to make 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(at the)
        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")

I asked for- 'What can you do?’: Here we are simply using ‘obj.t2s ()’ to produce some voice. If you know Python, you will easily understand the following code:

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' ",
        }
        years = """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(years)
        pprint.pprint(li_commands)
        t2s(years)

3. Fill in the code

import JarvisAI
import re
import pprint
random import

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(at the)
        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' ",
        }
        years = """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(years)
        pprint.pprint(li_commands)
        t2s(years)

4. Github repository

You can use my code with total freedom. Stand out if you like my work, subscribe YouTube if you love.

Just clone the repository https://github.com/Dipeshpal/Jarvis-Assisant.git

Then run pip install -r requirements.txt

It will install everything automatically.

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

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.