Spaces:
Sleeping
Sleeping
File size: 2,228 Bytes
ae70001 | 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | """
models/loader.py
Centralized lazy model loader.
Models are loaded once on first use and cached in memory.
"""
import torch
import whisper
from transformers import (
RobertaTokenizer, RobertaModel,
ViTImageProcessor, ViTModel,
pipeline as hf_pipeline
)
_cache = {}
def get_device():
return 'cuda' if torch.cuda.is_available() else 'cpu'
# ββ RoBERTa ββββββββββββββββββββββββββββββββββββββββββββββ
def get_roberta():
if 'roberta' not in _cache:
print("[loader] Loading RoBERTa-large...")
tokenizer = RobertaTokenizer.from_pretrained('roberta-large')
model = RobertaModel.from_pretrained('roberta-large').to(get_device())
model.eval()
_cache['roberta'] = (tokenizer, model)
return _cache['roberta']
# ββ ViT ββββββββββββββββββββββββββββββββββββββββββββββββββ
def get_vit():
if 'vit' not in _cache:
print("[loader] Loading ViT-L/16...")
extractor = ViTImageProcessor.from_pretrained('google/vit-large-patch16-224')
model = ViTModel.from_pretrained('google/vit-large-patch16-224').to(get_device())
model.eval()
_cache['vit'] = (extractor, model)
return _cache['vit']
# ββ Whisper βββββββββββββββββββββββββββββββββββββββββββββββ
def get_whisper():
if 'whisper' not in _cache:
print("[loader] Loading Whisper base...")
_cache['whisper'] = whisper.load_model('base')
return _cache['whisper']
# ββ Sentiment pipeline ββββββββββββββββββββββββββββββββββββ
def get_sentiment():
if 'sentiment' not in _cache:
print("[loader] Loading sentiment pipeline...")
_cache['sentiment'] = hf_pipeline(
'sentiment-analysis',
model='cardiffnlp/twitter-roberta-base-sentiment-latest',
device=0 if get_device() == 'cuda' else -1
)
return _cache['sentiment']
|