Spaces:
Sleeping
Sleeping
Update models.py
Browse files
models.py
CHANGED
|
@@ -9,49 +9,59 @@ from transformers import CLIPProcessor, CLIPModel
|
|
| 9 |
from gpt4all import GPT4All
|
| 10 |
from config import MODELS_CONFIG, DEPLOYMENT_MODE
|
| 11 |
from device_setup import setup_device
|
| 12 |
-
|
| 13 |
@st.cache_resource
|
| 14 |
def load_models():
|
| 15 |
-
'''
|
| 16 |
-
Load all required models for multimodal processing
|
| 17 |
-
Returns: text_encoder, clip_model, clip_processor, whisper_model, ocr_reader, llm, device, has_gpu
|
| 18 |
-
'''
|
| 19 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
# Setup device
|
| 21 |
device, has_gpu = setup_device()
|
|
|
|
| 22 |
|
| 23 |
-
# Progress tracking for better UX
|
| 24 |
progress = st.progress(0, text="Initializing models...")
|
| 25 |
|
| 26 |
-
# Text
|
| 27 |
progress.progress(0.1, text="Loading text encoder...")
|
| 28 |
st.write("π Loading text encoder...")
|
| 29 |
-
text_encoder = SentenceTransformer(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
# CLIP
|
| 32 |
progress.progress(0.3, text="Loading CLIP model...")
|
| 33 |
st.write("πΌοΈ Loading CLIP model...")
|
| 34 |
clip_model_name = MODELS_CONFIG["clip_model"]
|
| 35 |
-
clip_model = CLIPModel.from_pretrained(clip_model_name).to(device)
|
| 36 |
-
clip_processor = CLIPProcessor.from_pretrained(clip_model_name)
|
| 37 |
|
| 38 |
-
# Whisper
|
| 39 |
progress.progress(0.5, text="Loading Whisper...")
|
| 40 |
st.write("π€ Loading Whisper model...")
|
| 41 |
whisper_size = MODELS_CONFIG["whisper_base"]
|
| 42 |
-
whisper_model = whisper.load_model(whisper_size, device=
|
| 43 |
|
| 44 |
-
#
|
| 45 |
progress.progress(0.7, text="Loading OCR...")
|
| 46 |
st.write("π Loading OCR reader...")
|
| 47 |
-
ocr_reader = easyocr.Reader(['en'], gpu=has_gpu)
|
| 48 |
|
| 49 |
-
# Local LLM
|
| 50 |
progress.progress(0.9, text="Loading LLM...")
|
| 51 |
st.write("π§ Loading local LLM...")
|
| 52 |
-
|
| 53 |
try:
|
| 54 |
-
# Try to load the model
|
| 55 |
llm = GPT4All(MODELS_CONFIG["llm_model"], allow_download=True)
|
| 56 |
st.write("β
LLM loaded successfully!")
|
| 57 |
except Exception as llm_error:
|
|
@@ -61,7 +71,6 @@ def load_models():
|
|
| 61 |
|
| 62 |
progress.progress(1.0, text="All models loaded!")
|
| 63 |
st.success("π All models loaded successfully!")
|
| 64 |
-
|
| 65 |
return text_encoder, clip_model, clip_processor, whisper_model, ocr_reader, llm, device, has_gpu
|
| 66 |
|
| 67 |
except Exception as e:
|
|
|
|
| 9 |
from gpt4all import GPT4All
|
| 10 |
from config import MODELS_CONFIG, DEPLOYMENT_MODE
|
| 11 |
from device_setup import setup_device
|
|
|
|
| 12 |
@st.cache_resource
|
| 13 |
def load_models():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
try:
|
| 15 |
+
# Writable caches first (prevent /.cache writes)
|
| 16 |
+
import pathlib
|
| 17 |
+
cache_root = os.environ.get("HF_HOME", "/tmp/hf")
|
| 18 |
+
pathlib.Path(cache_root).mkdir(parents=True, exist_ok=True)
|
| 19 |
+
|
| 20 |
+
# Make sure all relevant env vars point to the same place
|
| 21 |
+
os.environ.setdefault("HF_HOME", cache_root)
|
| 22 |
+
os.environ.setdefault("TRANSFORMERS_CACHE", cache_root)
|
| 23 |
+
os.environ.setdefault("HUGGINGFACE_HUB_CACHE", cache_root)
|
| 24 |
+
os.environ.setdefault("HF_DATASETS_CACHE", cache_root)
|
| 25 |
+
os.environ.setdefault("SENTENCE_TRANSFORMERS_HOME", cache_root)
|
| 26 |
+
os.environ.setdefault("XDG_CACHE_HOME", "/tmp")
|
| 27 |
+
|
| 28 |
# Setup device
|
| 29 |
device, has_gpu = setup_device()
|
| 30 |
+
device_str = "cuda" if has_gpu else "cpu"
|
| 31 |
|
|
|
|
| 32 |
progress = st.progress(0, text="Initializing models...")
|
| 33 |
|
| 34 |
+
# Text embeddings (explicit cache folder)
|
| 35 |
progress.progress(0.1, text="Loading text encoder...")
|
| 36 |
st.write("π Loading text encoder...")
|
| 37 |
+
text_encoder = SentenceTransformer(
|
| 38 |
+
MODELS_CONFIG["text_encoder"],
|
| 39 |
+
device=device,
|
| 40 |
+
cache_folder=cache_root
|
| 41 |
+
)
|
| 42 |
|
| 43 |
+
# CLIP (route Hugging Face cache explicitly)
|
| 44 |
progress.progress(0.3, text="Loading CLIP model...")
|
| 45 |
st.write("πΌοΈ Loading CLIP model...")
|
| 46 |
clip_model_name = MODELS_CONFIG["clip_model"]
|
| 47 |
+
clip_model = CLIPModel.from_pretrained(clip_model_name, cache_dir=cache_root).to(device)
|
| 48 |
+
clip_processor = CLIPProcessor.from_pretrained(clip_model_name, cache_dir=cache_root)
|
| 49 |
|
| 50 |
+
# Whisper (force CPU on free tier unless GPU present)
|
| 51 |
progress.progress(0.5, text="Loading Whisper...")
|
| 52 |
st.write("π€ Loading Whisper model...")
|
| 53 |
whisper_size = MODELS_CONFIG["whisper_base"]
|
| 54 |
+
whisper_model = whisper.load_model(whisper_size, device=device_str)
|
| 55 |
|
| 56 |
+
# EasyOCR (avoid CUDA probing on CPU Spaces)
|
| 57 |
progress.progress(0.7, text="Loading OCR...")
|
| 58 |
st.write("π Loading OCR reader...")
|
| 59 |
+
ocr_reader = easyocr.Reader(['en'], gpu=False if not has_gpu else True)
|
| 60 |
|
| 61 |
+
# Local LLM
|
| 62 |
progress.progress(0.9, text="Loading LLM...")
|
| 63 |
st.write("π§ Loading local LLM...")
|
|
|
|
| 64 |
try:
|
|
|
|
| 65 |
llm = GPT4All(MODELS_CONFIG["llm_model"], allow_download=True)
|
| 66 |
st.write("β
LLM loaded successfully!")
|
| 67 |
except Exception as llm_error:
|
|
|
|
| 71 |
|
| 72 |
progress.progress(1.0, text="All models loaded!")
|
| 73 |
st.success("π All models loaded successfully!")
|
|
|
|
| 74 |
return text_encoder, clip_model, clip_processor, whisper_model, ocr_reader, llm, device, has_gpu
|
| 75 |
|
| 76 |
except Exception as e:
|