how toPython

Virtual Assistant (JARVIS) with Python

Virtual desktop assistant are getting famous after releasing IRON MAN movie,

if you want that your computer perform your tasks by hearing the command like JARVIS did for TONY STARK,

Here we are going to make an assistant, not like JARVIS but similar with it,

It is possible to make it by using the famous language Python.

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 run the below command.
pip install pyttsx3
  • SpeechRecognition: It allows us to convert audio input into text. To install this module run the following command in the terminal.
pip install SpeechRecognition
  • webbrowser: It provides a high-level interface that @llows displaying Web-based documents. To install this module run the below command in the terminal.
pip install webbrowser
  • Wikipedia: It is used to get information from the Wikipedia website. To install this module run the below command in the terminal.
pip install wikipedia

Virtual Assistant with Python

1) speak method,

This method use to make the assistant interactive with user, this helps to program to speak with the help of pyttsx3 module

def speak(audio):
	
	engine = pyttsx3.init()

	voices = engine.getProperty('voices')
	
	# [0]=male voice and
	# [1]=female voice 
	engine.setProperty('voice', voices[0].id)
	
	# Method to speaking for the assistant
	engine.say(audio)
	
	# Blocks when processing the currently commands
	engine.runAndWait()

2) Take queries method
This method will take the user’s query and check for the condition. If the condition is true it will run the condition. We can add any number of conditions for it and if the condition satisfies we will get the desired output.

def Take_query():

	# hello function
	# makes it more interactive
	Hello()
	
	# This loop runs infinite times as it will take
	# our queries continuously until and unless
	# the program is not exit or terminate

	while(True):
		

		query = takeCommand().lower()
		if "open resolve myquery" in query:
			speak("Opening resolvemyquery")
	
			webbrowser.open("www.resolvemyquery.com")
			continue
		
		elif "open google" in query:
			speak("Opening Google ")
			webbrowser.open("www.google.com")
			continue
			
		elif "which day it is" in query:
			tellDay()
			continue
		
		elif "tell me the time" in query:
			tellTime()
			continue
		
		# this will exit and terminate the program
		elif "bye" in query:
			speak("Bye. Check Out GFG for more exicting things")
			exit()
		
		elif "from wikipedia" in query:
			
			# if any one wants to have a information
			# from wikipedia
			speak("Checking the wikipedia ")
			query = query.replace("wikipedia", "")

			result = wikipedia.summary(query, sentences=4)
			speak("According to wikipedia")
			speak(result)
		
		elif "tell me your name" in query:
			speak("I am Jarvis. Your deskstop Assistant")

3) take command method: Virtual Assistant (JARVIS) with Python
This function takes the user command and convert it with speech recognition module

def takeCommand():

	r = sr.Recognizer()

	with sr.Microphone() as source:
		print('Listening')
		

		r.pause_threshold = 0.7
		audio = r.listen(source)
		
		try:
			print("Recognizing")
			

			Query = r.recognize_google(audio, language='en-in')
			print("the command is printed=", Query)
			
		except Exception as e:
			print(e)
			print("Say that again sir")
			return "None"
		
		return Query

4) Time function,

this function tell time to the user

# code
def tellTime(self):
# This method will give the time
	time = str(datetime.datetime.now())

	print(time)
	hour = time[11:13]
	min = time[14:16]
	self.Speak(self, "The time is sir" + hour + "Hours and" + min + "Minutes")	
    


5) Hello method
This is just used to greet the user with a hello message

def Hello():

	speak("""hello sir I am your desktop assistant. /
		Tell me how may I help you""")


6) Main method
From here your program execution started

if __name__ == '__main__':
	
	Take_query()

Complete Code:

import pyttsx3
import speech_recognition as sr
import webbrowser
import datetime
import wikipedia



def takeCommand():

	r = sr.Recognizer()

	with sr.Microphone() as source:
		print('Listening')
		

		r.pause_threshold = 0.7
		audio = r.listen(source)
		
	
		try:
			print("Recognizing")
			

			Query = r.recognize_google(audio, language='en-in')
			print("the command is printed=", Query)
			
		except Exception as e:
			print(e)
			print("Say that again sir")
			return "None"
		
		return Query

def speak(audio):
	
	engine = pyttsx3.init()

	voices = engine.getProperty('voices')
	

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

	engine.say(audio)
	

	engine.runAndWait()

def tellDay():
	

	day = datetime.datetime.today().weekday() + 1
	

	Day_dict = {1: 'Monday', 2: 'Tuesday',
				3: 'Wednesday', 4: 'Thursday',
				5: 'Friday', 6: 'Saturday',
				7: 'Sunday'}
	
	if day in Day_dict.keys():
		day_of_the_week = Day_dict[day]
		print(day_of_the_week)
		speak("The day is " + day_of_the_week)


def tellTime():
	
	# This method will give the current time in digital format
	time = str(datetime.datetime.now())
	

	print(time)
	hour = time[11:13]
	min = time[14:16]
	speak(self, "The time is sir" + hour + "Hours and" + min + "Minutes")	

def Hello():
	

	speak("hello sir I am your desktop assistant. /
		Tell me how may I help you")


def Take_query():

	# calling greet functions
	Hello()
	

	while(True):
		
	
		query = takeCommand().lower()
		if "open resolvemyquery" in query:
			speak("Opening resolvemyquery ")
			

			webbrowser.open("www.resolvemyquery.com")
			continue
		
		elif "open google" in query:
			speak("Opening Google ")
			webbrowser.open("www.google.com")
			continue
			
		elif "which day it is" in query:
			tellDay()
			continue
		
		elif "tell me the time" in query:
			tellTime()
			continue
		
		# this command will exit and terminate the program, you can change it to.
		elif "bye" in query:
			speak("Bye. Check Out GFG for more exicting things")
			exit()
		
		elif "from wikipedia" in query:
			
			# if user wants to have a information
			# from wikipedia
			speak("Checking the wikipedia ")
			query = query.replace("wikipedia", "")
			
			# it will gives the summary of 4 lines from
			# wikipedia you can increase and decrease
			# it also.
          
			result = wikipedia.summary(query, sentences=4)
			speak("According to wikipedia")
			speak(result)
		
		elif "tell me your name" in query:
			speak("I am Jarvis. Your deskstop Assistant")

if __name__ == '__main__':
	
	# main method for executing
	# the program
	Take_query()

Leave a Comment