new structure for transcription engine

- found problems with mps
- integrated jinja2 templates for rendering (.tex?)
- raw ui & bad structure
This commit is contained in:
2026-02-24 02:58:28 +03:00
parent 9e67b36842
commit 3d48f473b0
24 changed files with 584 additions and 403 deletions
View File
+30
View File
@@ -0,0 +1,30 @@
import torch
from abc import ABC, abstractmethod
class BaseEngine(ABC):
def __init__(
self,
modelName: str,
language: str,
dType: torch.dtype,
device: str
):
self.modelName = modelName
self.device = device
self.language = language
self.dType = dType
@abstractmethod
def loadModel(self) -> None:
pass
@abstractmethod
def unloadModel(self) -> None:
pass
@abstractmethod
def transcribeBatch(
self,
batch
) -> str:
pass
+65
View File
@@ -0,0 +1,65 @@
# from logging import Logger
import time
import torch
import gc
from transformers import WhisperForConditionalGeneration, WhisperProcessor
from transcription.engines.base_engine import BaseEngine
class WhisperEngine(BaseEngine):
def loadModel(self) -> None:
self.processor = WhisperProcessor.from_pretrained(self.modelName)
self.model = WhisperForConditionalGeneration.from_pretrained(
self.modelName,
torch_dtype = self.dType # check twice
).to(self.device) # ??? recheck
def unloadModel(self) -> None:
self.model = None
self.processor = None
# TODO: MPS?
if self.device == "cuda":
torch.cuda.empty_cache()
def transcribeBatch(
self,
batch,
) -> str:
assert self.processor is not None
assert self.model is not None
inputs = self.processor(
batch,
sampling_rate=16000,
return_tensors="pt",
padding=True,
)
input_features = inputs.input_features.to(self.device).to(self.dType)
with torch.no_grad():
predicted_ids = self.model.generate(
input_features,
language=self.language,
task="transcribe",
temperature=0.0,
)
batchText = self.processor.batch_decode(
predicted_ids,
skip_special_tokens=True,
)
inputs = None
input_features = None
predicted_ids = None
gc.collect()
# maybe do here something with MPS?
if self.device.startswith("cuda"):
torch.cuda.empty_cache()
torch.cuda.ipc_collect()
return " ".join(batchText)