-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeech_check.py
57 lines (48 loc) · 1.79 KB
/
speech_check.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
import speech_recognition as sr
import google.generativeai as genai
from gtts import gTTS
from playsound import playsound
import os
API_KEY = "YOUR_API_KEY"
def listen_and_convert():
# Initialize the recognizer
recognizer = sr.Recognizer()
try:
with sr.Microphone() as source:
recognizer.adjust_for_ambient_noise(source, duration=1)
print("Listening... Say something:")
audio = recognizer.listen(source, timeout=5) # Adjust the timeout as needed
# Recognize speech using Google Web Speech API
text = recognizer.recognize_google(audio)
print(f"You said: {text}")
return text
except sr.WaitTimeoutError:
print("Timed out. No speech detected.")
return None
except sr.UnknownValueError:
print("Sorry, I couldn't understand what you said.")
return None
except sr.RequestError as e:
print(f"Error connecting to Google Web Speech API: {e}")
return None
def get_response(user_input):
# Generate a response using OpenAI's API
genai.configure(api_key=API_KEY)
model = genai.GenerativeModel(model_name="gemini-1.5-flash-exp-0827")
response = model.generate_content(user_input)
return response.text
if __name__ == "__main__":
while True:
input_text = listen_and_convert()
if input_text:
print(f"Converted text: {input_text}")
# response = get_response(input_text)
response = input_text
if response:
print(response)
acc = 'us'
tts = gTTS(text=response, lang='en', tld=acc)
filename = 'temp.mp3'
tts.save(filename)
playsound(filename)
os.remove(filename)