Spaces:
Sleeping
Sleeping
urwebsiteaz-ux commited on
Commit Β·
375f155
1
Parent(s): 4c9ee07
app.py
CHANGED
|
@@ -25,17 +25,19 @@ processor = WhisperProcessor.from_pretrained(BASE_MODEL)
|
|
| 25 |
|
| 26 |
baseline_model = WhisperForConditionalGeneration.from_pretrained(
|
| 27 |
BASE_MODEL, torch_dtype=DTYPE).to(DEVICE).eval()
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
baseline_model.generation_config.no_repeat_ngram_size = 3
|
| 32 |
|
| 33 |
base_for_ft = WhisperForConditionalGeneration.from_pretrained(
|
| 34 |
BASE_MODEL, torch_dtype=DTYPE)
|
| 35 |
ft_model = PeftModel.from_pretrained(base_for_ft, ADAPTER_REPO).to(DEVICE).eval()
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
ft_model.generation_config.no_repeat_ngram_size = 3
|
| 40 |
print("Models ready.")
|
| 41 |
|
|
@@ -135,11 +137,25 @@ def transcribe_with(model, audio_tensor, num_beams: int):
|
|
| 135 |
feats = processor(audio_tensor.numpy(), sampling_rate=16000,
|
| 136 |
return_tensors="pt").input_features.to(DEVICE, dtype=DTYPE)
|
| 137 |
t0 = time.perf_counter()
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
dt = time.perf_counter() - t0
|
|
|
|
|
|
|
| 141 |
text = processor.batch_decode(ids.sequences, skip_special_tokens=True)[0].strip()
|
| 142 |
-
#
|
|
|
|
| 143 |
if hasattr(ids, "sequences_scores") and ids.sequences_scores is not None:
|
| 144 |
conf = float(torch.exp(ids.sequences_scores[0]).clamp(0, 1))
|
| 145 |
else:
|
|
|
|
| 25 |
|
| 26 |
baseline_model = WhisperForConditionalGeneration.from_pretrained(
|
| 27 |
BASE_MODEL, torch_dtype=DTYPE).to(DEVICE).eval()
|
| 28 |
+
# Configure generation_config only (not model.config) β avoids reconciliation
|
| 29 |
+
# warnings in transformers 4.47+ when both configs hold conflicting values.
|
| 30 |
+
baseline_model.generation_config.language = "de"
|
| 31 |
+
baseline_model.generation_config.task = "transcribe"
|
| 32 |
+
baseline_model.generation_config.forced_decoder_ids = None
|
| 33 |
baseline_model.generation_config.no_repeat_ngram_size = 3
|
| 34 |
|
| 35 |
base_for_ft = WhisperForConditionalGeneration.from_pretrained(
|
| 36 |
BASE_MODEL, torch_dtype=DTYPE)
|
| 37 |
ft_model = PeftModel.from_pretrained(base_for_ft, ADAPTER_REPO).to(DEVICE).eval()
|
| 38 |
+
ft_model.generation_config.language = "de"
|
| 39 |
+
ft_model.generation_config.task = "transcribe"
|
| 40 |
+
ft_model.generation_config.forced_decoder_ids = None
|
| 41 |
ft_model.generation_config.no_repeat_ngram_size = 3
|
| 42 |
print("Models ready.")
|
| 43 |
|
|
|
|
| 137 |
feats = processor(audio_tensor.numpy(), sampling_rate=16000,
|
| 138 |
return_tensors="pt").input_features.to(DEVICE, dtype=DTYPE)
|
| 139 |
t0 = time.perf_counter()
|
| 140 |
+
# ββ KEY FIX: pass input_features as a KEYWORD argument. ββββββββββββββββ
|
| 141 |
+
# PeftModelForSeq2SeqLM.generate() only accepts **kwargs (no positional
|
| 142 |
+
# args beyond self). Passing `feats` positionally raises:
|
| 143 |
+
# TypeError: generate() takes 1 positional argument but 2 were given
|
| 144 |
+
# WhisperForConditionalGeneration also accepts it as a keyword, so this
|
| 145 |
+
# call is correct for BOTH the bare baseline model and the PEFT wrapper.
|
| 146 |
+
ids = model.generate(
|
| 147 |
+
input_features=feats,
|
| 148 |
+
num_beams=num_beams,
|
| 149 |
+
max_new_tokens=225,
|
| 150 |
+
return_dict_in_generate=True,
|
| 151 |
+
output_scores=True,
|
| 152 |
+
)
|
| 153 |
dt = time.perf_counter() - t0
|
| 154 |
+
# ids is GenerateBeamEncoderDecoderOutput when return_dict_in_generate=True.
|
| 155 |
+
# .sequences holds the token-id tensor; decode it to text.
|
| 156 |
text = processor.batch_decode(ids.sequences, skip_special_tokens=True)[0].strip()
|
| 157 |
+
# Confidence proxy: exponentiate the beam score (sum of log-probs).
|
| 158 |
+
# sequences_scores is None for greedy/num_beams=1 β guard accordingly.
|
| 159 |
if hasattr(ids, "sequences_scores") and ids.sequences_scores is not None:
|
| 160 |
conf = float(torch.exp(ids.sequences_scores[0]).clamp(0, 1))
|
| 161 |
else:
|