File size: 1,445 Bytes
8d8cf1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ef1377
8d8cf1e
781aee4
8d8cf1e
 
781aee4
4936f95
e7f6e90
8d8cf1e
 
 
6eef7ab
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
import gradio as gr
import librosa
from transformers import AutoFeatureExtractor, pipeline


def load_and_fix_data(input_file, model_sampling_rate):
    speech, sample_rate = librosa.load(input_file)
    if len(speech.shape) > 1:
        speech = speech[:, 0] + speech[:, 1]
    if sample_rate != model_sampling_rate:
        speech = librosa.resample(speech, sample_rate, model_sampling_rate)
    return speech


feature_extractor = AutoFeatureExtractor.from_pretrained(
    "anuragshas/wav2vec2-xls-r-1b-hi-with-lm"
)
sampling_rate = feature_extractor.sampling_rate

asr = pipeline(
    "automatic-speech-recognition", model="anuragshas/wav2vec2-xls-r-1b-hi-with-lm"
)


def predict_and_ctc_lm_decode(input_file):
    speech = load_and_fix_data(input_file, sampling_rate)
    transcribed_text = asr(speech, chunk_length_s=5, stride_length_s=1)
    return transcribed_text["text"]


gr.Interface(
    predict_and_ctc_lm_decode,
    inputs=[
        gr.inputs.Audio(source="microphone", type="filepath", label="Record your audio")
    ],
    outputs=[gr.outputs.Textbox()],
    examples=[["example1.wav"]],
    title="Hindi ASR using Wav2Vec2-1B with LM",
    article="<p><center><img src='https://visitor-badge.glitch.me/badge?page_id=anuragshas/Hindi_ASR' alt='visitor badge'></center></p>",
    description="Built during Robust Speech Event",
    layout="horizontal",
    theme="huggingface",
).launch(enable_queue=True, cache_examples=True)