Akwbw commited on
Commit
d79b34d
·
verified ·
1 Parent(s): 4182f30

Create api.py

Browse files
Files changed (1) hide show
  1. api.py +17 -0
api.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import WhisperProcessor, WhisperForConditionalGeneration
2
+ import torch
3
+ import soundfile as sf
4
+
5
+ class UrduWhisper:
6
+ def __init__(self):
7
+ print("Loading Urdu Whisper Tiny model...")
8
+ self.processor = WhisperProcessor.from_pretrained("kingabzpro/whisper-tiny-urdu")
9
+ self.model = WhisperForConditionalGeneration.from_pretrained("kingabzpro/whisper-tiny-urdu")
10
+ self.model.to("cpu")
11
+
12
+ def transcribe(self, audio_file):
13
+ audio, sr = sf.read(audio_file)
14
+ inputs = self.processor(audio, sampling_rate=sr, return_tensors="pt")
15
+ with torch.no_grad():
16
+ predicted_ids = self.model.generate(inputs["input_features"])
17
+ return self.processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]