CoolFace
Apppublic

patharanor/asr-th

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py57 linesDownload Raw Back to root
1import gradio as gr2import torch3import numpy as np4from transformers import pipeline5from utils.thai_word import ThaiWord6from pythainlp.tokenize import word_tokenize7from collections import deque8from copy import deepcopy9 10MODEL_NAME = "biodatlab/whisper-th-medium-combined"11DEVICE = 0 if torch.cuda.is_available() else "cpu"12thw = ThaiWord()13 14# stride_length_s is a tuple of the left and right stride length.15# With only 1 number, both sides get the same stride, by default16# the stride_length on one side is 1/6th of the chunk_length_s17transcriber = pipeline(18    "automatic-speech-recognition", 19    model=MODEL_NAME,20    chunk_length_s=30,21    device=DEVICE22)23 24def transcribe(audio):25    result = ''26    try:27        sr, y = audio28        y = y.astype(np.float32)29        y /= np.max(np.abs(y))30 31        text = transcriber(32            {"sampling_rate": sr, "raw": y},33            generate_kwargs={"language":"<|th|>", "task":"transcribe"},34            return_timestamps=False,35            batch_size=1636        )["text"]37 38        if text is not None:39            # pretty text40            tokens = word_tokenize(text, engine="attacut", join_broken_num=True)41            print(tokens)42            result = f'pretty: {thw.pretty(deque(deepcopy(tokens)))}\n\n original: {text}' 43        else:44            result = 'โปรดลองพูดอีกครั้ง'45    except Exception as e:46        result = f'ไม่สามารถแปลงข้อความเสียงได้ โปรดลองอีกครั้ง\n\nพบข้อผิดพลาด: {str(e)}'47 48    return result49 50 51demo = gr.Interface(52    transcribe,53    gr.Audio(sources=["microphone"]),54    "text",55)56 57demo.launch()