shushukimura commited on
Commit
ca84573
·
1 Parent(s): a3653dc

commit files to HF hub

Browse files
Files changed (1) hide show
  1. asr_whisper_pipeline.py +15 -0
asr_whisper_pipeline.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq
2
+ import torch, torchaudio
3
+
4
+ def transcribe(audio_path):
5
+ model_name = "openai/whisper-tiny"
6
+ processor = AutoProcessor.from_pretrained(model_name)
7
+ model = AutoModelForSpeechSeq2Seq.from_pretrained(model_name)
8
+ speech, sr = torchaudio.load(audio_path)
9
+ if sr != 16000:
10
+ resampler = torchaudio.transforms.Resample(sr, 16000)
11
+ speech = resampler(speech)
12
+ inputs = processor(speech.squeeze(), sampling_rate=16000, return_tensors="pt")
13
+ with torch.no_grad():
14
+ ids = model.generate(**inputs)
15
+ return processor.batch_decode(ids, skip_special_tokens=True)[0]