Spaces:
Paused
Paused
Download modules/transcription/trocr_model.py from SamilG2/BACKEND-CheckScan: direct link, hf CLI and curl.
- Browser
- Download file 3.49 kB
-
https://huggingface.co/spaces/SamilG2/BACKEND-CheckScan/resolve/main/modules/transcription/trocr_model.py
- Command line
-
hf download hf://spaces/SamilG2/BACKEND-CheckScan/modules/transcription/trocr_model.py
-
curl -L -o trocr_model.py https://huggingface.co/spaces/SamilG2/BACKEND-CheckScan/resolve/main/modules/transcription/trocr_model.py
3.49 kB
| """Inferencia TrOCR optimizada para CPU (Intel Iris Xe y similares). | |
| Mejoras vs versión original: | |
| - Inferencia en BATCH: una sola pasada por N celdas en lugar de N pasadas. | |
| - Cuantización INT8 dinámica en CPU (3-4x más rápido, <1% pérdida de accuracy). | |
| - Greedy decoding (num_beams=1, do_sample=False) → mucho menos cómputo que beam search. | |
| En GPU (CUDA) se desactiva la cuantización automáticamente, ya que solo aporta en CPU. | |
| """ | |
| import os | |
| import numpy as np | |
| import torch | |
| from PIL import Image | |
| from transformers import TrOCRProcessor, VisionEncoderDecoderModel | |
| _processor = None | |
| _model = None | |
| _device = None | |
| _FALLBACK_MODEL = "gcruzl3/trocr-checkscan" | |
| def _should_quantize() -> bool: | |
| from config import TROCR_QUANTIZE | |
| if TROCR_QUANTIZE == "true": | |
| return True | |
| if TROCR_QUANTIZE == "false": | |
| return False | |
| # "auto" → cuantizar solo en CPU | |
| return not torch.cuda.is_available() | |
| def _load(): | |
| global _processor, _model, _device | |
| if _model is not None: | |
| return | |
| from config import TROCR_MODEL_PATH | |
| _device = "cuda" if torch.cuda.is_available() else "cpu" | |
| model_id = TROCR_MODEL_PATH if os.path.isdir(TROCR_MODEL_PATH) else _FALLBACK_MODEL | |
| print(f"[TrOCR] Cargando modelo '{model_id}' en {_device}...") | |
| _processor = TrOCRProcessor.from_pretrained(model_id) | |
| model = VisionEncoderDecoderModel.from_pretrained(model_id) | |
| model.eval() | |
| if _device == "cpu" and _should_quantize(): | |
| print("[TrOCR] Aplicando cuantización INT8 dinámica (Linear layers)...") | |
| model = torch.quantization.quantize_dynamic( | |
| model, {torch.nn.Linear}, dtype=torch.qint8 | |
| ) | |
| # Threads de CPU: usa todos los cores físicos. | |
| try: | |
| # os.cpu_count() en contenedores devuelve el total del host, no los vCPUs asignados | |
| container_cpus = int(os.getenv("TROCR_NUM_THREADS", "0")) or min(os.cpu_count() or 4, 8) | |
| torch.set_num_threads(max(1, container_cpus - 1)) | |
| except Exception: | |
| pass | |
| print(f"[TrOCR] Cuantizado. Threads CPU = {torch.get_num_threads()}") | |
| _model = model.to(_device) | |
| print("[TrOCR] Modelo listo.") | |
| def _to_pil_rgb(cell_gray: np.ndarray) -> Image.Image: | |
| return Image.fromarray(cell_gray).convert("RGB") | |
| def transcribe_batch(cells: list[np.ndarray], batch_size: int | None = None) -> list[str]: | |
| """Transcribe una lista de celdas preprocesadas en batches. | |
| Args: | |
| cells: lista de arrays gris 64x384 (salida de preprocess_for_trocr). | |
| batch_size: tamaño de batch. None → usa config.TROCR_BATCH_SIZE. | |
| Returns: | |
| Lista de strings transcritos, mismo orden que `cells`. | |
| """ | |
| if not cells: | |
| return [] | |
| _load() | |
| from config import TROCR_BATCH_SIZE | |
| bs = batch_size or TROCR_BATCH_SIZE | |
| images = [_to_pil_rgb(c) for c in cells] | |
| results: list[str] = [] | |
| for i in range(0, len(images), bs): | |
| chunk = images[i:i + bs] | |
| pixel_values = _processor(chunk, return_tensors="pt").pixel_values.to(_device) | |
| with torch.no_grad(): | |
| ids = _model.generate(pixel_values, num_beams=1, do_sample=False) | |
| results.extend(t.strip() for t in _processor.batch_decode(ids, skip_special_tokens=True)) | |
| return results | |
| def transcribe_cell(cell_gray: np.ndarray) -> str: | |
| """Compat: transcribe una sola celda. Internamente usa el path de batch.""" | |
| return transcribe_batch([cell_gray], batch_size=1)[0] | |