Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
import speech_recognition as sr
|
3 |
-
from io import BytesIO
|
4 |
from pydub import AudioSegment
|
|
|
5 |
|
6 |
def transcribe_audio(audio_input):
|
7 |
recognizer = sr.Recognizer()
|
@@ -12,17 +12,19 @@ def transcribe_audio(audio_input):
|
|
12 |
else:
|
13 |
raise ValueError("Expected audio_input to be a tuple with audio data bytes.")
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
26 |
audio_data = recognizer.record(source)
|
27 |
|
28 |
try:
|
|
|
1 |
import gradio as gr
|
2 |
import speech_recognition as sr
|
|
|
3 |
from pydub import AudioSegment
|
4 |
+
import tempfile
|
5 |
|
6 |
def transcribe_audio(audio_input):
|
7 |
recognizer = sr.Recognizer()
|
|
|
12 |
else:
|
13 |
raise ValueError("Expected audio_input to be a tuple with audio data bytes.")
|
14 |
|
15 |
+
# Write audio data to a temporary file in its original format
|
16 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio_file:
|
17 |
+
temp_audio_file.write(audio_data_bytes)
|
18 |
+
temp_audio_file_path = temp_audio_file.name
|
19 |
+
|
20 |
+
# Convert to WAV format using pydub and re-read for compatibility
|
21 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as wav_file:
|
22 |
+
audio_segment = AudioSegment.from_file(temp_audio_file_path)
|
23 |
+
audio_segment.export(wav_file.name, format="wav")
|
24 |
+
wav_file_path = wav_file.name
|
25 |
+
|
26 |
+
# Load the WAV file for transcription
|
27 |
+
with sr.AudioFile(wav_file_path) as source:
|
28 |
audio_data = recognizer.record(source)
|
29 |
|
30 |
try:
|