Views
No views yet
1
2import torch
3from transformers import WhisperForConditionalGeneration, WhisperProcessor, WhisperTokenizer,WhisperFeatureExtractor
4import soundfile as sf
5
6
7model="ARTPARK-IISc/whisper-small-vaani-tulu"
8
9# Load tokenizer and feature extractor individually
10feature_extractor = WhisperFeatureExtractor.from_pretrained(model)
11tokenizer = WhisperTokenizer.from_pretrained("openai/whisper-small", language="Kannada", task="transcribe")
12
13
14# Create the processor manually
15processor = WhisperProcessor(feature_extractor=feature_extractor, tokenizer=tokenizer)
16
17# Load and preprocess the audio file
18audio_file_path = "Sample_Audio.wav" # replace with your audio file path
19
20
21device = "cuda" if torch.cuda.is_available() else "cpu"
22
23# Load the processor and model
24model = WhisperForConditionalGeneration.from_pretrained(model).to(device)
25
26
27# load audio
28audio_data, sample_rate = sf.read(audio_file_path)
29# Ensure the audio is 16kHz (Whisper expects 16kHz audio)
30if sample_rate != 16000:
31 import torchaudio
32 resampler = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=16000)
33 audio_data = resampler(torch.tensor(audio_data).unsqueeze(0)).squeeze().numpy()
34
35
36# Use the processor to prepare the input features
37input_features = processor(audio_data, sampling_rate=16000, return_tensors="pt").input_features.to(device)
38
39# Generate transcription (disable gradient calculation during inference)
40with torch.no_grad():
41 predicted_ids = model.generate(input_features)
42
43# Decode the generated IDs into human-readable text
44transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
45
46print(transcription)
47
48
49
50