File size: 1,434 Bytes
7b0dc9d
104123a
7b0dc9d
b51dfa4
a9a94f3
b51dfa4
b7d0d3d
 
 
 
 
 
 
 
 
 
 
b51dfa4
104123a
 
 
 
 
 
 
b51dfa4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7d0d3d
104123a
b51dfa4
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
import torch
import gradio as gr
from transformers import pipeline
from scipy.io import wavfile

MODEL_NAME = "openai/whisper-large-v3"
BATCH_SIZE = 8

device = 0 if torch.cuda.is_available() else "cpu"

pipe = pipeline(
    task="automatic-speech-recognition",
    model=MODEL_NAME,
    chunk_length_s=30,
    device=device,
)

def transcribe_simple(inputs_path, task):
    if inputs_path is None:
        raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
    
    sampling_rate, inputs = wavfile.read(inputs_path) 
    out = pipe(inputs_path, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)
    text = out["text"]
    
    return [[transcript] for transcript in text.split(".") if transcript], text

with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            audio_input = gr.Audio(source="upload", type="filepath", label="Upload Audio")
            task_input = gr.Dropdown(choices=["transcribe", "translate"], value="transcribe", label="Task")
            submit_button = gr.Button("Transcribe")
        with gr.Column():
            output_text = gr.Dataframe(label="Transcripts")
            output_full_text = gr.Textbox(label="Full Text")
    
    submit_button.click(
        transcribe_simple,
        inputs=[audio_input, task_input],
        outputs=[output_text, output_full_text],
    )

demo.launch()