| import gradio as gr |
| import torch |
| import torchaudio |
| import subprocess |
| import os |
| import sys |
| from huggingface_hub import snapshot_download |
|
|
| |
| if not os.path.exists("chunkformer"): |
| os.system("git clone https://github.com/khanhld/chunkformer.git") |
|
|
| |
| snapshot_download( |
| repo_id="khanhld/chunkformer-large-vie", |
| local_dir="./chunkformer-large-vie", |
| local_dir_use_symlinks=False |
| ) |
|
|
| |
| sys.path.append("./chunkformer") |
|
|
| |
| class DummyPreprocModel: |
| def __call__(self, audio_tensor): |
| return audio_tensor * 0.5 |
|
|
| preproc_model = DummyPreprocModel() |
|
|
| |
| MODEL_CHECKPOINT = "./chunkformer-large-vie" |
|
|
| def process(audio_file, transcript, option): |
| waveform, sr = torchaudio.load(audio_file) |
|
|
| if option == "Không xử lý": |
| audio_in = audio_file |
| else: |
| |
| processed_waveform = preproc_model(waveform) |
| output_audio = "processed_audio.wav" |
| torchaudio.save(output_audio, processed_waveform, sr) |
| audio_in = output_audio |
|
|
| |
| output_txt = "asr_result.txt" |
|
|
| decode_cmd = [ |
| sys.executable, |
| "chunkformer/decode.py", |
| "--model_checkpoint", MODEL_CHECKPOINT, |
| "--long_form_audio", audio_in, |
| "--total_batch_duration", "1800", |
| "--chunk_size", "64", |
| "--left_context_size", "128", |
| "--right_context_size", "128" |
| ] |
|
|
| print("Running:", " ".join(decode_cmd)) |
| |
| with open(output_txt, "w") as f: |
| subprocess.run(decode_cmd, stdout=f, stderr=subprocess.STDOUT) |
|
|
| |
| with open(output_txt) as f: |
| transcript_out = f.read() |
|
|
| if option == "Không xử lý": |
| audio_out = None |
| else: |
| audio_out = audio_in |
|
|
| return audio_out, transcript_out |
|
|
|
|
| iface = gr.Interface( |
| fn=process, |
| inputs=[ |
| gr.Audio(type="filepath", label="Tải file audio"), |
| gr.Textbox(label="Transcript gốc"), |
| gr.Radio(choices=["Không xử lý", "Có xử lý"], label="Chọn phương án"), |
| ], |
| outputs=[ |
| gr.Audio(type="filepath", label="File audio đã xử lý (nếu có)"), |
| gr.Textbox(label="Transcript ASR"), |
| ], |
| title="Demo ASR & Tiền Xử Lý (Chunkformer)", |
| description="Tải file audio, chọn tiền xử lý hoặc không, chạy Chunkformer." |
| ) |
|
|
| if __name__ == "__main__": |
| iface.launch() |
|
|