-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVoiceAssistant.py
66 lines (55 loc) · 2.07 KB
/
VoiceAssistant.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Import necessary libraries
import speech_recognition as sr # Library for speech recognition
import pyttsx3 # Library for text-to-speech conversion
import datetime # Library for handling date and time
import random
import pyjokes # Library for generating jokes
import requests # Library for making HTTP requests
# Initialize the recognizer for speech recognition
r = sr.Recognizer()
# Initialize the text-to-speech engine
engine = pyttsx3.init()
# Set the voice to a female voice (change the index as needed)
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
# Function to convert text to speech
def speak(text):
engine.say(text)
engine.runAndWait()
# Function to get a motivational quote
def get_motivational_quote():
try:
response = requests.get("https://zenquotes.io/api/random")
data = response.json()
quote = data[0]['q']
return quote
except Exception as e:
return f"Sorry, I couldn't fetch a motivational quote at the moment. {e}"
# Initial greeting and available commands
speak("Hello Rabi, I am Xavier. I can help you with the time, date, tell a joke, or share a motivational quote. What can I do for you now?")
while True:
with sr.Microphone() as source:
print("Listening...")
audio = r.listen(source)
text = r.recognize_google(audio).lower()
print("You said:", text)
if "exit" in text:
speak("Goodbye! Have a great day.")
break
if "time" in text:
current_time = datetime.datetime.now().strftime("%I:%M %p")
response = f"The current time is {current_time}."
elif "date" in text:
current_date = datetime.datetime.now().strftime("%A, %B %d, %Y")
response = f"Today is {current_date}."
elif "joke" in text:
joke = pyjokes.get_joke()
response = joke
elif "motivation" in text:
quote = get_motivational_quote()
response = quote
else:
response = "oho...I am not able to do that."
speak(response)
if not "exit" in text:
speak("What can I do for you now?")