File size: 1,457 Bytes
99ef49e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9476e08
 
 
 
 
 
 
 
99ef49e
bdb4270
99ef49e
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline
from librosa import resample
import numpy as np

def transcribe(input_audio, model_id):
    pipe = pipeline(
    "automatic-speech-recognition",
    model=model_id,
    device="cpu"
    )
    sr, speech = input_audio
    # Convert to mono if stereo
    if speech.ndim > 1:
        speech = speech.mean(axis=1)
    # Convert to float32 if needed
    if speech.dtype != "float32":
        speech = speech.astype(np.float32)
    # Resample if sampling rate is not 16kHz
    if sr!=16000:
        speech = resample(speech, orig_sr=sr, target_sr=16000)
    output = pipe(speech, chunk_length_s=30, stride_length_s=5)['text']
    return output

model_ids_list = [
 "GetmanY1/wav2vec2-base-fi-150k-finetuned",
 "GetmanY1/wav2vec2-large-fi-150k-finetuned",
 "GetmanY1/wav2vec2-xlarge-fi-150k-finetuned"
]

gradio_app = gr.Interface(
    fn=transcribe,
    inputs=[
        gr.Audio(sources=["upload","microphone"]),
        gr.Dropdown(
            label="Model",
            value="GetmanY1/wav2vec2-large-fi-150k-finetuned",
            choices=model_ids_list
        )
        ],
    outputs="text",
    title="Finnish Automatic Speech Recognition",
    description ="Choose a model from the list. Select the Base model for the fastest inference and the XLarge one for the most accurate results."
)

if __name__ == "__main__":
    gradio_app.launch()



# if __name__ == "__main__":
#     gradio_app.launch()