Views
No views yet
openai/whisper-medium1import torch
2import torchaudio
3from transformers import WhisperForConditionalGeneration, WhisperProcessor
4
5# Load the fine-tuned model and processor
6model = WhisperForConditionalGeneration.from_pretrained("ghayth123/tunisian-asr-whisper_medium/checkpoint")
7processor = WhisperProcessor.from_pretrained("openai/whisper-medium")
8model.eval()
9
10# Load audio
11audio_path = "example.wav" # Replace with your audio file
12waveform, sample_rate = torchaudio.load(audio_path)
13
14# Resample to 16kHz if necessary
15if sample_rate != 16000:
16 waveform = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=16000)(waveform)
17
18# Convert to numpy and mono if needed
19speech = waveform[0].numpy()
20if waveform.shape[0] > 1:
21 speech = waveform.mean(dim=0).numpy()
22
23# Prepare input and run inference
24inputs = processor(speech, sampling_rate=16000, return_tensors="pt")
25with torch.no_grad():
26 predicted_ids = model.generate(inputs["input_features"])
27
28# Decode and print the transcription
29transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
30print("🎧 Transcription:", transcription)