Spaces:
Sleeping
Sleeping
Commit ·
f0f6001
1
Parent(s): ac9c1ed
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import speech_recognition as sr
|
| 2 |
+
from gtts import gTTS
|
| 3 |
+
from playsound import playsound
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def speech_to_text():
|
| 7 |
+
recognizer = sr.Recognizer()
|
| 8 |
+
|
| 9 |
+
with sr.Microphone() as source:
|
| 10 |
+
print("Speak something...")
|
| 11 |
+
audio = recognizer.listen(source)
|
| 12 |
+
|
| 13 |
+
try:
|
| 14 |
+
user_input = recognizer.recognize_google(audio)
|
| 15 |
+
return user_input
|
| 16 |
+
except sr.UnknownValueError:
|
| 17 |
+
print("Sorry, could not understand audio.")
|
| 18 |
+
return None
|
| 19 |
+
except sr.RequestError as e:
|
| 20 |
+
print("Could not request results; {0}".format(e))
|
| 21 |
+
return None
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def text_to_speech(text):
|
| 25 |
+
tts = gTTS(text=text, lang='en')
|
| 26 |
+
tts.save("output.mp3")
|
| 27 |
+
playsound("output.mp3")
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def main():
|
| 31 |
+
user_input = speech_to_text()
|
| 32 |
+
if user_input:
|
| 33 |
+
print("You said:", user_input)
|
| 34 |
+
text_to_speech(user_input)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
if __name__ == "__main__":
|
| 38 |
+
main()
|