File size: 723 Bytes
e6839e8
7fc5b77
e6839e8
 
 
7fc5b77
 
 
 
 
 
 
 
 
e6839e8
 
 
 
 
 
80c704e
 
 
 
 
 
e6839e8
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
from transformers import pipeline
import numpy as np

MODEL_NAME = "biodatlab/whisper-th-medium-combined"
DEVICE = 0 if torch.cuda.is_available() else "cpu"

transcriber = pipeline(
    "automatic-speech-recognition", 
    model=MODEL_NAME,
    chunk_length_s=30,
    device=DEVICE
)

def transcribe(audio):
    sr, y = audio
    y = y.astype(np.float32)
    y /= np.max(np.abs(y))

    return transcriber(
        {"sampling_rate": sr, "raw": y},
        generate_kwargs={"language":"<|th|>", "task":"transcribe"},
        return_timestamps=False,
        batch_size=16
    )["text"]


demo = gr.Interface(
    transcribe,
    gr.Audio(sources=["microphone"]),
    "text",
)

demo.launch()