0% found this document useful (0 votes)
57 views4 pages

Voice Assistant Code

The document describes an AI assistant named Sofia that can perform various tasks like telling the time, searching Wikipedia, playing music and sending WhatsApp messages upon user commands. The assistant interacts with the user by taking voice commands and also has capabilities like wishing on greetings and introducing itself.

Uploaded by

barkalesnehal1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views4 pages

Voice Assistant Code

The document describes an AI assistant named Sofia that can perform various tasks like telling the time, searching Wikipedia, playing music and sending WhatsApp messages upon user commands. The assistant interacts with the user by taking voice commands and also has capabilities like wishing on greetings and introducing itself.

Uploaded by

barkalesnehal1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

# pip install pyaudio

import datetime
import os
import smtplib
import webbrowser

import pyttsx3 # pip install pyttsx3


import pywhatkit
import speech_recognition as sr # pip install speechRecognition
import wikipedia # pip install wikipedia

engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
# print(voices[1].id)
engine.setProperty('voice', voices[1].id)

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("hello i am your assistant sofia how may I help you ")

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

def sendEmail(to, content):


server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login('[email protected]', 'vnlxewvpyqtprupj')
server.sendmail('[email protected]', to, content)
server.close()

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 'please introduce us and yourself' in query:


speak("Hello everyone my name is sofia and i am a voice
assistant created by Omkar,Snehal and Shivraj")
print("Hello everyone my name is sofia and i am a voice
assistant created by Omkar,Snehal ,Shivraj")

elif 'open google' in query:


webbrowser.open("google.com")

elif 'open stackoverflow' in query:


webbrowser.open("stackoverflow.com")

elif 'hello sofia' in query or "hello" in query:


speak("hello nice to meet you How are you")

elif 'how are you' in query:


speak("i am fine thank you for asking")

elif 'i am good' in query or 'i am fine' in query:


speak("nice to hear that")

elif 'play bhajans' in query:


music_dir = 'C:\\Users\\music\\bhajans'
songs = os.listdir(music_dir)
print(songs)
os.startfile(os.path.join(music_dir, songs[0]))

elif 'play songs' in query:


music_dir = 'C:\\Users\\music\\Arijit Singh'
songs = os.listdir(music_dir)
print(songs)
os.startfile(os.path.join(music_dir, songs[0]))

elif 'whatsapp to snehal' in query:


speak("What should I say?")
content = takeCommand()
from datetime import datetime, timedelta

current_time = datetime.now()
scheduled_time = current_time + timedelta(seconds=60)
pywhatkit.sendwhatmsg("+919004946621", content,
scheduled_time.hour, scheduled_time.minute)

elif 'whatsapp to omkar' in query:


speak("What should I say?")
content = takeCommand()
from datetime import datetime, timedelta

current_time = datetime.now()
scheduled_time = current_time + timedelta(seconds=60)
pywhatkit.sendwhatmsg("+919967253454", content,
scheduled_time.hour, scheduled_time.minute)

elif 'the time' in query:


strTime = datetime.datetime.now().strftime("%H:%M:%S")
speak(f"the time is {strTime}")
print(strTime)

elif 'open my ppt' in query:


speak("opening Power Point presentation")
power = r"C:\Users\barka\OneDrive\Documents\NPresentation.pptx"
os.startfile(power)

elif 'open code' in query:


codePath =
"C:\\Users\\Haris\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe"
os.startfile(codePath)

elif 'send email to snehal' in query:


try:
speak("What should I say?")
content = takeCommand()
print(content)
to = "[email protected]"
sendEmail(to, content)
speak("Email has been sent!")
except Exception as e:
print(e)
speak("Sorry I am not able to send this email")

elif 'send email to omkar' in query:


try:
speak("What should I say?")
content = takeCommand()
print(content)
to = "[email protected]"
sendEmail(to, content)
speak("Email has been sent!")
except Exception as e:
print(e)
speak("Sorry I am not able to send this email")

elif " search youtube" in query:


try:
speak("this is what i found")
query = query.replace("youtube search","")
query = query.replace("youtube", "")
query = query.replace("sofia", "")
web = "https://www.youtube.com/results?search_query=" +
query
webbrowser.open(web)
pywhatkit.playonyt(query)
except Exception as e:
print(e)
speak("Sorry")

else:
print("No query matched")

You might also like