Views
No views yet
1from transformers import WhisperProcessor, WhisperForConditionalGeneration
2
3processor = WhisperProcessor.from_pretrained("eddiegulay/Whisperer_Mozilla_Sw_2000")
4model = WhisperForConditionalGeneration.from_pretrained("eddiegulay/Whisperer_Mozilla_Sw_2000")
5forced_decoder_ids = processor.get_decoder_prompt_ids(language="swahili", task="transcribe")
6
7def transcribe(audio_path):
8 # Load the audio file
9 audio_input, sample_rate = torchaudio.load(audio_path)
10 target_sample_rate = 16000
11 audio_input = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=target_sample_rate)(audio_input)
12
13 # Preprocess the audio data
14 input_features = processor(audio_input[0], sampling_rate=target_sample_rate, return_tensors="pt").input_features
15
16 # generate token ids
17 predicted_ids = model.generate(input_features, forced_decoder_ids=forced_decoder_ids)
18
19 # Perform inference and transcribe
20 transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
21
22 return transcription
23
24
25
26transcribe('your_audio_file.mp3')