Guide to your very own AI Virtual Assistant in Python.

Voice assistants can make your life easy!

Have you ever wondered how cool it would be to have your own A.I. assistant? Imagine how easier it would be to send emails without typing a single word, searching on wikipedia without actually opening the web browsers, and performing many other daily tasks with the help of a single voice command.

In this tutorial, you are gonna learn how to code and build your own A.I. voice assistant in Python.

  • It can send emails for you.
  • It can play music for you.
  • It can do Wikipedia searches for you.
  • It can open websites like Google, Youtube, etc., in a web browser.
  • It can open your code editor or IDE with a single voice command.

Enough talk! Let’s start building our very own A.I assistant.

It can aid you with many things.

I used the Pycharm to code this up. Feel free to use any other IDE you are comfortable with and start a new project.

P.S: Don’t forget to decide a name for your voice assistant beforehand :P

The first and foremost thing for an A.I. assistant is to speak. To make our bot talk, we will code a speak() function. This function will take audio as an argument, and then, it will pronounce it.

def speak(audio):
pass #For now. We will write the conditions later.

Now, the next thing we need is audio. So, we are going to install a module named pyttsx3.

What is pyttsx3?

  • A python library which 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 the Python 3.

Installation:

pip install pyttsx3

After successfully installing pyttsx3, import this module in your program.

Usage:

import pyttsx3

engine = pyttsx3.init('sapi5')

voices = engine.getProperty('voices') #getting details of the current voice

engine.setProperty('voice', voice[0].id)

What is sapi5?

  • Speech API developed by Microsoft.
  • 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
def speak(audio):

engine.say(audio)


engine.runAndWait() #Without this command, speech will not be audible to us.

Now, we will create a main() function, and inside this main() function, we will call our speak function.

if __name__=="__main__" :

speak("Hello World! Hope you all are doing well.")

Whatever you will write inside this speak() function will be converted into speech. Congratulations! With this, our A.I. has its own voice, and it is ready to speak.

Now, we are going to make a wishme() function, that will make our A.I. wish or greet us according to the time on the computer.

To provide the current time to A.I., we need to import a module called datetime. Import this module to your program, by:

import datetime

Now, let’s start defining our wishme() function:

def wishme():
hour = int(datetime.datetime.now().hour)

Here, we have stored the integer value of the current hour or time into a variable named hour. Now, we will use this hour value inside an if-else loop.

def wishMe():
hour = int(datetime.datetime.now().hour)
if hour>=0 and hour<12:
speak("Good Morning!")

elif hour>=12 and hour<18:
speak("Good Afternoon!")

else:
speak("Good Evening!")

speak("Welcome! Please tell me how may I help you")

The next most important thing for our A.I. assistant is that it should be able to take command with the help of the microphone of our system. So, now we will code a takeCommand() function.

They are helpful almost everywhere.

With the help of the takeCommand() function, our A.I. assistant will be able to return a string output by taking microphone input from us.

Before defining the takeCommand() function, we need to install a module called speechRecognition. Install this module by:

pip install speechRecognition

After successfully installing this module, import this module into the program by writing an import statement.

import speechRecognition as sr

Let’s start coding our takeCommand() function:

def takeCommand():
#It takes microphone input from the user and returns string output

r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)

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.

try:
print("Recognizing...")
query = r.recognize_google(audio, language='en-in')
#Using google for voice recognition.
print(f"User said: {query}\n") #User query will be printed.

except Exception as e:

# print(e) use only if you want to print the error!
print("Say that again please...") #Say that again will be printed in case of improper voice
return "None"
#None string will be returned
return query

To do Wikipedia searches, we need to install and import the wikipedia module into our program.

Installing wikipedia module:

pip install wikipedia

After successfully installing wikipedia module, import the wikipedia module into the program by writing an import statement.

if __name__ == "__main__":
wishMe()
while True:

query = takeCommand().lower() #Converting user query into lower case

