Spaces:
Sleeping
Sleeping
File size: 2,090 Bytes
d7ae26e e59bf3f d7ae26e d8b3564 d7ae26e d8b3564 7ac8184 d7ae26e 7ac8184 d7ae26e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import gradio as gr
from transformers import Wav2Vec2ForCTC, AutoProcessor
import torch
import librosa
import json
import os
# with open('ISO_codes.json', 'r') as file:
# iso_codes = json.load(file)
languages = ["lug", "ach", "nyn", "teo"]
auth_token = os.environ.get("HF_TOKEN")
model_id = "Sunbird/sunbird-mms"
processor = AutoProcessor.from_pretrained(model_id, use_auth_token=True)
model = Wav2Vec2ForCTC.from_pretrained(model_id, use_auth_token=True)
def transcribe(audio_file_mic=None, audio_file_upload=None, language="Luganda (lug)"):
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"
# Make sure audio is 16kHz
speech, sample_rate = librosa.load(audio_file)
if sample_rate != 16000:
speech = librosa.resample(speech, orig_sr=sample_rate, target_sr=16000)
# 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
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
description = '''ASR with salt-mms'''
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="Luganda (eng)")
],
outputs=gr.Textbox(label="Transcription"),
examples=examples,
description=description
)
iface.launch() |