AhmedZaky1 commited on
Commit
b29c4ed
·
verified ·
1 Parent(s): 1c624f7

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +95 -4
README.md CHANGED
@@ -11,10 +11,101 @@ language:
11
  - en
12
  ---
13
 
14
- # Uploaded model
15
 
16
- - **Developed by:** AhmedZaky1
17
- - **License:** apache-2.0
18
- - **Finetuned from model :** unsloth/whisper-small
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
 
11
  - en
12
  ---
13
 
14
+ # Use Model
15
 
 
 
 
16
 
17
+ ```
18
+ import torch
19
+ import numpy as np
20
+ import re
21
+ import jiwer
22
+ import librosa
23
+ from transformers import WhisperProcessor, WhisperForConditionalGeneration
24
+
25
+
26
+ # TEXT NORMALIZATION
27
+ def normalize_text(text):
28
+ text = text.lower()
29
+ text = re.sub(r"[^\w\s]", "", text)
30
+ text = " ".join(text.split())
31
+ return text
32
+
33
+
34
+ # METRICS
35
+ def calculate_metrics(ref, hyp):
36
+ ref_n = normalize_text(ref)
37
+ hyp_n = normalize_text(hyp)
38
+
39
+ return {
40
+ "reference": ref_n,
41
+ "hypothesis": hyp_n,
42
+ "wer": jiwer.wer(ref_n, hyp_n),
43
+ "cer": jiwer.cer(ref_n, hyp_n)
44
+ }
45
+
46
+
47
+ # LOAD MODEL FROM HF
48
+ def load_whisper(model_name, device="cuda"):
49
+ print(f"Loading Whisper model from HuggingFace: {model_name}")
50
+
51
+ processor = WhisperProcessor.from_pretrained(model_name)
52
+ model = WhisperForConditionalGeneration.from_pretrained(model_name)
53
+
54
+ return processor, model.to(device).eval()
55
+
56
+
57
+ # TRANSCRIBE AUDIO
58
+ def transcribe_audio(audio_path, processor, model, device="cuda"):
59
+ waveform, sr = librosa.load(audio_path, sr=16000)
60
+
61
+ inputs = processor(
62
+ waveform,
63
+ sampling_rate=sr,
64
+ return_tensors="pt"
65
+ ).input_features.to(device)
66
+
67
+ with torch.no_grad():
68
+ predicted_ids = model.generate(inputs)
69
+
70
+ transcription = processor.batch_decode(
71
+ predicted_ids,
72
+ skip_special_tokens=True
73
+ )[0]
74
+
75
+ return transcription
76
+
77
+
78
+ # MAIN
79
+ def main():
80
+ # SET YOUR AUDIO FILE & REFERENCE TEXT HERE
81
+ audio_path = ""
82
+ reference_text = ""
83
+
84
+ # 2️ YOUR MODEL NAME
85
+ model_name = "AhmedZaky1/whisper-small-v1"
86
+
87
+ device = "cuda" if torch.cuda.is_available() else "cpu"
88
+
89
+ # Load model
90
+ processor, model = load_whisper(model_name, device)
91
+
92
+ # Transcribe
93
+ print("\nTranscribing audio...")
94
+ prediction = transcribe_audio(audio_path, processor, model, device)
95
+
96
+ # Compute metrics
97
+ metrics = calculate_metrics(reference_text, prediction)
98
+
99
+ print("\n==============================")
100
+ print("REFERENCE:", metrics["reference"])
101
+ print("PREDICTION:", metrics["hypothesis"])
102
+ print(f"WER: {metrics['wer'] * 100:.2f}%")
103
+ print(f"CER: {metrics['cer'] * 100:.2f}%")
104
+ print("==============================")
105
+
106
+
107
+ # Run script
108
+ if __name__ == "__main__":
109
+ main()
110
+ ```
111