As we know Python is a suitable language for script writers and developers. Let’s write a script for Personal Voice Assistant using Python. The query for the assistant can be manipulated as per the user’s need.
The implemented assistant can open up the application (if it’s installed in the system), search Google, Wikipedia and YouTube about the query, calculate any mathematical question, etc by just giving the voice command. We can process the data as per the need or can add the functionality, depends upon how we code things.
We are using Google speech recognition API and google text to speech for voice input and output respectively.
Also, for calculating mathematical expression WolframAlpha API can be used.
Playsound Package is used to play the saved mp3 sound from the system.
Python external Package Requirements:
-> gTTS – Google Text To Speech, for converting the given text to speech
-> speech_recognition – for recognizing the voice command and converting to text
-> selenium – for web based work from browser
-> wolframalpha – for calculation given by user
-> playsound – for playing the saved audio file.
-> pyaudio – for voice engine in python
Well, let’s get started with code. We will divide each function as a single code for easy understanding.
Here’s the main function, with
get_audio() function is created to get the audio from user using microphone, the phrase limit is set to 5 seconds (you can change it). Assistant speaks function is created to provide the output according to the processed data.
So, we have got an idea here how we are giving voice to the machine and take input from user. The next step and the main step is how you want to process your input. This is just basic code, there is a lot of other algorithms(NLP) can be used to process the text in a proper manner. We have made it static.
Wolframalpha api has been used to calculate the calculations part.
Now we have processed the input, it’s time for action!
There are two functions included that is
search_web is just a web crawler which uses selenium package to process. It can search google, wikipedia and can open YouTube. You just have to say include the name and it will open it in the Firefox browser. For other browsers, you need to install a proper browser package in selenium. Here we are using webdriver for Firefox.
open_application is just a function uses os package to open the application present in the system.
Here are some of the examples and output, which can help you understand how the above processing works.
1. Say "Search google Geeks for Geeks" 2. Say "Play Youtube your favourite song" 3. Say "Wikipedia Dhoni" 4. Say "Open Microsoft Word" 5. Say "Calculate anything you want"
In all the above cases, it will give do what is told. If the assistant can’t understand what is told it will ask you to google search it. For the thing which assistant can’t do is handled by this assistant.
Below are some screenshots for the talk between human and the assistant.
Well, that’s it. The above functionality can be coded in many ways, this is a basic implementation. Make sure you have the latest version of all the above packages for smooth work. To run the above code combine all the functions in same file.
Please find below a short video for the compilation of above code.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course