File size: 1,156 Bytes
9232ed6
 
 
 
84f0996
9232ed6
 
 
 
 
 
 
 
 
 
 
 
84f0996
9232ed6
 
84f0996
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9232ed6
 
84f0996
 
 
36a83db
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
import gradio as gr
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
import torch

model_id = "distil-whisper/distil-large-v3"

device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32

model = AutoModelForSpeechSeq2Seq.from_pretrained(
    model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
)
model.to(device)

torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
processor = AutoProcessor.from_pretrained(model_id)



def transcribe_audio(audio_file):
    pipe = pipeline(
        "automatic-speech-recognition",
        model=model,
        tokenizer=processor.tokenizer,
        feature_extractor=processor.feature_extractor,
        max_new_tokens=128,
        torch_dtype=torch_dtype,
        device=device,
    )
    results = pipe(audio_file)
    return results["text"]

inputs = [
    gr.Audio(sources="upload", type="filepath"),
]
outputs = gr.Textbox()

interface = gr.Interface(
    fn=transcribe_audio, inputs=inputs, outputs=outputs, title="Audio Transcription App"
)
interface.launch()