| import torch |
| import gradio as gr |
| import whisper |
| import os |
|
|
| |
|
|
| |
| model = whisper.load_model("large-v2", device="cuda" if torch.cuda.is_available() else "cpu") |
|
|
| def transcribe(audio_file): |
| |
| audio_path = audio_file |
|
|
| |
| result = model.transcribe(audio_path, language="Mandarin") |
| text = result["text"] |
|
|
| |
| base_name = os.path.splitext(os.path.basename(audio_path))[0] |
| |
| transcript_file_path = f"txt/{base_name}_transcript.txt" |
|
|
| |
| with open(transcript_file_path, "w") as file: |
| file.write(text) |
|
|
| |
| return text, f"Transcription saved to {transcript_file_path}" |
|
|
| |
| with gr.Blocks(css=".container { max-width: 800px; margin: auto; } .gradio-app { background-color: #f0f0f0; } button { background-color: #4CAF50; color: white; }") as demo: |
| gr.Markdown("ASR 語音語料辨識修正工具") |
| with gr.Row(): |
| audio_input = gr.Audio(source="upload", type="filepath", label="上傳你的音檔") |
| submit_button = gr.Button("語音識別") |
| output_text = gr.TextArea(label="識別結果") |
| save_status = gr.Text(label="儲存結果") |
|
|
| submit_button.click(fn=transcribe, inputs=audio_input, outputs=[output_text, save_status]) |
|
|
| demo.launch() |