Views
No views yet
| Model | openai/whisper-medium | reachan/Cantonese-Whisper-Medium |
|---|---|---|
| WER | 0.94438 | 0.39383 |
| CER | 0.38508 | 0.06281 |
1import torch
2import torchaudio
3from transformers import WhisperProcessor, WhisperForConditionalGeneration
4import gradio as gr
5
6DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
7WHISPER_SAMPLE_RATE = 16000
8
9processor = WhisperProcessor.from_pretrained("openai/whisper-medium")
10model = WhisperForConditionalGeneration.from_pretrained(
11 "reachan/Cantonese-Whisper-Medium"
12).to(DEVICE)
13
14
15def preprocess_audio(audio_path: str) -> torch.Tensor:
16 audio, sample_rate = torchaudio.load(audio_path)
17 # Resample if necessary
18 if sample_rate != WHISPER_SAMPLE_RATE:
19 resampler = torchaudio.transforms.Resample(
20 orig_freq=sample_rate, new_freq=WHISPER_SAMPLE_RATE
21 )
22 audio = resampler(audio)
23 # Convert to mono
24 if audio.shape[0] > 1:
25 audio = torch.mean(audio, dim=0)
26 return audio.squeeze()
27
28
29def transcribe(audio_path: str) -> str:
30 audio_input = preprocess_audio(audio_path)
31 input_features = processor(
32 audio_input,
33 sampling_rate=WHISPER_SAMPLE_RATE,
34 return_tensors="pt",
35 language="Chinese",
36 ).input_features.to(DEVICE)
37
38 predicted_ids = model.generate(input_features)
39 transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
40 return transcription
41
42
43iface = gr.Interface(
44 fn=transcribe,
45 inputs=gr.Audio(type="filepath"),
46 outputs="text",
47 title="Cantonese Speech Recognition",
48)
49iface.launch()