|
import gradio as gr |
|
import torch |
|
import torchaudio |
|
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor |
|
|
|
|
|
model_name = "Futuresony/Future-sw_ASR-24-02-2025" |
|
processor = Wav2Vec2Processor.from_pretrained(model_name) |
|
model = Wav2Vec2ForCTC.from_pretrained(model_name) |
|
|
|
|
|
def transcribe_live(microphone_audio): |
|
speech_array, sample_rate = torchaudio.load(microphone_audio) |
|
|
|
|
|
resampler = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=16000) |
|
speech_array = resampler(speech_array).squeeze().numpy() |
|
|
|
|
|
input_values = processor(speech_array, sampling_rate=16000, return_tensors="pt").input_values |
|
with torch.no_grad(): |
|
logits = model(input_values).logits |
|
predicted_ids = torch.argmax(logits, dim=-1) |
|
|
|
|
|
transcription = processor.batch_decode(predicted_ids)[0] |
|
return transcription |
|
|
|
|
|
interface = gr.Interface( |
|
fn=transcribe_live, |
|
inputs=gr.Audio(source="microphone", type="filepath"), |
|
outputs="text", |
|
live=True, |
|
title="Live Swahili ASR Transcription", |
|
description="Speak into your microphone, and the model will transcribe in real-time.", |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
interface.launch() |
|
|