Use Model

import torch
import numpy as np
import re
import jiwer
import librosa
from transformers import WhisperProcessor, WhisperForConditionalGeneration


# TEXT NORMALIZATION
def normalize_text(text):
    text = text.lower()
    text = re.sub(r"[^\w\s]", "", text)
    text = " ".join(text.split())
    return text


# METRICS
def calculate_metrics(ref, hyp):
    ref_n = normalize_text(ref)
    hyp_n = normalize_text(hyp)

    return {
        "reference": ref_n,
        "hypothesis": hyp_n,
        "wer": jiwer.wer(ref_n, hyp_n),
        "cer": jiwer.cer(ref_n, hyp_n)
    }


# LOAD MODEL FROM HF
def load_whisper(model_name, device="cuda"):
    print(f"Loading Whisper model from HuggingFace: {model_name}")

    processor = WhisperProcessor.from_pretrained(model_name)
    model = WhisperForConditionalGeneration.from_pretrained(model_name)

    return processor, model.to(device).eval()


# TRANSCRIBE AUDIO
def transcribe_audio(audio_path, processor, model, device="cuda"):
    waveform, sr = librosa.load(audio_path, sr=16000)

    inputs = processor(
        waveform,
        sampling_rate=sr,
        return_tensors="pt"
    ).input_features.to(device)

    with torch.no_grad():
        predicted_ids = model.generate(inputs)

    transcription = processor.batch_decode(
        predicted_ids,
        skip_special_tokens=True
    )[0]

    return transcription


# MAIN
def main():
    # SET YOUR AUDIO FILE & REFERENCE TEXT HERE
    audio_path = ""  
    reference_text = ""  

    # 2️ YOUR MODEL NAME
    model_name = "AhmedZaky1/whisper-small-v1"

    device = "cuda" if torch.cuda.is_available() else "cpu"

    # Load model
    processor, model = load_whisper(model_name, device)

    # Transcribe
    print("\nTranscribing audio...")
    prediction = transcribe_audio(audio_path, processor, model, device)

    # Compute metrics
    metrics = calculate_metrics(reference_text, prediction)

    print("\n==============================")
    print("REFERENCE:", metrics["reference"])
    print("PREDICTION:", metrics["hypothesis"])
    print(f"WER: {metrics['wer'] * 100:.2f}%")
    print(f"CER: {metrics['cer'] * 100:.2f}%")
    print("==============================")


# Run script
if __name__ == "__main__":
    main()
Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support

Model tree for AhmedZaky1/whisper-small-v1

Finetuned
(13)
this model