Spaces:
Sleeping
Sleeping
Commit ·
4e6fa5a
1
Parent(s): ae07f4b
Use Google-recommended transformers commit for native LASR support
Browse files- backend/models/medasr.py +8 -23
- requirements.txt +1 -1
backend/models/medasr.py
CHANGED
|
@@ -2,12 +2,10 @@
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
-
import types
|
| 6 |
from datetime import datetime, timezone
|
| 7 |
from pathlib import Path
|
| 8 |
|
| 9 |
import librosa
|
| 10 |
-
import numpy as np
|
| 11 |
try:
|
| 12 |
import torch
|
| 13 |
from transformers import AutoProcessor, AutoModelForCTC
|
|
@@ -56,18 +54,6 @@ class MedASRModel:
|
|
| 56 |
|
| 57 |
try:
|
| 58 |
self._processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
| 59 |
-
|
| 60 |
-
# Monkey-patch: LasrFeatureExtractor._torch_extract_fbank_features
|
| 61 |
-
# expects (self, waveform, device='cpu') but upstream code passes
|
| 62 |
-
# extra positional args. Wrap it to accept and ignore them.
|
| 63 |
-
fe = getattr(self._processor, 'feature_extractor', self._processor)
|
| 64 |
-
if hasattr(fe, '_torch_extract_fbank_features'):
|
| 65 |
-
_orig = fe._torch_extract_fbank_features
|
| 66 |
-
def _patched(waveform, *args, **kwargs):
|
| 67 |
-
device_arg = kwargs.get('device', 'cpu')
|
| 68 |
-
return _orig(waveform, device=device_arg)
|
| 69 |
-
fe._torch_extract_fbank_features = _patched
|
| 70 |
-
|
| 71 |
self._model = AutoModelForCTC.from_pretrained(model_id, trust_remote_code=True)
|
| 72 |
self._model = self._model.to(device)
|
| 73 |
self._model.eval()
|
|
@@ -103,15 +89,14 @@ class MedASRModel:
|
|
| 103 |
inputs = inputs.to(self._device)
|
| 104 |
|
| 105 |
with torch.no_grad():
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
transcript_text = transcript_text.strip()
|
| 115 |
|
| 116 |
except Exception as exc:
|
| 117 |
raise ModelExecutionError(f"MedASR inference failed: {exc}") from exc
|
|
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
|
|
|
| 5 |
from datetime import datetime, timezone
|
| 6 |
from pathlib import Path
|
| 7 |
|
| 8 |
import librosa
|
|
|
|
| 9 |
try:
|
| 10 |
import torch
|
| 11 |
from transformers import AutoProcessor, AutoModelForCTC
|
|
|
|
| 54 |
|
| 55 |
try:
|
| 56 |
self._processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
self._model = AutoModelForCTC.from_pretrained(model_id, trust_remote_code=True)
|
| 58 |
self._model = self._model.to(device)
|
| 59 |
self._model.eval()
|
|
|
|
| 89 |
inputs = inputs.to(self._device)
|
| 90 |
|
| 91 |
with torch.no_grad():
|
| 92 |
+
outputs = self._model.generate(**inputs)
|
| 93 |
+
transcript_text = self._processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
| 94 |
+
|
| 95 |
+
# Clean up special tokens that may remain
|
| 96 |
+
import re
|
| 97 |
+
transcript_text = transcript_text.replace("<epsilon>", "")
|
| 98 |
+
transcript_text = transcript_text.replace("</s>", "").replace("<s>", "")
|
| 99 |
+
transcript_text = re.sub(r'\s+', ' ', transcript_text).strip()
|
|
|
|
| 100 |
|
| 101 |
except Exception as exc:
|
| 102 |
raise ModelExecutionError(f"MedASR inference failed: {exc}") from exc
|
requirements.txt
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
torch==2.4.1
|
| 2 |
-
transformers
|
| 3 |
bitsandbytes>=0.46.1
|
| 4 |
accelerate>=1.2.1
|
| 5 |
gradio>=5.10.0
|
|
|
|
| 1 |
torch==2.4.1
|
| 2 |
+
transformers @ git+https://github.com/huggingface/transformers.git@65dc261512cbdb1ee72b88ae5b222f2605aad8e5
|
| 3 |
bitsandbytes>=0.46.1
|
| 4 |
accelerate>=1.2.1
|
| 5 |
gradio>=5.10.0
|