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. -> playaudio – 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() and assistant_speaks function. 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.
# importing speech recognition package from google api
importspeech_recognition as sr
importplaysound # to play saved mp3 file
fromgtts importgTTS # google text to speech
importos # to save/open files
importwolframalpha # to calculate strings into formula
fromselenium importwebdriver # to control browser operations
# num to rename every audio file
# with different name to remove ambiguity
print("PerSon : ", output)
toSpeak =gTTS(text =output, lang ='en', slow =False)
# saving the audio file given by google text to speech
# playsound package is used to play the same file.
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. Also, Wolframalpha api has been used to calculate the calculations part.
# a basic web crawler using selenium
elif"who are you" ininputor"define yourself" ininput:
speak ='''Hello, I am Person. Your personal Assistant.
I am here to make your life easier. You can command me to perform
various tasks such as calculating sums or opening applications etcetra'''
elif"who made you" ininputor"created you" ininput:
speak ="I have been created by Sheetansh Kumar."
elif"geeksforgeeks" ininput:# just
speak ="""Geeks forGeeks isthe Best Online Coding Platform forlearning."""
# write your wolframalpha app_id here
query =input.split()[indx +1:]
res =client.query(' '.join(query))
assistant_speaks("The answer is" +answer)
# another function to open
# different application available
assistant_speaks("I can search the web foryou, Do you want to continue?")
assistant_speaks("I don't understand, I can search the web foryou, Do you want to continue?")
Now we have processed the input, it’s time for action! There are two functions included that is search_web and open_application. 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 Login to comment...