Spaces:
Build error
Build error
File size: 965 Bytes
e9947ad 9c32f1b e9947ad 9c32f1b e9947ad 31864ac 9c32f1b 31864ac 9c32f1b 31864ac 9c32f1b | 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 langchain_community.embeddings import HuggingFaceEmbeddings
from dotenv import load_dotenv
load_dotenv()
def build_embedding(model_name: str):
embedding = HuggingFaceEmbeddings(
model_name=model_name, # model_kwargs={"device": "cuda"}, \
encode_kwargs={"normalize_embeddings": True},
)
embedding.show_progress = True
return embedding
class EmbeddingBuilder:
def __init__(self, model_name: str, device: str = "cpu"):
self.model_name = model_name
self.device = device
def __enter__(self):
# Initialize resources
embedding = HuggingFaceEmbeddings(
model_name=self.model_name,
model_kwargs={"device": self.device},
encode_kwargs={"normalize_embeddings": True},
)
embedding.show_progress = True
return embedding
def __exit__(self, exc_type, exc_value, traceback):
# Cleanup resources if necessary
pass
|