bomolopuu commited on
Commit
9c122f0
·
1 Parent(s): 7b359fc
Files changed (1) hide show
  1. app.py +30 -19
app.py CHANGED
@@ -1,48 +1,59 @@
1
  import gradio as gr
2
  import librosa
3
- import os
4
- import tempfile
5
- import shutil
6
  from asr import transcribe, ASR_LANGUAGES, model
7
  from lid import identify, LID_EXAMPLES
 
 
 
 
 
8
 
9
  def safe_process_file(file_obj):
10
  try:
11
- # Create a temporary directory
12
- with tempfile.TemporaryDirectory() as temp_dir:
13
- # Generate a safe file name
14
- safe_name = f"audio_{hash(file_obj.name)}.wav"
15
- temp_path = os.path.join(temp_dir, safe_name)
16
-
17
- # Copy the file to the temporary directory
18
- shutil.copy(file_obj.name, temp_path)
19
-
20
- # Load the audio from the temporary file
21
- audio, sr = librosa.load(temp_path)
22
-
23
- return audio, sr, safe_name
24
  except Exception as e:
25
- raise Exception(f"Error processing file: {str(e)}")
 
26
 
27
  def transcribe_multiple_files(audio_files, lang, transcription):
28
  transcriptions = []
29
  for audio_file in audio_files:
30
  try:
31
  audio, sr, safe_name = safe_process_file(audio_file)
 
 
 
 
32
  result = transcribe(model, audio, lang, transcription)
 
 
33
  transcriptions.append(f"File: {safe_name}\nTranscription: {result}\n")
34
  except Exception as e:
 
35
  transcriptions.append(f"Error processing file: {str(e)}\n")
36
  return "\n".join(transcriptions)
37
 
 
 
 
38
  mms_transcribe = gr.Interface(
39
  fn=transcribe_multiple_files,
40
  inputs=[
41
  gr.File(label="Audio Files", file_count="multiple"),
42
  gr.Dropdown(
43
- [f"{k} ({v})" for k, v in ASR_LANGUAGES.items()],
44
  label="Language",
45
- value="eng English",
46
  ),
47
  gr.Textbox(label="Optional: Provide your own transcription"),
48
  ],
 
1
  import gradio as gr
2
  import librosa
3
+ import io
 
 
4
  from asr import transcribe, ASR_LANGUAGES, model
5
  from lid import identify, LID_EXAMPLES
6
+ import logging
7
+
8
+ # Set up logging
9
+ logging.basicConfig(level=logging.DEBUG)
10
+ logger = logging.getLogger(__name__)
11
 
12
  def safe_process_file(file_obj):
13
  try:
14
+ logger.debug(f"Processing file: {file_obj.name}")
15
+ file_content = file_obj.read()
16
+
17
+ with io.BytesIO(file_content) as temp_file:
18
+ logger.debug("Loading audio with librosa")
19
+ audio, sr = librosa.load(temp_file)
20
+
21
+ safe_name = f"audio_{hash(file_obj.name)}.wav"
22
+ logger.debug(f"File processed successfully: {safe_name}")
23
+ return audio, sr, safe_name
 
 
 
24
  except Exception as e:
25
+ logger.error(f"Error processing file {file_obj.name}: {str(e)}")
26
+ raise
27
 
28
  def transcribe_multiple_files(audio_files, lang, transcription):
29
  transcriptions = []
30
  for audio_file in audio_files:
31
  try:
32
  audio, sr, safe_name = safe_process_file(audio_file)
33
+ logger.debug(f"Transcribing file: {safe_name}")
34
+ logger.debug(f"Language selected: {lang}")
35
+ logger.debug(f"User-provided transcription: {transcription}")
36
+
37
  result = transcribe(model, audio, lang, transcription)
38
+ logger.debug(f"Transcription result: {result}")
39
+
40
  transcriptions.append(f"File: {safe_name}\nTranscription: {result}\n")
41
  except Exception as e:
42
+ logger.error(f"Error in transcription process: {str(e)}")
43
  transcriptions.append(f"Error processing file: {str(e)}\n")
44
  return "\n".join(transcriptions)
45
 
46
+ # Prepare language options for Dropdown
47
+ language_options = [f"{k} ({v})" for k, v in ASR_LANGUAGES.items()]
48
+
49
  mms_transcribe = gr.Interface(
50
  fn=transcribe_multiple_files,
51
  inputs=[
52
  gr.File(label="Audio Files", file_count="multiple"),
53
  gr.Dropdown(
54
+ choices=language_options,
55
  label="Language",
56
+ value=language_options[0] if language_options else None,
57
  ),
58
  gr.Textbox(label="Optional: Provide your own transcription"),
59
  ],