-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmajorpro.py
More file actions
46 lines (41 loc) · 1.28 KB
/
Copy pathmajorpro.py
File metadata and controls
46 lines (41 loc) · 1.28 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 recognizer and text-to-speech engine
recognizer = sr.Recognizer()
engine = pyttsx3.init()
def speak(text):
engine.say(text)
engine.runAndWait()
def listen():
with sr.Microphone() as source:
print("Listening...")
audio = recognizer.listen(source)
try:
command = recognizer.recognize_google(audio)
print("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! How can I help you today?")
elif "your name" in command:
speak("I'm your Python 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 or "bye" in command:
speak("Goodbye!")
exit()
else:
speak("I don't understand that yet.")
# Main loop
while True:
command = listen()
if command:
respond(command)