Spaces:
Sleeping
Sleeping
File size: 1,311 Bytes
d997e06 |
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 |
from typing import List
from transformers import AutoTokenizer, AutoModel
import torch
import os
import numpy as np
class EmbeddingsProcessor:
"""
Class for processing text to obtain embeddings using a transformer model.
"""
def __init__(self, model_name: str):
"""
Initialize the EmbeddingsProcessor with a pre-trained model.
Args:
model_name (str): The name of the pre-trained model to use for generating embeddings.
"""
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
self.model = AutoModel.from_pretrained(model_name).to('cpu') # Change 'cuda' to 'cpu'
def get_embeddings(self, texts: List[str]) -> np.ndarray:
"""
Generate embeddings for a list of texts.
Args:
texts (List[str]): A list of text strings for which to generate embeddings.
Returns:
np.ndarray: A NumPy array of embeddings for the provided texts.
"""
encoded_input = self.tokenizer(texts, padding=True, truncation=True, return_tensors="pt")
encoded_input = {k: v.to('cpu') for k, v in encoded_input.items()} # Ensure all tensors are on CPU
model_output = self.model(**encoded_input)
return model_output.last_hidden_state.mean(dim=1).detach().numpy() |