import gradio as gr from transformers import Wav2Vec2Model, AutoProcessor import torch import os import librosa import numpy as np model_id = "facebook/mms-1b-all" processor = AutoProcessor.from_pretrained(model_id) model = Wav2Vec2Model.from_pretrained(model_id) def load_audio_file(file_path, db=0): # Resample audio to 16kHz if the sample rate is different waveform, sample_rate = librosa.load(file_path) if len(waveform.shape) > 1 and waveform.shape[0] > 1: waveform = librosa.to_mono(waveform) if sample_rate != 16000: waveform = librosa.resample(waveform, orig_sr=sample_rate, target_sr=16000) waveform = torch.Tensor(waveform) return waveform def transcribe(audio_file_upload=None): file_path = audio_file_upload base_name = os.path.splitext(os.path.basename(file_path))[0] waveform = load_audio_file(file_path) input_values = processor(waveform, return_tensors="pt", sampling_rate=16000).input_values with torch.no_grad(): features = model(input_values).last_hidden_state features = features.squeeze().cpu().detach().numpy() # Save features to numpy file npy_file = f"{base_name}.npy" np.save(npy_file, features) return npy_file 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(type="filepath", label="Upload Audio"), outputs=gr.File(label="Download Features (npy)"), description=description) iface.launch()