Spaces:
Running
Running
File size: 1,290 Bytes
8366946 |
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 |
"""Base class for E5 model handling.
Downloads, caches, and loads the model for reuse.
"""
import logging
import os
# Import CACHE_PATH from your config
from src.modal_services.app_config import CACHE_PATH
# Define the model directory using the imported CACHE_PATH
E5_MODEL_DIR = f"{CACHE_PATH}/e5_model"
class E5ModelBase:
"""Base class for downloading and loading the E5 model."""
def setup_e5_model(self) -> None:
"""Downloads and loads the E5 embedding model."""
try:
# Lazy imports to avoid issues in Docker
from huggingface_hub import snapshot_download
from sentence_transformers import SentenceTransformer
# Cache E5 embedding model into /cache/e5_model
os.makedirs(E5_MODEL_DIR, exist_ok=True)
if not os.listdir(E5_MODEL_DIR):
snapshot_download("intfloat/e5-small-v2", local_dir=E5_MODEL_DIR)
logging.info("E5 model downloaded.")
self.vectorizer = SentenceTransformer(E5_MODEL_DIR, device="cuda")
logging.info("E5 model loaded on GPU.")
except Exception as e:
logging.error(f"[E5ModelBase] Failed to setup E5 model: {e}")
raise RuntimeError("[E5ModelBase] E5 model setup failed.") from e
|