Update api.py
Browse files
api.py
CHANGED
|
@@ -1,18 +1,17 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
Response:
|
| 15 |
-
```json
|
| 16 |
-
{
|
| 17 |
-
"text": "urdu transcription here"
|
| 18 |
-
}
|
|
|
|
| 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]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|