-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel_manager.py
More file actions
39 lines (31 loc) · 1.25 KB
/
model_manager.py
File metadata and controls
39 lines (31 loc) · 1.25 KB
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
import importlib
import logging
import os
import torch
loaded_models = {}
model_locks = {}
def clear_gpu_memory():
if torch.cuda.is_available():
torch.cuda.empty_cache()
def load_model(model_version):
print(f"Attempting to load model version: {model_version}")
# Return cached model if available
if model_version in loaded_models and loaded_models[model_version]:
print("Using cached model")
return loaded_models[model_version]
try:
module_name = f"models.{model_version}.model"
if model_version not in model_locks:
model_module = importlib.import_module(module_name)
# Initialize model if not already initialized
if not hasattr(model_module, 'is_initialized'):
print("Initializing model for the first time")
model_module.initialize_model()
model_module.is_initialized = True
loaded_models[model_version] = model_module
print("Model loaded and cached successfully")
return loaded_models[model_version]
except Exception as e:
print(f"Error loading model: {str(e)}")
logging.error(f"Model loading error: {str(e)}")
return None