patrickvonplaten commited on
Commit
77a871f
1 Parent(s): c35fa41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -5
app.py CHANGED
@@ -16,17 +16,28 @@ def process_audio_file(file):
16
  input_values = feature_extractor(data, return_tensors="pt").input_values
17
  return input_values
18
 
19
- def transcribe(file, target_language):
20
 
21
  target_code = target_language.split("(")[-1].split(")")[0]
22
  forced_bos_token_id = MAPPING[target_code]
23
-
 
 
 
 
 
 
 
 
 
 
 
24
  input_values = process_audio_file(file)
25
 
26
- sequences = model.generate(input_values, forced_bos_token_id=forced_bos_token_id)
27
 
28
  transcription = tokenizer.batch_decode(sequences, skip_special_tokens=True)
29
- return transcription[0]
30
 
31
  target_language = [
32
  "English (en)",
@@ -69,7 +80,8 @@ MAPPING = {
69
  iface = gr.Interface(
70
  fn=transcribe,
71
  inputs=[
72
- gr.inputs.Audio(source="microphone", type='filepath'),
 
73
  gr.inputs.Dropdown(target_language),
74
  ],
75
  outputs="text",
16
  input_values = feature_extractor(data, return_tensors="pt").input_values
17
  return input_values
18
 
19
+ def transcribe(file_mic, file_upload, target_language):
20
 
21
  target_code = target_language.split("(")[-1].split(")")[0]
22
  forced_bos_token_id = MAPPING[target_code]
23
+
24
+ warn_output = ""
25
+ if (file_mic is not None) and (file_upload is not None):
26
+ warn_output = "WARNING: You've uploaded an audio file and used the microphone. The recorded file from the microphone will be used and the uploaded audio will be discarded.\n"
27
+ file = file_mic
28
+ elif (file_mic is None) and (file_upload is None):
29
+ return "ERROR: You have to either use the microphone or upload an audio file"
30
+ elif file_mic is not None:
31
+ file = file_mic
32
+ else:
33
+ file = file_upload
34
+
35
  input_values = process_audio_file(file)
36
 
37
+ sequences = model.generate(input_values, forced_bos_token_id=forced_bos_token_id, num_beams=1, max_length=30))
38
 
39
  transcription = tokenizer.batch_decode(sequences, skip_special_tokens=True)
40
+ return warn_output + transcription[0]
41
 
42
  target_language = [
43
  "English (en)",
80
  iface = gr.Interface(
81
  fn=transcribe,
82
  inputs=[
83
+ gr.inputs.Audio(source="microphone", type='filepath', optional=True),
84
+ gr.inputs.Audio(source="upload", type='filepath', optional=True),
85
  gr.inputs.Dropdown(target_language),
86
  ],
87
  outputs="text",