Virtual desktop assistant is an awesome thing. If you want your machine to run on your command like Jarvis did for Tony. Yes it is possible. It is possible using Python. Python offers a good major library so that we can use it for making a virtual assistant. Windows has Sapi5 and Linux has Espeak which can help us in having the voice from our machine. It is a weak A.I.
Modules needed
- pyttsx3:pyttsx is a cross-platform text to speech library which is platform independent. The major advantage of using this library for text-to-speech conversion is that it works offline. To install this module type the below command in the terminal.
pip install pyttsx3
- SpeechRecognition:It allow us to convert audio into text for further processing. To install this module type the below command in the terminal.
pip install SpeechRecognition
- webbrowser:Itprovides a high-level interface which allows displaying Web-based documents to users. To install this module type the below command in the terminal.
pip install webbrowser
- Wikipedia:It is used to fetch a variety of information from the Wikipedia website. To install this module type the below command in the terminal.
pip install wikipedia
Methods used for Virtual Assistant
1) Speak Method
Speak Method will help us in taking the voice from the machine. Here is the code explanation of Speak Method
Python3defspeak(audio):engine=pyttsx3.init()# getter method(gets the current value# of engine property)voices=engine.getProperty('voices')# setter method .[0]=male voice and# [1]=female voice in set Property.engine.setProperty('voice',voices[0].id)# Method for the speaking of the assistantengine.say(audio)# Blocks while processing all the currently# queued commandsengine.runAndWait()
2) Take query method
This method will check for the condition. If the condition is true it will return output. We can add any number if conditions for it and if the condition satisfy we will get the desired output.
Python3defTake_query():# calling the Hello function for# making it more interactiveHello()# This loop is infinite as it will take# our queries continuously until and unless# we do not say bye to exit or terminate# the programwhile(True):# taking the query and making it into# lower case so that most of the times# query matches and we get the perfect# outputquery=takeCommand().lower()if"open geeksforgeeks"inquery:speak("Opening GeeksforGeeks ")# in the open method we just to give the link# of the website and it automatically open# it in your default browserwebbrowser.open("www.geeksforgeeks.com")continueelif"open google"inquery:speak("Opening Google ")webbrowser.open("www.google.com")continueelif"which day it is"inquery:tellDay()continueelif"tell me the time"inquery:tellTime()continue# this will exit and terminate the programelif"bye"inquery:speak("Bye. Check Out GFG for more exciting things")exit()elif"from wikipedia"inquery:# if any one wants to have a information# from wikipediaspeak("Checking the wikipedia ")query=query.replace("wikipedia","")# it will give the summary of 4 lines from# wikipedia we can increase and decrease# it also.result=wikipedia.summary(query,sentences=4)speak("According to wikipedia")speak(result)elif"tell me your name"inquery:speak("I am Jarvis. Your desktop Assistant")
3) takeCommand method
This method is for taking the commands and recognizing the command from the speech_Recognition module
Python3# this method is for taking the commands# and recognizing the command from the# speech_Recognition module we will use# the recongizer method for recognizingdeftakeCommand():r=sr.Recognizer()# from the speech_Recognition module# we will use the Microphone module# for listening the commandwithsr.Microphone()assource:print('Listening')# seconds of non-speaking audio before# a phrase is considered completer.pause_threshold=0.7audio=r.listen(source)# Now we will be using the try and catch# method so that if sound is recognized# it is good else we will have exception# handlingtry:print("Recognizing")# for Listening the command in indian# english we can also use 'hi-In'# for hindi recognizingQuery=r.recognize_google(audio,language='en-in')print("the command is printed=",Query)exceptExceptionase:print(e)print("Say that again sir")return"None"returnQuery
*)tellTime method
Python3# codedeftellTime(self):# This method will give the timetime=str(datetime.datetime.now())# the time will be displayed like this "2020-06-05 17:50:14.582630"# nd then after slicing we can get timeprint(time)hour=time[11:13]min=time[14:16]self.Speak(self,"The time is sir"+hour+"Hours and"+min+"Minutes")""" This method will take time and slice it "2020-06-05 17:50:14.582630" from 11 to 12 for hour and 14-15 for min and then speak function will be called and then it will speak the current time """
4) Hello method
This is just used to greet the user with a hello message.
Python3defHello():# This function is for when the assistant# is called it will say hello and then# take queryspeak("hello sir I am your desktop assistant. /TellmehowmayIhelpyou")
5) Main method
Main method is the method where all the files get executed so we will call the Take_query method here so that it can recognize and tell or give us the desired output.
Python3if__name__=='__main__':# main method for executing# the functionsTake_query()
Complete Code:
Python3importpyttsx3importspeech_recognitionassrimportwebbrowserimportdatetimeimportwikipedia# this method is for taking the commands# and recognizing the command from the# speech_Recognition module we will use# the recongizer method for recognizingdeftakeCommand():r=sr.Recognizer()# from the speech_Recognition module# we will use the Microphone module# for listening the commandwithsr.Microphone()assource:print('Listening')# seconds of non-speaking audio before# a phrase is considered completer.pause_threshold=0.7audio=r.listen(source)# Now we will be using the try and catch# method so that if sound is recognized# it is good else we will have exception# handlingtry:print("Recognizing")# for Listening the command in indian# english we can also use 'hi-In'# for hindi recognizingQuery=r.recognize_google(audio,language='en-in')print("the command is printed=",Query)exceptExceptionase:print(e)print("Say that again sir")return"None"returnQuerydefspeak(audio):engine=pyttsx3.init()# getter method(gets the current value# of engine property)voices=engine.getProperty('voices')# setter method .[0]=male voice and# [1]=female voice in set Property.engine.setProperty('voice',voices[0].id)# Method for the speaking of the assistantengine.say(audio)# Blocks while processing all the currently# queued commandsengine.runAndWait()deftellDay():# This function is for telling the# day of the weekday=datetime.datetime.today().weekday()+1#this line tells us about the number# that will help us in telling the dayDay_dict={1:'Monday',2:'Tuesday',3:'Wednesday',4:'Thursday',5:'Friday',6:'Saturday',7:'Sunday'}ifdayinDay_dict.keys():day_of_the_week=Day_dict[day]print(day_of_the_week)speak("The day is "+day_of_the_week)deftellTime():# This method will give the timetime=str(datetime.datetime.now())# the time will be displayed like# this "2020-06-05 17:50:14.582630"#nd then after slicing we can get timeprint(time)hour=time[11:13]min=time[14:16]speak(self,"The time is sir"+hour+"Hours and"+min+"Minutes")defHello():# This function is for when the assistant# is called it will say hello and then# take queryspeak("hello sir I am your desktop assistant. /TellmehowmayIhelpyou")defTake_query():# calling the Hello function for# making it more interactiveHello()# This loop is infinite as it will take# our queries continuously until and unless# we do not say bye to exit or terminate# the programwhile(True):# taking the query and making it into# lower case so that most of the times# query matches and we get the perfect# outputquery=takeCommand().lower()if"open geeksforgeeks"inquery:speak("Opening GeeksforGeeks ")# in the open method we just to give the link# of the website and it automatically open# it in your default browserwebbrowser.open("www.geeksforgeeks.com")continueelif"open google"inquery:speak("Opening Google ")webbrowser.open("www.google.com")continueelif"which day it is"inquery:tellDay()continueelif"tell me the time"inquery:tellTime()continue# this will exit and terminate the programelif"bye"inquery:speak("Bye. Check Out GFG for more exciting things")exit()elif"from wikipedia"inquery:# if any one wants to have a information# from wikipediaspeak("Checking the wikipedia ")query=query.replace("wikipedia","")# it will give the summary of 4 lines from# wikipedia we can increase and decrease# it also.result=wikipedia.summary(query,sentences=4)speak("According to wikipedia")speak(result)elif"tell me your name"inquery:speak("I am Jarvis. Your desktop Assistant")if__name__=='__main__':# main method for executing# the functionsTake_query()
Output:

Create AI Desktop Assistant using Python and Tkinter