|
import gradio as gr |
|
import torch |
|
from transformers import pipeline, WhisperForConditionalGeneration, WhisperProcessor |
|
|
|
|
|
device = 0 if torch.cuda.is_available() else -1 |
|
|
|
|
|
model_id = "riteshkr/quantized-whisper-large-v3" |
|
model = WhisperForConditionalGeneration.from_pretrained(model_id) |
|
processor = WhisperProcessor.from_pretrained(model_id) |
|
|
|
|
|
forced_decoder_ids = processor.get_decoder_prompt_ids(language="english", task="transcribe") |
|
|
|
|
|
pipe = pipeline( |
|
"automatic-speech-recognition", |
|
model=model, |
|
tokenizer=processor.tokenizer, |
|
feature_extractor=processor.feature_extractor, |
|
device=device |
|
) |
|
|
|
|
|
def transcribe_speech(filepath): |
|
batch_size = 16 if torch.cuda.is_available() else 4 |
|
|
|
output = pipe( |
|
filepath, |
|
max_new_tokens=256, |
|
generate_kwargs={ |
|
"forced_decoder_ids": forced_decoder_ids, |
|
}, |
|
chunk_length_s=30, |
|
batch_size=batch_size, |
|
) |
|
return output["text"] |
|
|
|
|
|
mic_transcribe = gr.Interface( |
|
fn=transcribe_speech, |
|
inputs=gr.Audio(sources="microphone", type="filepath"), |
|
outputs=gr.Textbox(), |
|
) |
|
|
|
|
|
file_transcribe = gr.Interface( |
|
fn=transcribe_speech, |
|
inputs=gr.Audio(sources="upload", type="filepath"), |
|
outputs=gr.Textbox(), |
|
) |
|
|
|
|
|
demo = gr.Blocks() |
|
|
|
with demo: |
|
gr.TabbedInterface( |
|
[mic_transcribe, file_transcribe], |
|
["Transcribe Microphone", "Transcribe Audio File"], |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch(debug=True, share=True) |
|
|