Views
No views yet
| Step | Training Loss | Validation Loss | WER (%) |
|---|---|---|---|
| 250 | 0.120300 | 0.711645 | 37.0963 |
| 500 | 0.014100 | 0.770001 | 33.0668 |
| 750 | 0.001000 | 0.792062 | 32.2354 |
| 1000 | 0.000700 | 0.803786 | 32.0754 |
| ------ | --------------- | ----------------- | --------- |
| Model | WER (%) | Improvement |
|---|---|---|
| Original Whisper-Small (OpenAI) | 103.10 | - |
| Fine-tuned Whisper-Small (Swahili) | 32.07 | 68% improvement (dropped by 70) |
1import torch
2import torchaudio
3from transformers import WhisperProcessor, WhisperForConditionalGeneration
4
5# --- 1️⃣ Load processor (shared between models) ---
6processor = WhisperProcessor.from_pretrained("openai/whisper-small")
7
8# --- 2️⃣ Load models ---
9# Fine-tuned Swahili Whisper-Small
10finetuned_model_path = "./Ex02/whisper-small-swh/checkpoint-1000"
11finetuned_model = WhisperForConditionalGeneration.from_pretrained(finetuned_model_path)
12finetuned_model.generation_config.language = "swahili"
13finetuned_model.generation_config.task = "transcribe"
14finetuned_model.eval()
15
16# Original OpenAI Whisper-Small
17original_model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small")
18original_model.generation_config.language = "swahili"
19original_model.generation_config.task = "transcribe"
20original_model.eval()
21
22# --- 3️⃣ Load audio file ---
23audio_path = "./Ex02/Recording.wav"
24waveform, sample_rate = torchaudio.load(audio_path)
25
26# Resample if not 16kHz
27if sample_rate != 16000:
28 resampler = torchaudio.transforms.Resample(sample_rate, 16000)
29 waveform = resampler(waveform)
30
31# Prepare input features
32input_features = processor(
33 waveform.numpy()[0],
34 sampling_rate=16000,
35 return_tensors="pt"
36).input_features
37
38# --- 4️⃣ Transcribe ---
39with torch.no_grad():
40 # Fine-tuned
41 predicted_ids_finetuned = finetuned_model.generate(input_features)
42 transcription_finetuned = processor.batch_decode(predicted_ids_finetuned, skip_special_tokens=True)[0]
43
44 # Original
45 predicted_ids_original = original_model.generate(input_features)
46 transcription_original = processor.batch_decode(predicted_ids_original, skip_special_tokens=True)[0]
47
48# --- 5️⃣ Print results ---
49print("\n=== Transcriptions ===")
50print(f"Fine-tuned Swahili Whisper-Small: {transcription_finetuned}")
51print(f"Original Whisper-Small (OpenAI): {transcription_original}")
52
53
54## How to Use
55
56```python
57import torch
58from transformers import WhisperProcessor, WhisperForConditionalGeneration
59import torchaudio
60from huggingface_hub import login
61
62# Optional: login if model is private, i.e Error 401s
63login(token="hf_somekey")
64
65# ============================================================
66# 1️⃣ Load Processor and Quantized Model
67# ============================================================
68MODEL_ID = "adoamesh/whisper-tiny-swahili-distilled-8bit"
69
70processor = WhisperProcessor.from_pretrained(MODEL_ID)
71model = WhisperForConditionalGeneration.from_pretrained(MODEL_ID)
72model.eval()
73
74# Force CPU execution (quantized model works best on CPU)
75device = torch.device("cpu")
76model.to(device)
77
78# ============================================================
79# 2️⃣ Load and Preprocess Audio
80# ============================================================
81audio_path = "Recording.wav"
82waveform, sample_rate = torchaudio.load(audio_path)
83
84# Resample to 16 kHz if necessary
85if sample_rate != 16000:
86 resampler = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=16000)
87 waveform = resampler(waveform)
88
89# Prepare input features
90input_features = processor(
91 waveform.squeeze().numpy(), # remove channel dim if present
92 sampling_rate=16000,
93 return_tensors="pt"
94).input_features.to(device)
95
96# ============================================================
97# 3️⃣ Generate Transcription
98# ============================================================
99with torch.no_grad():
100 predicted_ids = model.generate(input_features)
101
102# Decode text
103transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
104print(f"\n🗣️ Transcription:\n{transcription}\n")
105@misc{whisper-tiny-swahili-distilled-8bit,
author = {Daniel Amemba Odhiambo},
title = {Whisper Tiny Swahili Distilled 8-bit},
year = {2025},
publisher = {Hugging Face},
journal = {Hugging Face Model Hub},
howpublished = {\url{https://huggingface.co/adoamesh/whisper-tiny-swahili-distilled-8bit}}
#### Fleurs-SLU for the Swahili speech data
@misc{schmidt2025fleursslumassivelymultilingualbenchmark,
title={Fleurs-SLU: A Massively Multilingual Benchmark for Spoken Language Understanding},
author={Fabian David Schmidt and Ivan Vulić and Goran Glavaš and David Ifeoluwa Adelani},
year={2025},
eprint={2501.06117},
archivePrefix={arXiv},
primaryClass={cs.CL},
url={https://arxiv.org/abs/2501.06117},
}
@misc{adelani2023sib200,
title={SIB-200: A Simple, Inclusive, and Big Evaluation Dataset for Topic Classification in 200+ Languages and Dialects},
author={David Ifeoluwa Adelani and Hannah Liu and Xiaoyu Shen and Nikita Vassilyev and Jesujoba O. Alabi and Yanke Mao and Haonan Gao and Annie En-Shiun Lee},
year={2023},
eprint={2309.07445},
archivePrefix={arXiv},
primaryClass={cs.CL}
}
}