JARVIS In Python With Full Source Code (BUILD YOUR OWN FRIEND)
Hi, I am scientificoder I am going to tell you that how you can make your own Jarvis in Python.
First name a file main.py
and press enter.
1. Speek Function
Here we are trying to make a model such that our python can understand our language and perform certain functions.
So first go to your Windows Terminal (by right clicking the start button) and write pip install pyttsx3
and press enter.
Then write pip install SpeechRecognition
and press enter.
import pyttsx3
import speech_recognitionengine = pyttsx3.init("sapi5")
voices = engine.getProperty("voices")
engine.setProperty("voice", voices[0].id)
rate = engine.setProperty("rate",170)def speak(audio):
engine.say(audio)
engine.runAndWait()def takeCommand():
r = speech_recognition.Recognizer()
with speech_recognition.Microphone() as source:
print("Listening.....")
r.pause_threshold = 1
r.energy_threshold = 300
audio = r.listen(source,0,4) try:
print("Understanding..")
query = r.recognize_google(audio,language='en-in')
print(f"You Said: {query}\n")
except Exception as e:
print("Say that again")
return "None"return query
2. Greet Me Function
Now after writing the speak function we are ready to make Jarvis Greet Us Good Morning or Good Night according to the time.
So first paste the code within the main.py
file.
if __name__ == "__main__":
while True:
query = takeCommand().lower()
if "wake up" in query:
from GreetMe import greetMe
greetMe()
Then Make A NEW File named as greet.py
and paste the following code:-
import pyttsx3
import datetimeengine = pyttsx3.init("sapi5")
voices = engine.getProperty("voices")
engine.setProperty("voice", voices[0].id)
engine.setProperty("rate",200)def speak(audio):
engine.say(audio)
engine.runAndWait()def greetMe():
hour = int(datetime.datetime.now().hour)
if hour>=0 and hour<=12:
speak("Good Morning,sir")
elif hour >12 and hour<=18:
speak("Good Afternoon ,sir")
else:
speak("Good Evening,sir") speak("Please tell me, How can I help you ?")
Now you are ready to run the file and Jarvis would respond to you according to your time.
3. Searching from web
For Searching from web just write the following code in main.py
elif "google" in query:
from SearchNow import searchGoogle
searchGoogle(query)elif "youtube" in query:
from SearchNow import searchYoutube
searchYoutube(query)elif "wikipedia" in query:
from SearchNow import searchWikipedia
searchWikipedia(query)
Then make a new file and name it SearchNow.py
in and write the following code :-
But Before that just open your Windows Terminal and install a few pakages like pip install pywhatkit
, pip install wikipedia
import speech_recognition
import pyttsx3
import pywhatkit
import wikipedia
import webbrowserdef takeCommand():
r = speech_recognition.Recognizer()
with speech_recognition.Microphone() as source:
print("Listening.....")
r.pause_threshold = 1
r.energy_threshold = 300
audio = r.listen(source,0,4)
try:
print("Understanding..")
query = r.recognize_google(audio,language='en-in')
print(f"You Said: {query}\n")
except Exception as e:
print("Say that again")
return "None"
return queryquery = takeCommand().lower()engine = pyttsx3.init("sapi5")
voices = engine.getProperty("voices")
engine.setProperty("voice", voices[0].id)
engine.setProperty("rate",170)def speak(audio):
engine.say(audio)
engine.runAndWait()def searchGoogle(query):
if "google" in query:
import wikipedia as googleScrap
query = query.replace("jarvis","")
query = query.replace("google search","")
query = query.replace("google","")
speak("This is what I found on google") try:
pywhatkit.search(query)
result = googleScrap.summary(query,1)
speak(result) except:
speak("No speakable output available")def searchYoutube(query):
if "youtube" in query:
speak("This is what I found for your search!")
query = query.replace("youtube search","")
query = query.replace("youtube","")
query = query.replace("jarvis","")
web = "https://www.youtube.com/results?search_query=" + query
webbrowser.open(web)
pywhatkit.playonyt(query)
speak("Done, Sir")def searchWikipedia(query):
if "wikipedia" in query:
speak("Searching from wikipedia....")
query = query.replace("wikipedia","")
query = query.replace("search wikipedia","")
query = query.replace("jarvis","")
results = wikipedia.summary(query,sentences = 2)
speak("According to wikipedia..")
print(results)
speak(results)
DONE NOW YOU CAN SEARCH ANYTHING FROM GOOGLE, WIKIPEDIA AND YOUTUBE JUST SAY THE THINGS THAT YOU WROTE IN THE elif statements
you wrote above..
For Example you said “google titanic” then it would open up google and search the word titanic in it. Similarly if you said “ youtube titanic theme song” then it would open youtube and play the first search result for you.
HOPE YOU LIKED IT
BY SCIENTIFICODER
For Full Source Code With a Youtube Video For full Explanation Do Visit :-
https://scientificoder.blogspot.com/2021/12/jarvis-trilogy-part-1.html