File size: 1,252 Bytes
8f3666f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python3
"""Transcribe one Urdu call-audio file with PakUrdu-Conversational-ASR-r12.

Usage:
    python inference_example.py call.wav [more.wav ...]

Loads the public base model from the HF hub, applies this LoRA adapter,
merges, and decodes with the certified settings (language=ur, greedy).
"""
import sys

import soundfile as sf
import torch
from peft import PeftModel
from transformers import WhisperForConditionalGeneration, WhisperProcessor

BASE = "openai/whisper-large-v3-turbo"
ADAPTER = "KhiredNetworks/PakUrdu-Conversational-ASR"

if len(sys.argv) < 2:
    sys.exit(__doc__)

processor = WhisperProcessor.from_pretrained(BASE)
model = WhisperForConditionalGeneration.from_pretrained(BASE)
model = PeftModel.from_pretrained(model, ADAPTER).merge_and_unload()

for path in sys.argv[1:]:
    speech, sr = sf.read(path, dtype="float32")
    if speech.ndim > 1:
        speech = speech.mean(axis=1)
    inputs = processor(speech, sampling_rate=sr, return_tensors="pt")
    with torch.no_grad():
        ids = model.generate(inputs.input_features, language="ur",
                             task="transcribe", do_sample=False)
    text = processor.batch_decode(ids, skip_special_tokens=True)[0].strip()
    print(f"{path}: {text}")