# Logic for executing tasks based on query
if 'wikipedia' in query: #if wikipedia found in the query then this block will be executed
speak('Searching Wikipedia...')
query = query.replace("wikipedia", "")
results = wikipedia.summary(query, sentences=2)
speak("According to Wikipedia")
print(results)
speak(results)

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 (which you can change by changing the number of sentences from 2 to any number you want) from the summary of the Wikipedia page will be converted to speech with the help of the speak function.

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 pip statement, we can directly import it into our program by writing an import statement.

Code:

elif 'open youtube' in query:
webbrowser.open("youtube.com")

Here, we are using the elif loop to check whether the Youtube is in the query of the user or not. Let’ suppose, the user gives a command as “Please, open youtube.”

So, open youtube will be in the user’s query, and the elif condition will be true.

elif 'open google' in query:
webbrowser.open("google.com")

We are opening Google in a web-browser by applying the same logic that we used while opening Youtube.

To play music, we need to import a module called os. Import this module directly with an import statement.

elif 'play music' in query:
music_dir = 'D:\\Non Critical\\songs\\Favorite Songs2'
songs = os.listdir(music_dir)
print(songs)
os.startfile(os.path.join(music_dir, songs[0]))

In the above code, we first opened our music directory and then listed all the songs present in the directory with the help of the os module.

With the help of os.starfile, you can play any song of your choice. You can also play a random song with the help of a random module. Every time you command to play music, the A.I. will play any random song from the song directory.

elif 'the time' in query:
strTime = datetime.datetime.now().strftime("%H:%M:%S")
speak(f"Ma'am, the time is {strTime}")

In the above code, we are using datetime() function and storing the current time of the 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 the speech.

elif 'open code' in query:
codePath = "
/Applications/PyCharm CE.app" #that's the code path.
os.startfile(codePath)

To open the Pycharm Code or any other application, we need the code path of the application.

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.

To send an email, we need to import a module called smtplib.

What is smtplib?

  • Simple Mail Transfer Protocol (SMTP) is a protocol that allows us to send emails and to route emails between mail servers.

An instance method called sendmail is present in the SMTP module. This instance method allows us to send an email.

It takes 3 parameters:

  • The sender: Email address of the sender.
  • The receiver: Email of the receiver.
  • The message: A string message which needs to be sent to one or more than one recipient.

Now, we will create a sendEmail() function, which will help us to send emails to one or more than one recipients.

def sendEmail(to, content):
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login('youremail@gmail.com', 'your-password')
server.sendmail('youremail@gmail.com', to, content)
server.close()

Note: Do not forget to ‘enable the less secure apps’ feature in your Gmail account. Otherwise, the sendEmail function will not work properly.

elif 'email to aishwarya' in query:
try:
speak("What should I say?")
content = takeCommand()
to = "aishwarya45@gmail.com"
sendEmail(to, content)
speak("Email has been sent!")
except Exception as e:
print(e)
speak("Sorry my friend. I am not able to send this email")

We are using the try and except block to handle any possible error that can occur while sending emails.

  1. First of all, we have created a wishme() function that gives the functionality of greeting the user according to the system time.
  2. After wishme() function, we have created a takeCommand() function, which helps our A.I. to take command from the user. This function is also responsible for returning the user’s query in a string format.
  3. We developed the code logic for opening different websites like google, youtube, etc.
  4. Developed code logic for opening Pycharm or any other application.
  5. At last, we added functionality to send emails.

A lot of people will argue that the virtual assistant that we have created is not an A.I., but it is the output of a bunch statements. If we look at the very basic level, the sole purpose of A.I. is to develop machines that can perform human tasks with the same effectiveness or even more effectively.

It is a fact that our virtual assistant is not a very good example of A.I., but it is an A.I.!

Hurray! with this, you have successfully made your very first virtual assistant. Explore and try to add other functionalities to it. I hope you all liked this blog xD

Yay! You did it!

A learning enthusiast.