Spaces:
Runtime error
Runtime error
Fix: Add get_embedding function and caching
Browse files5 unmodified lines678910111213141516171819202122231 unmodified line2526272829305 unmodified linesimport torchimport torch.nn.functional as Fdef get_embeddings(model_name: str, texts: List[str]) -> np.ndarray:"""Loads the specified model and generates embeddings for the given texts.Handles 'nomic' and 'qwen' specific requirements (trust_remote_code)."""print(f"Loading embedding model: {model_name}...")trust_remote_code = Falseif "nomic" in model_name or "qwen" in model_name:trust_remote_code = Truemodel = SentenceTransformer(model_name, trust_remote_code=trust_remote_code, device='cpu')# Generate embeddings# Convert to numpy array if it returns a tensor or list1 unmodified linereturn embeddingsdef mrl_slice(vectors: np.ndarray, dims: int) -> np.ndarray:"""Slices the vectors to the specified dimensions AND applies L2 normalization *after* slicing.5 unmodified lines678910111213141516171819202122232425261 unmodified line282930313233343536373839405 unmodified linesimport torchimport torch.nn.functional as F_MODEL_CACHE = {}def get_model(model_name: str):if model_name not in _MODEL_CACHE:print(f"Loading embedding model: {model_name}...")trust_remote_code = "nomic" in model_name or "qwen" in model_name_MODEL_CACHE[model_name] = SentenceTransformer(model_name, trust_remote_code=trust_remote_code, device='cpu')return _MODEL_CACHE[model_name]def get_embeddings(model_name: str, texts: List[str]) -> np.ndarray:"""Loads the specified model and generates embeddings for the given texts.Handles 'nomic' and 'qwen' specific requirements (trust_remote_code)."""model = get_model(model_name)# Generate embeddings# Convert to numpy array if it returns a tensor or list1 unmodified linereturn embeddingsdef get_embedding(text: str, model_name: str = "nomic-ai/nomic-embed-text-v1.5") -> np.ndarray:"""Generates a single embedding for a query string."""embeddings = get_embeddings(model_name, [text])return embeddings[0]def mrl_slice(vectors: np.ndarray, dims: int) -> np.ndarray:"""Slices the vectors to the specified dimensions AND applies L2 normalization *after* slicing.
src/data_pipeline.py+17-7