ML PROJECT (Personal AI Virtual Assistant)

Prince Prashant Saini
4 min readSep 5, 2020

We have create own personal virtual assistant with help of ML and AI for this we have import some module before import we have download some module we have import with pip and pip3

Pyttsx3=>

pyttsx3 is a text-to-speech conversion library in Python. Unlike alternative libraries, it works offline, and is compatible with both Python 2 and 3

pip install pyttsx3
pip install pypiwin32

SpeechRecognition=>

Library for performing speech recognition, with support for several engines and APIs, online and offline.

Speech recognition engine/API support

pip install SpeechRecognition

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 i

pip install wikipedia

PyAudio=>

PyAudio v0.2.11: Python Bindings for PortAudio.

Copyright © 2006 Hubert Pham

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

pip install PyAudio
or
conda install pyaudio

Now start writing the code we have import module first

import pyttsx3
import speech_recognition as sr
import datetime
import wikipedia
import webbrowser
import os
import smtplib

imagine how easier to send email without typing single word , also search Wikipedia they read this Wikipedia and all task

Now we have written pyttex3 functions and check speck or not and also see how many type of voice available or not

engine = pyttsx3.init(‘sapi5’)
voices = engine.getProperty(‘voice’)
engine.setProperty(‘voice’, voices)

for check

def speak(audio):
engine.say(audio)
engine.runAndWait()

Now we have create wish me function so my AI wish me with time liek in in Morning they speack good morning and afternoon they speck good afternoon and good evening

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 Machine Who created by prince Prashant saini Sir. Please tell me how may I help you”)

Now create input funtion that create microphone input and back to back take input

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

Now create a function for mail server for sending a mail for this and give password so that have send mail

def sendEmail(to, content):
server = smtplib.SMTP(‘smtp.gmail.com’, 587)
server.ehlo()
server.starttls()
server.login(‘youremail@gmail.com’, ‘your-password’)
server.sendmail(‘youremail@gmail.com’, to, content)
server.close()

Now last create if take input so we send to Wikipedia, webbrowser or os path
now first time we have send all of one email then send

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 ‘open rightArth’ in query:
webbrowser.open(“rightarth.com”)

elif ‘open Linux world india’ in query:
webbrowser.open(“lwindia.com”)

elif ‘play music’ in query:
music_dir = ‘C:\\songs\\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}”)

elif ‘open code’ in query:
codePath = “C:\\Users\\Haris\\AppData\\Local\\Code.py”
os.startfile(codePath)

elif ‘email to harry’ in query:
try:
speak(“What should I say?”)
content = takeCommand()
to = “sender@gmail.com”
sendEmail(to, content)
speak(“Email has been sent!”)
except Exception as e:
print(e)
speak(“Sorry . I am not able to send this email”)

🙏🙏🙏Now Thanks For Reading 🙏🙏🙏

--

--