Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
import speech_recognition as sr
|
| 5 |
+
|
| 6 |
+
def transcribe_speech():
|
| 7 |
+
# Initialize the Recognizer object
|
| 8 |
+
r = sr.Recognizer()
|
| 9 |
+
|
| 10 |
+
# Open a Microphone source
|
| 11 |
+
with sr.Microphone() as source:
|
| 12 |
+
# Set the recording length (in seconds)
|
| 13 |
+
print("Listening...")
|
| 14 |
+
|
| 15 |
+
# Record the sound and adjust the volume
|
| 16 |
+
audio = r.listen(source)
|
| 17 |
+
|
| 18 |
+
# If the speech pause is greater than 1 second, transcribe the speech
|
| 19 |
+
if r.DynamicAdjustThreshold(source, audio, 1):
|
| 20 |
+
try:
|
| 21 |
+
# Use Google's web-based speech-to-text algorithm to convert the audio to text
|
| 22 |
+
text = r.recognize_google(audio, language='en-US')
|
| 23 |
+
#print(f"The text you spoke was: {text}")
|
| 24 |
+
return text
|
| 25 |
+
|
| 26 |
+
# If the speech was not recognized, throw an exception and run the function again
|
| 27 |
+
except sr.UnknownValueError:
|
| 28 |
+
print("Sorry, I can't understand you.")
|
| 29 |
+
except sr.RequestError as e:
|
| 30 |
+
print(f"Error; {e}")
|
| 31 |
+
|
| 32 |
+
else:
|
| 33 |
+
print("Speech pause is less than 1 second. Not transcribing.")
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
transcribe_speech
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def greet(name):
|
| 41 |
+
return "Hello " + name + "!!"
|
| 42 |
+
|
| 43 |
+
demo = gr.Interface(fn=transcribe_speech, inputs="text", outputs="text")
|
| 44 |
+
demo.launch()
|