Mohamed Aymane Farhi commited on
Commit
364aa46
1 Parent(s): 0b452e3

Add other languages.

Browse files
Files changed (2) hide show
  1. README.md +1 -1
  2. app.py +12 -6
README.md CHANGED
@@ -1,6 +1,6 @@
1
  ---
2
  title: MMS ASR
3
- emoji: 🏃
4
  colorFrom: green
5
  colorTo: pink
6
  sdk: gradio
 
1
  ---
2
  title: MMS ASR
3
+ emoji: 🎤
4
  colorFrom: green
5
  colorTo: pink
6
  sdk: gradio
app.py CHANGED
@@ -1,12 +1,13 @@
1
  import gradio as gr
2
  from transformers import Wav2Vec2ForCTC, AutoProcessor
3
  import torch
4
- import numpy as np
5
  import librosa
6
 
7
  model_id = "facebook/mms-1b-all"
 
 
8
 
9
- def transcribe(audio_file_mic=None, audio_file_upload=None):
10
  if audio_file_mic:
11
  audio_file = audio_file_mic
12
  elif audio_file_upload:
@@ -14,12 +15,14 @@ def transcribe(audio_file_mic=None, audio_file_upload=None):
14
  else:
15
  return "Please upload an audio file or record one"
16
 
 
17
  speech, sample_rate = librosa.load(audio_file)
18
  if sample_rate != 16000:
19
  speech = librosa.resample(speech, orig_sr=sample_rate, target_sr=16000)
20
 
21
- processor = AutoProcessor.from_pretrained(model_id)
22
- model = Wav2Vec2ForCTC.from_pretrained(model_id)
 
23
 
24
  inputs = processor(speech, sampling_rate=16_000, return_tensors="pt")
25
 
@@ -30,11 +33,14 @@ def transcribe(audio_file_mic=None, audio_file_upload=None):
30
  transcription = processor.decode(ids)
31
  return transcription
32
 
 
 
33
  iface = gr.Interface(fn=transcribe,
34
  inputs=[
35
  gr.Audio(source="microphone", type="filepath"),
36
- gr.Audio(source="upload", type="filepath")
 
37
  ],
38
- outputs=["textbox"],
39
  )
40
  iface.launch()
 
1
  import gradio as gr
2
  from transformers import Wav2Vec2ForCTC, AutoProcessor
3
  import torch
 
4
  import librosa
5
 
6
  model_id = "facebook/mms-1b-all"
7
+ processor = AutoProcessor.from_pretrained(model_id)
8
+ model = Wav2Vec2ForCTC.from_pretrained(model_id)
9
 
10
+ def transcribe(audio_file_mic=None, audio_file_upload=None, language="eng"):
11
  if audio_file_mic:
12
  audio_file = audio_file_mic
13
  elif audio_file_upload:
 
15
  else:
16
  return "Please upload an audio file or record one"
17
 
18
+ # Make sure audio is 16kHz mono WAV
19
  speech, sample_rate = librosa.load(audio_file)
20
  if sample_rate != 16000:
21
  speech = librosa.resample(speech, orig_sr=sample_rate, target_sr=16000)
22
 
23
+ # Keep the same model in memory and simply switch out the language adapters by calling load_adapter() for the model and set_target_lang() for the tokenizer
24
+ processor.tokenizer.set_target_lang(language)
25
+ model.load_adapter(language)
26
 
27
  inputs = processor(speech, sampling_rate=16_000, return_tensors="pt")
28
 
 
33
  transcription = processor.decode(ids)
34
  return transcription
35
 
36
+ languages = list(processor.tokenizer.vocab.keys())
37
+
38
  iface = gr.Interface(fn=transcribe,
39
  inputs=[
40
  gr.Audio(source="microphone", type="filepath"),
41
+ gr.Audio(source="upload", type="filepath"),
42
+ gr.Dropdown(choices=languages, label="Language")
43
  ],
44
+ outputs=["textbox"]
45
  )
46
  iface.launch()