Spaces:
Sleeping
Sleeping
fixed globals.py to handle localized models as well
Browse files- backend/preprocessing/globals.py +36 -25
backend/preprocessing/globals.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
# globals.py
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
import joblib
|
|
@@ -9,31 +10,41 @@ from transformers import AutoModel
|
|
| 9 |
from pathlib import Path
|
| 10 |
from huggingface_hub import hf_hub_download
|
| 11 |
|
|
|
|
| 12 |
BACKEND_DIR = Path(__file__).parent.parent
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
repo_id="archis99/Novartis-models",
|
| 17 |
-
filename="scaler_enrollment.pkl"
|
| 18 |
-
)
|
| 19 |
-
scaler = joblib.load(scaler_path)
|
| 20 |
-
|
| 21 |
-
# Download label encoders from hugging face
|
| 22 |
-
label_encoders_path = hf_hub_download(
|
| 23 |
-
repo_id="archis99/Novartis-models",
|
| 24 |
-
filename="feature_label_encoders.pkl"
|
| 25 |
-
)
|
| 26 |
-
label_encoders = joblib.load(label_encoders_path)
|
| 27 |
-
|
| 28 |
-
# Download unique attributes from hugging face
|
| 29 |
-
unique_attributes_path = hf_hub_download(
|
| 30 |
-
repo_id="archis99/Novartis-models",
|
| 31 |
-
filename="study_design_attributes.pkl"
|
| 32 |
-
)
|
| 33 |
-
unique_attributes = joblib.load(unique_attributes_path)
|
| 34 |
-
|
| 35 |
-
# # --- Load saved artifacts using the absolute path ---
|
| 36 |
-
# scaler = joblib.load(BACKEND_DIR / "models/scaler_enrollment.pkl")
|
| 37 |
-
# label_encoders = joblib.load(BACKEND_DIR / "models/feature_label_encoders.pkl")
|
| 38 |
-
# unique_attributes = joblib.load(BACKEND_DIR / "models/study_design_attributes.pkl")
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# globals.py
|
| 2 |
+
import os
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
| 5 |
import joblib
|
|
|
|
| 10 |
from pathlib import Path
|
| 11 |
from huggingface_hub import hf_hub_download
|
| 12 |
|
| 13 |
+
# Base directory
|
| 14 |
BACKEND_DIR = Path(__file__).parent.parent
|
| 15 |
+
MODEL_DIR = BACKEND_DIR / "models"
|
| 16 |
+
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 17 |
|
| 18 |
+
# Hugging Face repo
|
| 19 |
+
HF_REPO = "archis99/Novartis-models"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# Files to manage
|
| 22 |
+
FILES = {
|
| 23 |
+
"scaler": "scaler_enrollment.pkl",
|
| 24 |
+
"label_encoders": "feature_label_encoders.pkl",
|
| 25 |
+
"unique_attributes": "study_design_attributes.pkl"
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
# Dictionary to hold loaded objects
|
| 29 |
+
loaded_artifacts = {}
|
| 30 |
+
|
| 31 |
+
for key, filename in FILES.items():
|
| 32 |
+
local_path = MODEL_DIR / filename
|
| 33 |
+
|
| 34 |
+
# If not present locally, download from HF
|
| 35 |
+
if not local_path.exists():
|
| 36 |
+
print(f"Downloading {filename} from Hugging Face...")
|
| 37 |
+
hf_hub_download(
|
| 38 |
+
repo_id=HF_REPO,
|
| 39 |
+
filename=filename,
|
| 40 |
+
local_dir=MODEL_DIR,
|
| 41 |
+
local_dir_use_symlinks=False
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
# Load the artifact
|
| 45 |
+
loaded_artifacts[key] = joblib.load(local_path)
|
| 46 |
+
|
| 47 |
+
# Unpack for usage
|
| 48 |
+
scaler = loaded_artifacts["scaler"]
|
| 49 |
+
label_encoders = loaded_artifacts["label_encoders"]
|
| 50 |
+
unique_attributes = loaded_artifacts["unique_attributes"]
|