mgokg commited on
Commit
f58dbd6
·
verified ·
1 Parent(s): 85d1e6a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -22
app.py CHANGED
@@ -1,6 +1,4 @@
1
  import gradio as gr
2
-
3
-
4
  import speech_recognition as sr
5
 
6
  def transcribe_speech():
@@ -12,33 +10,29 @@ def transcribe_speech():
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()
 
1
  import gradio as gr
 
 
2
  import speech_recognition as sr
3
 
4
  def transcribe_speech():
 
10
  # Set the recording length (in seconds)
11
  print("Listening...")
12
 
13
+ # Adjust for ambient noise
14
+ r.adjust_for_ambient_noise(source)
15
+
16
+ # Record the sound
17
  audio = r.listen(source)
18
 
19
+ try:
20
+ # Use Google's web-based speech-to-text algorithm to convert the audio to text
21
+ text = r.recognize_google(audio, language='en-US')
 
 
 
22
  return text
23
 
24
+ except sr.UnknownValueError:
25
+ print("Sorry, I can't understand you.")
26
+ return "Sorry, I can't understand you."
 
 
 
 
 
 
 
 
 
27
 
28
+ except sr.RequestError as e:
29
+ print(f"Error: {e}")
30
+ return f"Error: {e}"
31
 
32
+ # Define a simple greeting function for demonstration
33
  def greet(name):
34
  return "Hello " + name + "!!"
35
 
36
+ # Create a Gradio interface for the transcription function
37
+ demo = gr.Interface(fn=transcribe_speech, inputs="microphone", outputs="text")
38
  demo.launch()