arshadrana commited on
Commit
5f94e97
·
verified ·
1 Parent(s): 6fdb7ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -12
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
- # Use BytesIO to create a file-like object from the audio bytes
16
- audio_file = BytesIO(audio_data_bytes)
17
-
18
- # Convert audio to WAV format using pydub
19
- audio_segment = AudioSegment.from_file(audio_file)
20
- wav_io = BytesIO()
21
- audio_segment.export(wav_io, format="wav")
22
- wav_io.seek(0) # Move to the beginning of the file-like object
23
-
24
- # Load the audio file from the file-like object in WAV format
25
- with sr.AudioFile(wav_io) as source:
 
 
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: