|
import gradio as gr |
|
import torch |
|
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline |
|
from datasets import load_dataset |
|
|
|
|
|
device = "cuda:0" if torch.cuda.is_available() else "cpu" |
|
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32 |
|
|
|
|
|
model_id = "ylacombe/whisper-large-v3-turbo" |
|
model = AutoModelForSpeechSeq2Seq.from_pretrained( |
|
model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True |
|
) |
|
model.to(device) |
|
processor = AutoProcessor.from_pretrained(model_id) |
|
|
|
|
|
pipe = pipeline( |
|
"automatic-speech-recognition", |
|
model=model, |
|
tokenizer=processor.tokenizer, |
|
feature_extractor=processor.feature_extractor, |
|
torch_dtype=torch_dtype, |
|
device=device, |
|
) |
|
|
|
def transcribe_audio(audio): |
|
|
|
audio_input = processor(audio, return_tensors="pt", sampling_rate=16000) |
|
audio_input = audio_input.to(device) |
|
|
|
|
|
result = pipe(audio_input) |
|
return result["text"] |
|
|
|
|
|
demo = gr.Interface( |
|
transcribe_audio, |
|
inputs=gr.Audio(source="upload", type="file"), |
|
outputs="text", |
|
title="Speech-to-Text Transcription", |
|
description="Upload an audio file to transcribe its content.", |
|
) |
|
|
|
|
|
demo.launch() |