-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaiva.py
More file actions
46 lines (40 loc) 路 1.26 KB
/
Copy pathaiva.py
File metadata and controls
46 lines (40 loc) 路 1.26 KB
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
import speech_recognition as sr
import pyttsx3
# Initialize the speech engine
engine = pyttsx3.init()
def speak(text):
engine.say(text)
engine.runAndWait()
def listen():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("馃帳 Listening...")
audio = recognizer.listen(source)
try:
command = recognizer.recognize_google(audio)
print(f" You said: {command}")
return command.lower()
except sr.UnknownValueError:
speak("Sorry, I didn't catch that.")
return ""
except sr.RequestError:
speak("Sorry, my speech service is down.")
return ""
def respond(command):
if "hello" in command:
speak("Hello Ashwinee! How can I help you today?")
elif "your name" in command:
speak("I'm your Python voice assistant.")
elif "time" in command:
from datetime import datetime
now = datetime.now().strftime("%H:%M")
speak(f"The time is {now}")
elif "exit" in command:
speak("Goodbye!")
exit()
else:
speak("I didn't understand that. Can you try again?")
# Main loop
while True:
command = listen()
respond(command)