Spaces:
Sleeping
Sleeping
| 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() | |