• Offers
    • RegisterLogin
      • Learn More
    PythonPoint.netPythonPoint.net
    • Offers
    • RegisterLogin
      • Learn More

      Python

      SKILL IS IMPORTANT THAN DEGREE Be skill full.
      • Home
      • Blog
      • Python
      • How to make Jarvis in Python

      How to make Jarvis in Python

      • Posted by Python Point Team
      • Categories Python
      • Date December 31, 2022
      • Comments 0 comment
      how to make jarvis in python

      Jarvis is an A.I. based character in the Iron Man films. It is a personal assistant.

      In this article, we will see how to make a personal A.I. assistant using Python.

      Our assistant will be able to do the following stuff:

      • It can play music for you.
      • It can do Wikipedia searches for you.
      • It is capable of opening websites like Google, Youtube, etc., in a web browser.

      Defining Speak Function

      The first thing for an A.I. assistant is that it should be able to speak. To make our assistant talk, we will make a function called speak(). This function will take audio as an argument, and then, it will pronounce it.

      Now, the next thing we need is audio. We must supply audio so that we can pronounce it using the speak() function we made. We are going to install a module called pyttsx3. It is a python library that will help us to convert text to speech. It works offline, and it is compatible with Python 2 as well as the Python 3.

      Installation:

      pip install pyttsx3

      Then, install pypiwin32 by typing the below command in the terminal :

      pip install pypiwin32.

      After successfully installing pyttsx3, import this module in your program.

      import pyttsx3
      
      engine = pyttsx3.init('sapi5')
      
      voices= engine.getProperty('voices') #getting details of current voice
      
      engine.setProperty('voice', voice[0].id)

      sapi5 is Speech API developed by Microsoft. it helps in synthesis and recognition of voice.

      VoiceId helps us to select different voices. voice[0].id is Male voice and voice[1].id is Female voice

      Writing Our speak() Function :

      We made a function called speak() at the starting of this article. Now, we will write our speak() function so that it can convert our text to speech.

      def speak(audio):
      
      engine.say(audio) 
      
      engine.runAndWait() #Without this command, speech will not be audible to us.

      Creating Our main() function: 

      Now, we will create a main() function, and inside this main() Function, we will call our speak function.

      if __name__=="__main__" :
      
      speak("Hello")
      
       

      Whatever you will write inside this speak() function will be converted into speech. Now our assistant is able to speak.

      Defining Wish me Function :

      Now, we are going to make a wishme() function, that will make our JARVIS wish or greet the user according to the time of computer or pc. To provide current or live time to A.I., we need to import a module called datetime. Import this module to your program by:

      import datetime

      Now, let’s start defining the wishme() function:

      def wishme():
      
      hour = int(datetime.datetime.now().hour)
       

      Here, we have stored the integer value of the current hour or time into a variable named hour. Now, we will use this hour value inside an if-else loop.

      Defining Take command Function :

      The next most important thing for our A.I. assistant is that it should be able to take command with the help of the microphone of the user’s system. So, now we will make a takeCommand() function.  With the help of the takeCommand() function, our A.I. assistant will be able to return a string output by taking microphone input from the user.

      Before defining the takeCommand() function, we need to install a module called SpeechRecognition. Install this module by: 

      pip install speechRecognition

      After successfully installing this module, import this module into the program by writing an import statement.

      import speechRecognition as sr

      Let’s write the takeCommand() function :

      def takeCommand():
          #It takes microphone input from the user and returns string output
      
          r = sr.Recognizer()
          with sr.Microphone() as source:
              print("Listening...")
              r.pause_threshold = 1
              audio = r.listen(source)

      We have successfully created our takeCommand() function. Now we are going to add a try and except block to our program to handle errors effectively.

        try:
              print("Recognizing...")    
              query = r.recognize_google(audio, language='en-in') #Using google for voice recognition.
              print(f"User said: {query}\n")  #User query will be printed.
      
          except Exception as e:
              # print(e)    
              print("Say that again please...")   #Say that again will be printed in case of improper voice 
              return "None" #None string will be returned
          return query

       Coding logic of Jarvis

       Now, we will develop logics for different commands such as Wikipedia searches, playing music, etc.

       Defining Task 1: To search something on Wikipedia 

       To do Wikipedia searches, we need to install and import the Wikipedia module into our program. Type the below command to install the Wikipedia module :

      pip install wikipedia

       After successfully installing the Wikipedia module, import it into the program by writing an import statement.

      if __name__ == "__main__":
          wishMe()
          while True:
          # if 1:
              query = takeCommand().lower() #Converting user query into lower case
      
              # Logic for executing tasks based on query
              if 'wikipedia' in query:  #if wikipedia found in the query then this block will be executed
                  speak('Searching Wikipedia...')
                  query = query.replace("wikipedia", "")
                  results = wikipedia.summary(query, sentences=2) 
                  speak("According to Wikipedia")
                  print(results)
                  speak(results)

      In the above code, we have used an if statement to check whether Wikipedia is in the search query of the user or not. If Wikipedia is found in the user’s search query, then two sentences from the summary of the Wikipedia page will be converted to speech with the help of speak function.

      Defining Task 2: To open YouTube site in a web-browser

      To open any website, we need to import a module called webbrowser. It is an in-built module, and we do not need to install it with pip statement, we can directly import it into our program by writing an import statement.

           elif 'open youtube' in query:
                  webbrowser.open("youtube.com")

      Here, we are using the elif loop to check whether the Youtube is in the query of the user or not. Let’ suppose, the user gives command as “JARVIS, open youtube.” So, open youtube will be in the user’s query, and the elif condition will be true.

      Defining Task 3: To open Google site in a web-browser
      elif 'open google' in query:
                  webbrowser.open("google.com")

      We are opening Google in a web-browser by applying the same logic that we used to open youtube. 

      Defining Task 4: To play music 

      To play music, we need to import a module called os. Import this module directly with an import statement.

      elif 'play music' in query:
                  music_dir = 'C:\Users\user\Music'
                  songs = os.listdir(music_dir)
                  print(songs)    
                  os.startfile(os.path.join(music_dir, songs[0]))

      In the above code, we first opened our music directory and then listed all the songs present in the directory with the help of the os module. With the help of os.starfile, you can play any song of your choice. Here we are playing the first song in the directory. However, you can also play a random song with the help of a random module. Every time you command to play music, J.A.R.V.I.S. will play any random song from the song directory.

      Defining Task 5: To know the current time
        elif 'the time' in query:
                  strTime = datetime.datetime.now().strftime("%H:%M:%S")    
                  speak(f"The time is {strTime}")

      In the above, code with are using datetime() function and storing the current or live of the system into a variable called strTime. After storing the time in strTime, we are passing this variable as an argument in speak function. Now, the time string will be converted into the speech.

      Final code

      import pyttsx3 #pip install pyttsx3
      import speech_recognition as sr #pip install speechRecognition
      import datetime
      import wikipedia #pip install wikipedia
      import webbrowser
      import os
      
      engine = pyttsx3.init('sapi5')
      voices = engine.getProperty('voices')
      # print(voices[1].id)
      engine.setProperty('voice', voices[0].id)
      
      
      def speak(audio):
          engine.say(audio)
          engine.runAndWait()
      
      
      def wishMe():
          hour = int(datetime.datetime.now().hour)
          if hour>=0 and hour<12:
              speak("Good Morning!")
      
          elif hour>=12 and hour<18:
              speak("Good Afternoon!")   
      
          else:
              speak("Good Evening!")  
      
          speak("I am Jarvis. Please tell me how may I help you")       
      
      def takeCommand():
          #It takes microphone input from the user and returns string output
      
          r = sr.Recognizer()
          with sr.Microphone() as source:
              print("Listening...")
              r.pause_threshold = 1
              audio = r.listen(source)
      
          try:
              print("Recognizing...")    
              query = r.recognize_google(audio, language='en-in')
              print(f"User said: {query}\n")
      
          except Exception as e:
              # print(e)    
              print("Say that again please...")  
              return "None"
          return query
      
      if __name__ == "__main__":
          wishMe()
          while True:
          # if 1:
              query = takeCommand().lower()
      
              # Logic for executing tasks based on query
              if 'wikipedia' in query:
                  speak('Searching Wikipedia...')
                  query = query.replace("wikipedia", "")
                  results = wikipedia.summary(query, sentences=2)
                  speak("According to Wikipedia")
                  print(results)
                  speak(results)
      
              elif 'open youtube' in query:
                  webbrowser.open("youtube.com")
      
              elif 'open google' in query:
                  webbrowser.open("google.com")
      
              elif 'open stackoverflow' in query:
                  webbrowser.open("stackoverflow.com")   
      
      
              elif 'play music' in query:
                  music_dir = 'C:/Users/user/Music'
                  songs = os.listdir(music_dir)
                  print(songs)    
                  os.startfile(os.path.join(music_dir, songs[0]))
      
              elif 'the time' in query:
                  strTime = datetime.datetime.now().strftime("%H:%M:%S")    
                  speak(f"Sir, the time is {strTime}")
      

      We have successfully created our JARVIS assistant

       

      • Share:
      author avatar
      Python Point Team

      Previous post

      How to make apps using python?
      December 31, 2022

      Next post

      How to master Python
      December 31, 2022

      You may also like

      15 Powerful Step for Mastering JSON Parsing in Python: Boosting Data Manipulation and Validation
      21 June, 2023

      Introduction In the world of programming, data plays a crucial role, and managing it efficiently is of utmost importance. JSON (JavaScript Object Notation) has emerged as a popular data interchange format due to its simplicity and flexibility. In this article, …

      Introduction to Transfer Learning with Python: A Practical Guide
      31 December, 2022

      Introduction: Definition of transfer learning Overview of how transfer learning works in the context of machine learning Why transfer learning is useful and important Section 1: Transfer learning in Python with Keras In this section, we will explore how to …

      How to Check Type in Python
      31 December, 2022

      In this article, we will learn to check type in Python. The built-in function type() can be used to check the type of data in Python.

      Subscribe
      Login
      Notify of
      Please login to comment
      0 Discussion
      Inline Feedbacks
      View all comments

      Latest Courses

      (Hindi) Ways to earn minimum 1 Lakh Per month as Programmer

      (Hindi) Ways to earn minimum 1 Lakh Per month as Programmer

      ₹10,000
      (HINDI) Full Stack Web Development In Python 3.8 And Django 3.1

      (HINDI) Full Stack Web Development In Python 3.8 And Django 3.1

      ₹25,000 ₹2,500

      Latest Posts

      • 15 Powerful Step for Mastering JSON Parsing in Python: Boosting Data Manipulation and Validation
      • Introduction to Transfer Learning with Python: A Practical Guide
      • How to Check Type in Python
      • How to make web crawler in python?
      • Why was the language called “python”?
      Contact
      •   support@pythonpoint.com

      We get you the best Python Courses and Blogs aiming to provide skill.

      We Believe Skill is much more important than a Degree

      Company
      • About Us
      • Blog
      • Offers
      • Contact
      Useful Links
      • Courses
      Support
      • Need Support

      © 2020 ALL RIGHTS RESERVED​ PYTHONPOINT.NET

      PythonPoint

      • Terms of Use
      • Refund Policy
      • Privacy Policy

      Login with your site account

      Lost your password?

      Not a member yet? Register now

      Register a new account

      Are you a member? Login now

      wpDiscuz