voicebased / app.py
geethareddy's picture
Update app.py
7bae5a3 verified
raw
history blame
1.79 kB
import speech_recognition as sr
import pyttsx3
import time
import sounddevice as sd
def speak(text):
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
def get_voice_input(prompt, timeout=30):
recognizer = sr.Recognizer()
microphone = sr.Microphone()
speak(prompt)
with microphone as source:
print(prompt)
recognizer.adjust_for_ambient_noise(source)
try:
audio = recognizer.listen(source, timeout=timeout)
except sr.WaitTimeoutError:
speak("Time is up. Please try again.")
return get_voice_input(prompt, timeout)
try:
text = recognizer.recognize_google(audio)
print(f"Recognized: {text}")
return text
except sr.UnknownValueError:
speak("Sorry, I could not understand. Please repeat.")
return get_voice_input(prompt, timeout)
except sr.RequestError:
speak("Speech service is unavailable right now.")
return None
if __name__ == "__main__":
speak("Welcome to Biryani Hub")
username = get_voice_input("Please tell me your name. You have 30 seconds.", timeout=30)
if username:
speak(f"You said {username}. Now, please tell me your email address.")
email = get_voice_input("Please tell me your email address. You have 30 seconds.", timeout=30)
else:
speak("No response received for the name. Please try again later.")
exit()
if email:
with open("user_details.txt", "w") as file:
file.write(f"Username: {username}\n")
file.write(f"Email: {email}\n")
speak("Thank you! Your details have been recorded.")
else:
speak("No response received for the email. Please try again later.")