-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai.py
More file actions
26 lines (22 loc) · 732 Bytes
/
ai.py
File metadata and controls
26 lines (22 loc) · 732 Bytes
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
import speech_recognition as sr
# Create a Recognizer instance
r = sr.Recognizer()
# Function to take voice input
def voice_input():
# Initialize the microphone
with sr.Microphone() as source:
print("Speak now...")
# Listen for audio input
audio = r.listen(source)
try:
# Use Google Speech Recognition API to recognize speech
text = r.recognize_google(audio)
return text
except sr.UnknownValueError:
print("Sorry, I could not understand what you said.")
except sr.RequestError as e:
print(f"Speech recognition service error: {e}")
# Example usage
response = voice_input()
if response:
print(f"You said: {response}")