Create your own Virtual Personal Assistant
You know about Cortana, Siri and Google Assistant, right? Have you ever imagined that you can make your own virtual personal assistant and customize it as you want? Today, we’ll be doing it here. We’ll be building a personal assistant from scratch in python. Oh, Before getting into it, let me tell you that by no means it’s an AI but just a powerful example of what AI can do, and how versatile and amazing python is. Also, you need to have some experience with python in order to get started with it. So, Let’s begin:
First, We need to install some important packages:
- SpeechRecognition : Library for performing speech recognition, with support for several engines and APIs, online and offline.
- Pyttsx3 : Pyttsx is a good text to speech conversion library in python.
- Wikipedia : Wikipedia is a Python library that makes it easy to access and parse data from Wikipedia.
- Wolframalpha : Python Client built against the Wolfram|Alpha v2.0 API.
- PyAudio : Python Bindings for PortAudio.
Make sure you have installed all these packages or else you might run into some errors and this is how you install it:
pip install PackageName
You might run into some errors while installing Pyaudio, I’ve been through the same. You can make use of these steps to avoid the installation errors:
- find your Python version by
python --versionmine is
- find the appropriate
.whlfile from here, for example mine is
PyAudio‑0.2.11‑cp37‑cp37m‑win_amd64.whl, and download it.
- go to the folder where it is downloaded for example
- install the
pipfor example in my case:
pip install PyAudio-0.2.11-cp37-cp37m-win_amd64.whl
Also, install this to avoid unnecessary errors:
pip install pypiwin32
If you are done installing those packages then we can import them and get back to the code:
import speech_recognition as sr
Now, We’’ll be using ‘SAPI5’ as the TTS engine for pyttsx3 and getting the key for wolframaplha, and defining the client.
engine = pyttsx3.init(‘sapi5’)
client = wolframalpha.Client(‘Get your own key’)
You can get your own key from wolframalpha.com -> Apps -> Key.
Now, We’ll be initializing a variable and getting the necessary voice parameter we need. For female voice, you can set the value as -1 in the second line and -2 for male voice. Next, We’ll be creating a function talk with audio as the input parameter.
voices = engine.getProperty(‘voices’)
engine.setProperty(‘voice’, voices[len(voices) — 2].id)
print(‘KryptoKnite: ‘ + audio)
Next, let’s create another function greetMe and this will be used to greet the user when he runs the program. datetime.datetime.now().hour is being used to get the current time in terms of hour and give the output as per the time and the conditions written below. Talk fn will be used to give the output in terms of voice.
CurrentHour = int(datetime.datetime.now().hour)
if CurrentHour >= 0 and CurrentHour < 12:
elif CurrentHour >= 12 and CurrentHour < 18:
elif CurrentHour >= 18 and CurrentHour != 0:
talk('Hey Buddy, It\'s your assistant KryptoKnite!')
talk('tell me about today?')
Next, we’ll be creating another function GivenCommand which is used to recognize the user input, it will define that microphone is used as the source of input and We’ll set the pause threshold as 1. Try except block is used and language to recognized will be set as English-India, and if the voice isn’t recognized or heard We’ll send the text input as a kind of error message.
k = sr.Recognizer()
with sr.Microphone() as source:
k.pause_threshold = 1
audio = k.listen(source)
Input = k.recognize_google(audio, language='en-in')
print('Kunal Dhariwal: ' + Input + '\n')
talk('Sorry! I didn\'t get that! Try typing it here!')
Input = str(input('Command: '))
Now, let’s start the main function:
Here, We’ll be declaring some important functions and conditions which will enhance the functionality of our personal assistant and help him to deliver the output and receive the inputs from the user.
if __name__ == '__main__':
Input = GivenCommand()
Input = Input.lower()
if 'open google' in Input:
elif 'open youtube' in Input:
elif "what\'s up" in Input or 'how are you' in Input:
setReplies = ['Just doing some stuff!', 'I am good!',
'Nice!', 'I am amazing and full of power']
Similarly you can add up more elif with other functionalities like I’ve added one for sending the email.
elif 'email' in Input:
talk('Who is the recipient? ')
recipient = GivenCommand()
if 'me' in recipient:
talk('What should I say? ')
content = GivenCommand()
server = smtplib.SMTP('smtp.gmail.com', 587)
server.sendmail('Your_Username', "Recipient_Username", content)
talk('Sorry ! I am unable to send your message at this moment!')
Or may be play some music?
elif 'play music' in Input:
music_folder = 'Path
music = ['song']
random_music = music_folder + random.choice(music) + '.mp3'
talk('Okay, here is your music! Enjoy!')
Next, We’ll be adding functionalities to use the input to make some searches on wikipedia, google and use wolframalpha.
Input = Input
res = client.Input(Input)
outputs = next(res.outputs).text
outputs = wikipedia.summary(Input, sentences=3)
talk("searching on google for " + Input)
say = Input.replace(' ', '+')
webbrowser.open('https://www.google.co.in/search?q=' + Input)
talk('Next Command! Please!')
When all this is done, it is very important for the program to exit. Let’s write a condition for it here:
elif 'nothing' in Input or 'abort' in Input or 'stop' in Input:
talk('Bye, have a good day.')
elif 'bye' in Input:
talk('Bye, have a great day.')
That’s it! You have created your own virtual personal assistant.
You can now customize it as you like and set any conditions for it, Also you can add N number of functionalities to it and make it even more amazing.
Complete code: https://bit.ly/2VaBsEU
You can get the video demo here on my LinkedIn Post: https://bit.ly/2DW8qU0
If you encounter any error or need any help, you can always make a comment or ping me on LinkedIn.
I hope that this has helped you to enhance your knowledge base :)
Follow me for more!
Thanks for your read and valuable time!