Spaces:
Sleeping
Sleeping
File size: 2,660 Bytes
8d7b4b1 | 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | from pathlib import Path
from typing import Dict, Sequence
import numpy as np
from sentence_transformers import SentenceTransformer
from pathlib import Path
from typing import Optional
from src.features.preprocessing import FeaturePreprocessor, PCATransform
import logging
logger = logging.getLogger(__name__)
from src.features.sanitization import sanitize_array
class TextFeatureExtractor:
"""SentenceTransformer-based text embedding for transcripts.
Optionally applies a serialized `FeaturePreprocessor` (normalizer + PCA)
saved during training so the runtime embeddings match the model input
dimensionality (e.g. 768 -> 133).
"""
def __init__(self, model_name: str = "sentence-transformers/all-mpnet-base-v2", preprocessor_path: Optional[Path] = None):
self.model = SentenceTransformer(model_name)
self.preprocessor: Optional[FeaturePreprocessor] = None
if preprocessor_path is not None:
p = Path(preprocessor_path)
if p.exists():
try:
self.preprocessor = FeaturePreprocessor.load(p)
logger.info("Loaded FeaturePreprocessor from %s", str(p))
except Exception:
try:
# Try loading a raw PCA reducer if present
pca = PCATransform.load(p)
class _PCAWrap:
def __init__(self, pca):
self._pca = pca
def transform(self, x):
return self._pca.transform(x)
self.preprocessor = _PCAWrap(pca)
logger.info("Loaded PCATransform from %s", str(p))
except Exception:
logger.warning("Failed to load preprocessor from %s; continuing without it", str(p))
self.preprocessor = None
def encode(self, texts: Sequence[str]) -> np.ndarray:
embeddings = self.model.encode(list(texts), convert_to_numpy=True, show_progress_bar=False)
arr = np.asarray(embeddings, dtype=np.float32)
if self.preprocessor is not None:
try:
arr = self.preprocessor.transform(arr)
except Exception:
pass
return sanitize_array(arr)
def encode_text(self, transcript: str, chunks: Sequence[str]) -> Dict[str, np.ndarray]:
session_embedding = self.encode([transcript])[0]
chunk_embeddings = self.encode(chunks)
return {
"session": session_embedding,
"chunks": chunk_embeddings,
}
|