Jarvis/Voice Assistant using python 100% working code!!!
# Python program for voice assistant!!!
#INSTALL THE FOLLOWING MODULES FOR COMPLETE FUN!
#pip install pyaudio
#pip install Py-Dictionary/PyDictionary
#pip install flask
#pip install wikipedia
#pip install pywhatkit
#pip install pyjokes
#pip install pyttsx3
#pip install SpeechRecognition
#pip install beautifulSoup
from bs4 import BeautifulSoup
import requests
import speech_recognition as sr
import os
import pyttsx3
import wikipedia
import datetime
import webbrowser
from random import *
import pyjokes
import time
import pywhatkit
from pydictionary import Dictionary
# Initialize the recognizer
r = sr.Recognizer()
nam='julius'
# Function to convert text to speech
def SpeakText(command):
# Initializing the engine
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)
engine.setProperty('rate',170)
engine.say(command)
engine.runAndWait()
def wishme():
hr=int(datetime.datetime.now().hour)
if hr>=0 and hr<12:
SpeakText("Hi, its {},Good Morning , how may i help you".format(nam))
elif hr>=12 and hr<15:
SpeakText("Hi, its {},Good Afternoon , how may i help you".format(nam))
elif hr>=15:
SpeakText("Hi, its {},Good Evening , how may i help you".format(nam))
print("Say {} to activate".format(nam))
MyText=None
while(1):
with sr.Microphone() as source2:
r.adjust_for_ambient_noise(source2, duration=0.2)
#listens for the user's input
audio2 = r.listen(source2)
# Using google to recognize audio
MyText = r.recognize_google(audio2)
#converting the query to lower case so it detects easily
MyText = MyText.lower()
if nam in MyText:
wishme()
break
else:
print("Try Again...")
while(1):
try:
with sr.Microphone() as source2:
r.adjust_for_ambient_noise(source2, duration=0.2)
#listens for the user's input
audio2 = r.listen(source2)
# Using google to recognize audio
MyText = r.recognize_google(audio2)
MyText = MyText.lower()
if "bye" in MyText or "quit" in MyText or "exit" in MyText or "shut up" in MyText:
print("Good bye thanks for your time,have a great day")
SpeakText("Good bye thanks for your time,have a great day")
break
exit
elif "wikipedia" in MyText: #word
try:
SpeakText("Searching in Wikipedia") #respond to your word
MyText=MyText.replace("wikipedia","")
src=wikipedia.summary(MyText,sentences=1) #action
SpeakText("According to Wikipedia...")
print(src)
#time.sleep(30)
SpeakText(src)
except exception as e:
print(e)
elif "music" in MyText:
try:
dr='C:/Users/Administrator/Desktop/Selected music/Even more Selected Music'
sngs=os.listdir(dr)
for i in range(len(sngs)):
print(sngs[i])
SpeakText("Playing a Random song out of them...")
x=randint(0,6)
os.startfile(os.path.join(dr,sngs[x]))
except exception as e:
print(e)
elif "open youtube" in MyText:
SpeakText("Opening Youtube...")
webbrowser.open("youtube.com")
elif "dictionary" in MyText:
try:
SpeakText("Which word's meaning you want to know?")
r.adjust_for_ambient_noise(source2, duration=0.2)
audio2 = r.listen(source2)
MyText = r.recognize_google(audio2)
MyText = MyText.lower()
dict = Dictionary(MyText)
#meaning
meanings_list = dict.meanings()
print("Meaning :",meanings_list[0])
SpeakText(meanings_list[0])
#Synonms
synonyms_list= dict.synonyms()
print("Synonym :",synonyms_list[0])
sy="Synonym of "+MyText+" is "+synonyms_list[0]
SpeakText(sy)
#Antonyms
antonyms_list = dict.antonyms()
print("Antonym :",antonyms_list[0])
ant="Antonym of "+MyText+" is "+antonyms_list[0]
SpeakText(ant)
except exception as e:
print(e)
elif "play" in MyText:
try:
MyText=MyText.replace("play","")
d='playing '+MyText+' from youtube...'
print(d)
SpeakText(d)
pywhatkit.playonyt(MyText);
except exception as e:
print(e)
elif "open google" in MyText:
SpeakText("Opening Google...")
webbrowser.open("google.com")
elif "time" in MyText:
ctime=datetime.datetime.now().strftime("%H:%M:%S")
st="The time now is "+ctime
SpeakText(st)
elif "i love you" in MyText:
SpeakText("Good for you , but , who doesn't love me?")
elif "joke" in MyText:
my_joke=pyjokes.get_jokes(language="en",category="all")
jk=randint(1,100)
print(my_joke[jk])
SpeakText(my_joke[jk])
elif "are you" in MyText:
SpeakText("I am good what about you...")
elif "doing great" in MyText:
SpeakText("That's Great to hear...")
elif nam in MyText:
SpeakText("I am listening go ahead...")
elif "news" in MyText:
try:
SpeakText("Which type of news you want to hear")
print("Sports News")
SpeakText("Sports News")
print("Technology News")
SpeakText("Technology News")
print("Business News")
SpeakText("Business News")
print("Science News")
SpeakText("Science News")
print("Entertainment News")
SpeakText("Entertainment News")
r.adjust_for_ambient_noise(source2, duration=0.2)
audio2 = r.listen(source2)
MyText = r.recognize_google(audio2)
MyText = MyText.lower()
if "business" in MyText:
SpeakText("Fetching some Business news for you!")
url="https://www.inshorts.com/en/read/business"
def GetResult(url):
result=requests.get(url)
soup=BeautifulSoup(result.text,'html.parser')
return soup
soup=GetResult(url)
news=soup.findAll('span',attrs={'itemprop':'headline'})
lis=[]
for span in news:
lis.append(span.getText())
for i in range(5):
print(lis[i])
SpeakText(lis[i])
elif "sports" in MyText:
SpeakText("Fetching some sports news for you!")
url="https://www.inshorts.com/en/read/sports"
def GetResult(url):
result=requests.get(url)
soup=BeautifulSoup(result.text,'html.parser')
return soup
soup=GetResult(url)
news=soup.findAll('span',attrs={'itemprop':'headline'})
lis=[]
for span in news:
lis.append(span.getText())
for i in range(5):
print(lis[i])
SpeakText(lis[i])
elif "technology" in MyText:
SpeakText("Fetching some technology news for you!")
url="https://www.inshorts.com/en/read/technology"
def GetResult(url):
result=requests.get(url)
soup=BeautifulSoup(result.text,'html.parser')
return soup
soup=GetResult(url)
news=soup.findAll('span',attrs={'itemprop':'headline'})
lis=[]
for span in news:
lis.append(span.getText())
for i in range(5):
print(lis[i])
SpeakText(lis[i])
elif "entertainment" in MyText:
SpeakText("Fetching some entertainment news for you!")
url="https://www.inshorts.com/en/read/entertainment"
def GetResult(url):
result=requests.get(url)
soup=BeautifulSoup(result.text,'html.parser')
return soup
soup=GetResult(url)
news=soup.findAll('span',attrs={'itemprop':'headline'})
lis=[]
for span in news:
lis.append(span.getText())
for i in range(5):
print(lis[i])
SpeakText(lis[i])
elif "science" in MyText:
SpeakText("Fetching some science news for you!")
url="https://www.inshorts.com/en/read/science"
def GetResult(url):
result=requests.get(url)
soup=BeautifulSoup(result.text,'html.parser')
return soup
soup=GetResult(url)
news=soup.findAll('span',attrs={'itemprop':'headline'})
lis=[]
for span in news:
lis.append(span.getText())
for i in range(5):
print(lis[i])
SpeakText(lis[i])
except exception as e:
print(e)
elif "thank" in MyText:
SpeakText("Pleasure is mine what else can i do for you...")
elif "weather" in MyText or "temperature" in MyText:
try:
url = f"https://www.google.com/search?q=temperature in gandhinagar"
rq = requests.get(url)
data = BeautifulSoup(rq.text,"html.parser")
temp = data.find("div",class_="BNeawe").text
#print(temp)
#
url='https://www.timeanddate.com/weather/india/gandhinagar/ext'
def getResult(url):
result=requests.get(url)
soup=BeautifulSoup(result.text,'html.parser')
return soup
soup=getResult(url)
news=soup.findAll('td',attrs={'class':'small'})
lis=[]
for td in news:
lis.append(td.getText())
#print(temp,lis[0])
ty=str(lis[0])
tmp=str(temp)
x="Current Temperature in Gandhinagar is "
x=x+tmp+" and "+ty
print(x)
SpeakText(x)
except exception as e:
print(e)
else:
SpeakText("I did not quite get that...")
except sr.RequestError as e:
print("Could not request results; {0}".format(e))
except sr.UnknownValueError:
print("unknown error occured")
Comments
Post a Comment