Spaces:
Sleeping
Sleeping
File size: 903 Bytes
f0f6001 | 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 | import speech_recognition as sr
from gtts import gTTS
from playsound import playsound
def speech_to_text():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Speak something...")
audio = recognizer.listen(source)
try:
user_input = recognizer.recognize_google(audio)
return user_input
except sr.UnknownValueError:
print("Sorry, could not understand audio.")
return None
except sr.RequestError as e:
print("Could not request results; {0}".format(e))
return None
def text_to_speech(text):
tts = gTTS(text=text, lang='en')
tts.save("output.mp3")
playsound("output.mp3")
def main():
user_input = speech_to_text()
if user_input:
print("You said:", user_input)
text_to_speech(user_input)
if __name__ == "__main__":
main()
|