greenintellect / app /services /ml_models.py
Tanxshh's picture
Deploy GreenIntellect Backend API with ML models and scraping
02cc7f6
Raw
History Blame Contribute Delete
1.19 kB
from sentence_transformers import SentenceTransformer
from transformers import pipeline
import torch
class MLModels:
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super(MLModels, cls).__new__(cls)
cls._instance.device = 'cuda' if torch.cuda.is_available() else 'cpu'
print(f"Loading models on {cls._instance.device}...")
# Load Sentence Transformer
cls._instance.st_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2', device=cls._instance.device)
# Load FinBERT for sentiment
cls._instance.finbert = pipeline("text-classification", model="yiyanghkust/finbert-tone", device=0 if cls._instance.device == 'cuda' else -1)
# Load ClimateBERT for ESG sentiment (optional, can be heavy)
# cls._instance.climatebert = pipeline("text-classification", model="climatebert/distilroberta-base-climate-sentiment", device=0 if cls._instance.device == 'cuda' else -1)
print("Models loaded successfully.")
return cls._instance
ml_models = MLModels()