File size: 1,860 Bytes
0b452e3
8f5169d
0b452e3
8f5169d
0b452e3
8f5169d
0b452e3
 
364aa46
8f5169d
0b452e3
8f5169d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b452e3
8f5169d
0b452e3
8f5169d
 
 
 
 
 
0b452e3
b867ef9
 
 
 
0b452e3
8f5169d
 
 
 
 
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
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()