Project 1: AI Desktop Voice Assistant | Python For Absolute Beginners
Personal Voice Assistant in Python
As we know Python is a suitable language for script writers and developers. Let’s write a script for Personal Voice Assistant using Python. The query for the assistant can be manipulated as per the user’s need.
The implemented assistant can open up the application (if it’s installed in the system), search Google, Wikipedia and YouTube about the query, calculate any mathematical question, etc by just giving the voice command. We can process the data as per the need or can add the functionality, depends upon how we code things.
We are using Google speech recognition API and google text to speech for voice input and output respectively.
PYTHON INBUILT MODULES TO BE INSTALLED AND IMPORTED
Pyttsx: 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.I used it for changing the rate of speech.
Pyttsx3: a cross-platform library, pyttsx3. This lets you synthesize text in to audio you can hear. This package works in Windows, Mac, and Linux. It uses native speech drivers when available and works completely offline.It has sapi5 which is The Speech Application Programming Interface or SAPI is an API developed by Microsoft to allow the use of speech recognition and speech synthesis within Windows applications. To date, a number of versions of the API have been released, which have shipped either as part of a Speech SDK or as part of the Windows OS itself. Applications that use SAPI include Microsoft Office, Microsoft Agent and Microsoft Speech Server.
Datetime:In Python, date, time and datetime classes provides a number of function to deal with dates, times and time intervals. Date and datetime are an object in Python, so when you manipulate them, you are actually manipulating objects and not string or timestamps. Whenever you manipulate dates or time, you need to import datetime function.
Speech_recognition: Speech recognition is the process of converting spoken words to text. Python supports many speech recognition engines and APIs, including Google Speech Engine, Google Cloud Speech API.
Webbrowser:The webbrowser module provides a high-level interface to allow displaying Web-based documents to users. Under most circumstances, simply calling the open() function from this module will do the right thing.
Wikipedia:Wikipedia is a Python library that makes it easy to access and parse data from Wikipedia.Search Wikipedia, get article summaries, get data like links and images from a page, and more. Wikipedia wraps the MediaWiki API so you can focus on using Wikipedia data, not getting it.
Os : This module provides a portable way of using operating system dependent functionality. If you just want to read or write a file see open().
Smtlib :Simple Mail Transfer Protocol (SMTP) is a protocol, which handles sending e-mail and routing e-mail between mail servers.Python provides smtplib module, which defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon.
CODE FOR VOICE ASSISTANT IS GIVEN BELOW:
import pyttsx3
import datetime
import speech_recognition as sr
import wikipedia
import webbrowser
import os
import smtplib
engine=pyttsx3.init('sapi5')
voices=engine.getProperty('voices')
engine.setProperty('voices',voices[0].id)
engine = pyttsx3.init()
rate = engine.getProperty('rate')
engine.setProperty('rate',rate-90)
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("Hye I am karen How can i help you")
def takeCommand():
r=sr.Recognizer()
with sr.Microphone() as source:
print("Listening!!!!!!!!!!!!!!!!!!!")
r.pause_threshold=1
audio = r.listen(source)
try:
print("RECOGNISING..")
query = r.recognize_google(audio,language='en-in')
print(f"User said:{query}\n")
except Exception as e:
print("say that again please")
return"NONE"
return query
def sendEmail(to,content):
server=smtplib.SMTP('smtp,gmail.com',587)
server.ehlo()
server.starttls()
server.login('your email', 'password')
server.sendmail('to whom you want to send mail id',to, content)
server.close()
if __name__=="__main__":
wishMe()
while True:
query=takeCommand().lower()
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:
speak("opening youtube just wait")
webbrowser.open("youtube.com")
elif 'open facebook' in query:
speak("opening facebook just wait")
webbrowser.open("facebook.com")
elif 'open google' in query:
speak("opening google please wait ")
webbrowser.open("google.com")
elif 'open blogger' in query:
speak("opening blogger please wait")
webbrowser.open("blogspot.com")
elif 'open quora' in query:
speak("opening quora please wait")
webbrowser.open("quora.com")
elif 'open udemy' in query:
speak("please wait opening udemy")
webbrowser.open("udemy.com")
elif 'open stackoverflow' in query:
speak("opening please wait")
webbrowser.open("stackoverflow.com")
elif 'open hackersrank' in query:
speak("opening please wait")
webbrowser.open("hackerrank.com")
elif 'open codechef' in query:
speak("opening please wait")
webbrowser.open("codechef.com")
elif 'open nptel swayam' in query:
speak("opening please wait")
webbrowser.open("swayam.gov.in")
elif 'open jeexams' in query:
speak("opening please wait!!!!")
webbrowser.open("jeexams.blogspot.com")
elif 'play music' in query:
music_dir='D:\\songs'
songs=os.listdir(music_dir)
print("songs")
os.startfile(os.path.join(music_dir,songs[0]))
elif 'play movies' in query:
movie_dir='D:\\movies'
movies=os.listdir(movie_dir)
print("movies")
os.startfile(os.path.join(movie_dir,movies[7]))
elif 'the time' in query:
strTime=datetime.datetime.now().strftime("%H;%M;%S")
speak(f"Sir the time is{strTime}")
elif 'email to JARVIS' in query:
try:
speak("what should i say")
content=takeCommand()
to="email of whom u want to send"
sendEmail(to,content)
speak("email has been send")
except Exception as e:
print(e)
speak("sorry i cant do the task")
FUNCTIONS PERFORMED BY YOUR VOICE ASSISTANT:
It can open various websites for you , for example : Google ,Facebook , youtube , Udemy , stackoverflow , hackersrank.com , codechef , Quora ,Blogger by just recognizing your voice as Windows PC assistant.
It can read the Wikipedia for you as per your order.
It can send Email.
It can play Music which is stored in your PC
It can play movies for you.
HOPE YOU ENJOY CODING IT!!!!!!!!
FOR ANY QUERY COMMENT DOWN BELOW