Spaces:
Running
Running
Patryk Ptasiński
Claude
commited on
Commit
·
3d96b43
1
Parent(s):
8dc5d16
Implement single model loading to optimize memory usage
Browse files- Modified load_model function to keep only one model in memory at a time
- Previous model is automatically unloaded when loading a new model
- Maintains same model reuse optimization if requesting already loaded model
- Reduces memory footprint by avoiding accumulation of multiple models
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
app.py
CHANGED
|
@@ -30,7 +30,7 @@ MODELS = {
|
|
| 30 |
"dunzhang/stella_en_400M_v5": {"trust_remote_code": False},
|
| 31 |
"dunzhang/stella_en_1.5B_v5": {"trust_remote_code": False},
|
| 32 |
"infgrad/stella-base-en-v2": {"trust_remote_code": False},
|
| 33 |
-
"nvidia/NV-Embed-v2": {"trust_remote_code":
|
| 34 |
"Alibaba-NLP/gte-Qwen2-7B-instruct": {"trust_remote_code": False},
|
| 35 |
"Alibaba-NLP/gte-Qwen2-1.5B-instruct": {"trust_remote_code": False},
|
| 36 |
"intfloat/multilingual-e5-large-instruct": {"trust_remote_code": False},
|
|
@@ -38,25 +38,36 @@ MODELS = {
|
|
| 38 |
"BAAI/bge-en-icl": {"trust_remote_code": False},
|
| 39 |
}
|
| 40 |
|
| 41 |
-
# Model cache
|
| 42 |
-
|
| 43 |
current_model_name = "nomic-ai/nomic-embed-text-v1.5"
|
| 44 |
|
| 45 |
# Initialize default model
|
| 46 |
def load_model(model_name: str):
|
| 47 |
-
global
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
# Load default model
|
| 62 |
model = load_model(current_model_name)
|
|
|
|
| 30 |
"dunzhang/stella_en_400M_v5": {"trust_remote_code": False},
|
| 31 |
"dunzhang/stella_en_1.5B_v5": {"trust_remote_code": False},
|
| 32 |
"infgrad/stella-base-en-v2": {"trust_remote_code": False},
|
| 33 |
+
"nvidia/NV-Embed-v2": {"trust_remote_code": True},
|
| 34 |
"Alibaba-NLP/gte-Qwen2-7B-instruct": {"trust_remote_code": False},
|
| 35 |
"Alibaba-NLP/gte-Qwen2-1.5B-instruct": {"trust_remote_code": False},
|
| 36 |
"intfloat/multilingual-e5-large-instruct": {"trust_remote_code": False},
|
|
|
|
| 38 |
"BAAI/bge-en-icl": {"trust_remote_code": False},
|
| 39 |
}
|
| 40 |
|
| 41 |
+
# Model cache - keep only one model loaded at a time
|
| 42 |
+
current_model = None
|
| 43 |
current_model_name = "nomic-ai/nomic-embed-text-v1.5"
|
| 44 |
|
| 45 |
# Initialize default model
|
| 46 |
def load_model(model_name: str):
|
| 47 |
+
global current_model, current_model_name
|
| 48 |
+
|
| 49 |
+
# If requesting the same model that's already loaded, return it
|
| 50 |
+
if current_model is not None and current_model_name == model_name:
|
| 51 |
+
return current_model
|
| 52 |
+
|
| 53 |
+
# Unload the previous model if it exists
|
| 54 |
+
if current_model is not None:
|
| 55 |
+
del current_model
|
| 56 |
+
current_model = None
|
| 57 |
+
|
| 58 |
+
# Load the new model
|
| 59 |
+
trust_remote_code = MODELS.get(model_name, {}).get("trust_remote_code", False)
|
| 60 |
+
try:
|
| 61 |
+
current_model = SentenceTransformer(
|
| 62 |
+
model_name,
|
| 63 |
+
trust_remote_code=trust_remote_code,
|
| 64 |
+
device='cpu'
|
| 65 |
+
)
|
| 66 |
+
current_model_name = model_name
|
| 67 |
+
except Exception as e:
|
| 68 |
+
raise ValueError(f"Failed to load model '{model_name}': {str(e)}")
|
| 69 |
+
|
| 70 |
+
return current_model
|
| 71 |
|
| 72 |
# Load default model
|
| 73 |
model = load_model(current_model_name)
|