|
import gradio as gr |
|
from transformers import Wav2Vec2ForCTC, AutoProcessor |
|
import torch |
|
import librosa |
|
import json |
|
|
|
with open('ISO_codes.json', 'r') as file: |
|
iso_codes = json.load(file) |
|
|
|
languages = list(iso_codes.keys()) |
|
|
|
model_id = "facebook/mms-1b-all" |
|
processor = AutoProcessor.from_pretrained(model_id) |
|
model = Wav2Vec2ForCTC.from_pretrained(model_id) |
|
|
|
def transcribe(audio_file_mic=None, audio_file_upload=None, language="English (eng)"): |
|
if audio_file_mic: |
|
audio_file = audio_file_mic |
|
elif audio_file_upload: |
|
audio_file = audio_file_upload |
|
else: |
|
return "Please upload an audio file or record one" |
|
|
|
|
|
speech, sample_rate = librosa.load(audio_file) |
|
if sample_rate != 16000: |
|
speech = librosa.resample(speech, orig_sr=sample_rate, target_sr=16000) |
|
|
|
|
|
language_code = iso_codes[language] |
|
processor.tokenizer.set_target_lang(language_code) |
|
model.load_adapter(language_code) |
|
|
|
inputs = processor(speech, sampling_rate=16_000, return_tensors="pt") |
|
|
|
with torch.no_grad(): |
|
outputs = model(**inputs).logits |
|
|
|
ids = torch.argmax(outputs, dim=-1)[0] |
|
transcription = processor.decode(ids) |
|
return transcription |
|
|
|
examples = [["kab_1.mp3", None, "Amazigh (kab)"], |
|
["kab_2.mp3", None, "Amazigh (kab)"]] |
|
|
|
description = '''Automatic Speech Recognition with [MMS](https://ai.facebook.com/blog/multilingual-model-speech-recognition/) (Massively Multilingual Speech) by Meta. |
|
Supports [1162 languages](https://dl.fbaipublicfiles.com/mms/misc/language_coverage_mms.html). Read the paper for more details: [Scaling Speech Technology to 1,000+ Languages](https://arxiv.org/abs/2305.13516).''' |
|
|
|
iface = gr.Interface(fn=transcribe, |
|
inputs=[ |
|
gr.Audio(source="microphone", type="filepath", label="Record Audio"), |
|
gr.Audio(source="upload", type="filepath", label="Upload Audio"), |
|
gr.Dropdown(choices=languages, label="Language", value="English (eng)") |
|
], |
|
outputs=gr.Textbox(label="Transcription"), |
|
examples=examples, |
|
description=description |
|
) |
|
iface.launch() |