llm / services /model_service.py
Chris4K's picture
Update services/model_service.py
5a1eeff verified
raw
history blame
1.89 kB
# services/model_service.py
from transformers import AutoTokenizer, AutoModelForCausalLM, LlamaConfig
from sentence_transformers import SentenceTransformer
import torch
from functools import lru_cache
from config.config import settings
import logging
logger = logging.getLogger(__name__)
class ModelService:
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._instance._initialized = False
return cls._instance
def __init__(self):
if not self._initialized:
self._initialized = True
self._load_models()
@lru_cache(maxsize=1)
def _load_models(self):
try:
self.tokenizer = AutoTokenizer.from_pretrained(settings.MODEL_NAME )
# Modify the model configuration to use a valid rope_scaling format
config = LlamaConfig.from_pretrained(settings.model_name)
if hasattr(config, "rope_scaling"):
config.rope_scaling = {
"type": "linear",
"factor": 32.0
}
# Load model with updated configuration
#self.model = AutoModelForCausalLM.from_pretrained(model_name, config=config).to(device)
self.model = AutoModelForCausalLM.from_pretrained(
settings.MODEL_NAME,
torch_dtype=torch.float16 if settings.DEVICE == "cuda" else torch.float32,
device_map="auto" if settings.DEVICE == "cuda" else None,
config=config
)
self.embedder = SentenceTransformer(settings.EMBEDDER_MODEL)
except Exception as e:
logger.error(f"Error loading models: {e}")
raise
def get_models(self):
return self.tokenizer, self.model, self.embedder