patrickvonplaten commited on
Commit
c6dd8c8
1 Parent(s): aca2fbe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -5
app.py CHANGED
@@ -19,16 +19,28 @@ def process_audio_file(file):
19
  input_values = feature_extractor(data, return_tensors="pt").input_values.to(device)
20
  return input_values
21
 
22
- def transcribe(file, target_language):
 
23
  target_code = target_language.split("(")[-1].split(")")[0]
24
  forced_bos_token_id = MAPPING[target_code]
25
-
 
 
 
 
 
 
 
 
 
 
 
26
  input_values = process_audio_file(file)
27
 
28
- sequences = model.generate(input_values, forced_bos_token_id=forced_bos_token_id, num_beams=1, max_length=30)
29
 
30
  transcription = tokenizer.batch_decode(sequences, skip_special_tokens=True)
31
- return transcription[0]
32
 
33
  target_language = [
34
  "English (en)",
@@ -71,7 +83,8 @@ MAPPING = {
71
  iface = gr.Interface(
72
  fn=transcribe,
73
  inputs=[
74
- gr.inputs.Audio(source="microphone", type='filepath'),
 
75
  gr.inputs.Dropdown(target_language),
76
  ],
77
  outputs="text",
19
  input_values = feature_extractor(data, return_tensors="pt").input_values.to(device)
20
  return input_values
21
 
22
+ def transcribe(file_mic, file_upload, target_language):
23
+
24
  target_code = target_language.split("(")[-1].split(")")[0]
25
  forced_bos_token_id = MAPPING[target_code]
26
+
27
+ warn_output = ""
28
+ if (file_mic is not None) and (file_upload is not None):
29
+ 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"
30
+ file = file_mic
31
+ elif (file_mic is None) and (file_upload is None):
32
+ return "ERROR: You have to either use the microphone or upload an audio file"
33
+ elif file_mic is not None:
34
+ file = file_mic
35
+ else:
36
+ file = file_upload
37
+
38
  input_values = process_audio_file(file)
39
 
40
+ sequences = model.generate(input_values, forced_bos_token_id=forced_bos_token_id)
41
 
42
  transcription = tokenizer.batch_decode(sequences, skip_special_tokens=True)
43
+ return warn_output + transcription[0]
44
 
45
  target_language = [
46
  "English (en)",
83
  iface = gr.Interface(
84
  fn=transcribe,
85
  inputs=[
86
+ gr.inputs.Audio(source="microphone", type='filepath', optional=True),
87
+ gr.inputs.Audio(source="upload", type='filepath', optional=True),
88
  gr.inputs.Dropdown(target_language),
89
  ],
90
  outputs="text",