Python virtual assistance
Python vitual assistance
This virtual assistance is just like your google assistanece or siri!, it perform many tasks like open browers/youtube/google, told you time, date and days,
This is a Beta version of this assistance so it not perform complicated task right now you can add more code and bring it to next level !
import pyttsx3
import datetime
import speech_recognition as sr
import wikipedia
import webbrowser
import os
import requests
from bs4 import BeautifulSoup
import pywhatkit
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
# print(voices[1].id)
engine.setProperty('voice',voices[1].id)
# l = ['play music','search youtube']
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 your assistant sir, Please tell me how may i help you")
def takeCommand():
'''it take microphone input from user and
return string output '''
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recoginizing...")
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 excuting task
if 'wikipedia' in query:
try:
speak('searching wikipedia... Please wait')
query = query.replace('wikipedia','')
results = wikipedia.summary(query,sentences=2)
print(results)
speak("According to wikipedia")
speak(results)
except Exception as e:
# print(e)
print('Sorry sir, Did not find it')
speak('Sorry sir, Did not find it')
elif "temperature " in query :
speak("In which city sir?")
city = takeCommand().lower()
search = f"temperature in {city}"
url = f"https://www.google.com/search?q={search}"
x = requests.get(url)
data = BeautifulSoup(x.text,"html.parser")
temp = data.find("div",class_="BNeawe").text
print(f"sir, current temprature in {city} is {temp}")
speak(f"sir, current temprature in {city} is {temp}")
elif "google search" in query or "search" in query:
speak("Speak your query sir?")
try:
yourQuery = takeCommand().lower()
search = f"searching.. {yourQuery}"
url = f"https://www.google.com/search?q={yourQuery}"
x = requests.get(url)
data = BeautifulSoup(x.text,"html.parser")
result = data.find("div",class_="BNeawe").text
print(f"sir, i found this on google, {result}")
speak(f"sir i found this on google, {result}")
except Exception as e:
print('Sorry sir, Did not find it')
speak('Sorry sir, Did not find it')
elif "open youtube" in query:
speak("opening youtube sir")
webbrowser.open("youtube.com")
elif "play music" in query :
speak("Which songs would you like to listen sir ?")
yourQuery = takeCommand().lower()
search = f"playing... {yourQuery}"
pywhatkit.playonyt(yourQuery)
print(f"opening youtube and {search} sir please wait...")
speak("opening youtube and Playing sir please wait...")
elif "open google" in query:
speak("opening google sir")
webbrowser.open("google.com")
elif 'time' in query:
strTime = datetime.datetime.now().strftime("%I:%M %p")
speak(f"Sir, The time is {strTime}")
print(strTime)
elif 'date' in query or 'day' in query:
strDate = datetime.date.today()
strDay = datetime.datetime.now().strftime("%A")
print(strDate)
print(strDay)
speak(f"Sir, Today is {strDate} and {strDay}")
elif 'open code' in query:
codePath = "C:\\Users\\Onamk\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe"
os.startfile(codePath)
elif 'close browser' in query or 'close this' in query:
# closeBrowser = "msedge.exe"
speak("closing browser sir")
os.system("taskkill /im msedge.exe /f")
elif 'bye bye' in query:
speak("Okay sir, bye bye and Tata have a nice day,see you soon ")
os._exit(0)
# break
Comments
Post a Comment