Python Jarvis Virtual Assistant
Software Group Project
In running of my 6th sem. Software Group Project task given by my collage. so JARVIS(Desktop Voice Assistant) based on python develop by me (D19IT155-Chaudhary Darshil) and my project partner (D19IT156-Shah Shivani).
Why Python?
Python is the only prerequisite for basic scripting, Machine Learning and Deep Learning knowledge will help this model to do task like AI-ML.
Why has Python gained so much popularity in all this fields?
Demand for AI
Types of Artificial Intelligence
Our Jarvis Virtual Ass. project is based on Artificial Narrow Intelligence.
Commonly known as weak AI, Artificial Narrow Intelligence involves applying AI only to specific tasks.
The existing AI-based systems that claim to use “artificial intelligence” are actually operating as a weak AI. Alexa is a good example of narrow intelligence. It operates within a limited predefined range of functions.
Google search engine, Sophia, self-driving cars and even the famous AlphaGo, fall under the category of weak AI
JARVIS
Our Virtual Assistant will able to do the following things...
Code Explanation
Defining Speak Function:
The first and foremost thing for an A.I. assistant is that it should be able to speak. To make our J.A.R.V.I.S. talk, we will make a function called speak(). This function will take audio as an argument, and then it will pronounce it.
Now, the next thing we need is audio. We must supply audio so that we can pronounce it using the speak() function we made. We are going to install a module called pyttsx3.
What is pyttsx3?
- A python library that will help us to convert text to speech. In short, it is a text-to-speech library.
- It works offline, and it is compatible with Python 2 as well as Python 3.
Installation:
After successfully installing pyttsx3, import this module into your program.
Usage:
What is sapi5?
- Microsoft developed speech API.
- Helps in synthesis and recognition of voice.
What Is VoiceId?
- Voice id helps us to select different voices.
- voice[0].id = Male voice
- voice[1].id = Female voice
Writing Our speak() Function :
We made a function called speak() at the starting of this project. Now, we will write our speak() function to convert our text to speech.
Creating Our main() function:
We will create a main() function, and inside this main() Function, we will call our speak function.
Code:
Whatever you will write inside this speak() function will be converted into speech. Congratulations! With this, our J.A.R.V.I.S. has its own voice, and it is ready to speak.
Defining Wish me Function :
Now, we will make a wishme() function, that will make our J.A.R.V.I.S. wish or greet the user according to the time of computer or pc. To provide current or live time to A.I., we need to import a module called datetime. Import this module to your program, by:
Now, let's start defining the wishme() function:
Here, we have stored the current hour or time integer value into a variable named hour. Now, we will use this hour value inside an if-else loop.
Defining Take command Function :
The next most important thing for our A.I. assistant is that it should take command with the help of the microphone of the user's system. So, now we will make a takeCommand() function. With the help of the takeCommand() function, our A.I. assistant will return a string output by taking microphone input from the user.
Before defining the takeCommand() function, we need to install a module called speechRecognition. Install this module by:
After successfully installing this module, import this module into the program by writing an import statement.
Let's start coding the takeCommand() function :
We have successfully created our takeCommand() function. Now we are going to add a try and except block to our program to handle errors effectively.
Coding logic of Jarvis
Now, we will develop logic for different commands such as Wikipedia searches, playing music, etc.
– Defining Task 1: To search something on Wikipedia
To do Wikipedia searches, we need to install and import the Wikipedia module into our program. Type the below command to install the Wikipedia module :
After successfully installing the Wikipedia module, import it into the program by writing an import statement.
In the above code, we have used an if statement to check whether Wikipedia is in the search query of the user or not. If Wikipedia is found in the user's search query, then two sentences from the summary of the Wikipedia page will be converted to speech with the speak function's help.
– Defining Task 2: To open YouTube site in a web-browser
To open any website, we need to import a module called webbrowser. It is an in-built module, and we do not need to install it with a pip statement; we can directly import it into our program by writing an import statement.
Code:
Here, we are using the elif loop to check whether Youtube is in the user's query. Let' suppose the user gives a command as "J.A.R.V.I.S., open youtube." So, open youtube will be in the user's query, and the elif condition will be true.
– Defining Task 3: To open Google site in a web-browser
We are opening Google in a web-browser by applying the same logic that we used to open youtube.
– Defining Task 4: To play music
To play music, we need to import a module called os. Import this module directly with an import statement.
In the above code, we first opened our music directory and then listed all the songs present in the directory with the os module's help. With the help of os.startfile, you can play any song of your choice. I am playing the first song in the directory. However, you can also play a random song with the help of a random module. Every time you command to play music, J.A.R.V.I.S. will play any random song from the song directory.
– Defining Task 5: To know the current time
In the above, code with using datetime() function and storing the current or live system into a variable called strTime. After storing the time in strTime, we are passing this variable as an argument in speak function. Now, the time string will be converted into speech.
– Defining Task 6: To open the VS Code Program
To open the VS Code or any other application, we need the code path of the application.
Steps to get the code path of the application:
Step 1: Open the file location.
Step 2: Right-click on the application and click on properties.
Step 3: Copy the target from the target section.
After copying the target of the application, save the target into a variable. Here, I am saving the target into a variable called codePath, and then we are using the os module to open the application
Comments
Post a Comment