|
import librosa |
|
from transformers import Wav2Vec2ForCTC, AutoProcessor |
|
import torch |
|
|
|
ASR_SAMPLING_RATE = 16_000 |
|
|
|
MODEL_ID = "facebook/mms-1b-all" |
|
processor = AutoProcessor.from_pretrained(MODEL_ID) |
|
model = Wav2Vec2ForCTC.from_pretrained(MODEL_ID) |
|
|
|
def transcribe(audio_source=None, microphone=None, file_upload=None): |
|
audio_fp = file_upload if file_upload else microphone |
|
if audio_fp is None: |
|
return "ERROR: You have to either use the microphone or upload an audio file" |
|
|
|
audio_samples = librosa.load(audio_fp, sr=ASR_SAMPLING_RATE, mono=True)[0] |
|
|
|
|
|
processor.tokenizer.set_target_lang("fao") |
|
model.load_adapter("fao") |
|
|
|
inputs = processor(audio_samples, sampling_rate=ASR_SAMPLING_RATE, return_tensors="pt") |
|
|
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
model.to(device) |
|
inputs = inputs.to(device) |
|
|
|
with torch.no_grad(): |
|
outputs = model(**inputs).logits |
|
ids = torch.argmax(outputs, dim=-1)[0] |
|
transcription = processor.decode(ids) |
|
|
|
return transcription |
|
|