Spaces:
Sleeping
Sleeping
Update app/configs.py
Browse files- app/configs.py +16 -77
app/configs.py
CHANGED
|
@@ -4,40 +4,34 @@ from pathlib import Path
|
|
| 4 |
from typing import Dict, Any, List
|
| 5 |
import os
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# ======================
|
| 8 |
# MLflow Configuration
|
| 9 |
# ======================
|
| 10 |
MLFLOW_URI = os.getenv("MLFLOW_TRACKING_URI", "https://omarelrayes-mlflow-server.hf.space")
|
| 11 |
mlflow.set_tracking_uri(MLFLOW_URI)
|
| 12 |
|
| 13 |
-
# HuggingFace Hub Configuration
|
| 14 |
-
# β οΈ IMPORTANT: HF_TOKEN must be set in Space Settings (not hardcoded!)
|
| 15 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 16 |
if not HF_TOKEN:
|
| 17 |
-
raise ValueError(
|
| 18 |
-
"HF_TOKEN environment variable is not set! "
|
| 19 |
-
"Please add it in HuggingFace Space Settings -> Variables and secrets"
|
| 20 |
-
)
|
| 21 |
os.environ["HF_TOKEN"] = HF_TOKEN
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
# Model URIs (UPDATE WITH YOUR NEW RUN IDs)
|
| 25 |
-
# ======================
|
| 26 |
-
# β οΈ IMPORTANT: Replace these with the actual run IDs from registration
|
| 27 |
CLASSIFICATION_RUN_ID = os.getenv("CLASSIFICATION_RUN_ID")
|
| 28 |
SEGMENTATION_RUN_ID = os.getenv("SEGMENTATION_RUN_ID")
|
| 29 |
|
| 30 |
if not CLASSIFICATION_RUN_ID:
|
| 31 |
-
raise ValueError(
|
| 32 |
-
"CLASSIFICATION_RUN_ID environment variable is not set! "
|
| 33 |
-
"Please add it in HuggingFace Space Settings -> Variables and secrets"
|
| 34 |
-
)
|
| 35 |
-
|
| 36 |
if not SEGMENTATION_RUN_ID:
|
| 37 |
-
raise ValueError(
|
| 38 |
-
"SEGMENTATION_RUN_ID environment variable is not set! "
|
| 39 |
-
"Please add it in HuggingFace Space Settings -> Variables and secrets"
|
| 40 |
-
)
|
| 41 |
|
| 42 |
CLASSIFICATION_MODEL_URI = f"runs:/{CLASSIFICATION_RUN_ID}/model"
|
| 43 |
SEGMENTATION_MODEL_URI = f"runs:/{SEGMENTATION_RUN_ID}/model"
|
|
@@ -67,24 +61,7 @@ def get_classification_model():
|
|
| 67 |
)
|
| 68 |
print("β
Classification Model Loaded Successfully!")
|
| 69 |
except Exception as e:
|
| 70 |
-
print(f"β Error
|
| 71 |
-
print("\nπ Debugging Info:")
|
| 72 |
-
print(f" - Tracking URI: {mlflow.get_tracking_uri()}")
|
| 73 |
-
print(f" - Model URI: {CLASSIFICATION_MODEL_URI}")
|
| 74 |
-
|
| 75 |
-
# Try to list available runs
|
| 76 |
-
try:
|
| 77 |
-
from mlflow.tracking import MlflowClient
|
| 78 |
-
client = MlflowClient()
|
| 79 |
-
runs = client.search_runs(experiment_names=["skin_cancer_classification"])
|
| 80 |
-
print(f" - Available runs: {[run.info.run_id for run in runs]}")
|
| 81 |
-
|
| 82 |
-
if runs:
|
| 83 |
-
artifacts = client.list_artifacts(runs[0].info.run_id)
|
| 84 |
-
print(f" - Artifacts in first run: {[a.path for a in artifacts]}")
|
| 85 |
-
except Exception as debug_err:
|
| 86 |
-
print(f" - Debug error: {debug_err}")
|
| 87 |
-
|
| 88 |
raise
|
| 89 |
|
| 90 |
return _classification_model
|
|
@@ -99,7 +76,6 @@ def get_segmentation_model():
|
|
| 99 |
print("="*60)
|
| 100 |
print("π Loading Segmentation Model via MLflow...")
|
| 101 |
print(f"π URI: {SEGMENTATION_MODEL_URI}")
|
| 102 |
-
print(f"π Tracking URI: {MLFLOW_URI}")
|
| 103 |
print("="*60)
|
| 104 |
|
| 105 |
try:
|
|
@@ -108,46 +84,9 @@ def get_segmentation_model():
|
|
| 108 |
)
|
| 109 |
print("β
Segmentation Model Loaded Successfully!")
|
| 110 |
except Exception as e:
|
| 111 |
-
print(f"β Error
|
| 112 |
-
print("\nπ Debugging Info:")
|
| 113 |
-
print(f" - Tracking URI: {mlflow.get_tracking_uri()}")
|
| 114 |
-
print(f" - Model URI: {SEGMENTATION_MODEL_URI}")
|
| 115 |
-
|
| 116 |
-
# Try to list available runs
|
| 117 |
-
try:
|
| 118 |
-
from mlflow.tracking import MlflowClient
|
| 119 |
-
client = MlflowClient()
|
| 120 |
-
runs = client.search_runs(experiment_names=["skin_cancer_segmentation"])
|
| 121 |
-
print(f" - Available runs: {[run.info.run_id for run in runs]}")
|
| 122 |
-
|
| 123 |
-
if runs:
|
| 124 |
-
artifacts = client.list_artifacts(runs[0].info.run_id)
|
| 125 |
-
print(f" - Artifacts in first run: {[a.path for a in artifacts]}")
|
| 126 |
-
except Exception as debug_err:
|
| 127 |
-
print(f" - Debug error: {debug_err}")
|
| 128 |
-
|
| 129 |
raise
|
| 130 |
|
| 131 |
return _segmentation_model
|
| 132 |
|
| 133 |
-
#
|
| 134 |
-
# Model Classes
|
| 135 |
-
# ======================
|
| 136 |
-
model_classes = {
|
| 137 |
-
0: "benign",
|
| 138 |
-
1: "malignant"
|
| 139 |
-
}
|
| 140 |
-
|
| 141 |
-
request_history: List[Dict[str, Any]] = []
|
| 142 |
-
|
| 143 |
-
# ======================
|
| 144 |
-
# Storage Directories
|
| 145 |
-
# ======================
|
| 146 |
-
STORAGE_DIR = Path("storage")
|
| 147 |
-
IMAGES_DIR = STORAGE_DIR / "images"
|
| 148 |
-
SEGMENTS_DIR = STORAGE_DIR / "segments"
|
| 149 |
-
|
| 150 |
-
for d in [STORAGE_DIR, IMAGES_DIR, SEGMENTS_DIR]:
|
| 151 |
-
d.mkdir(parents=True, exist_ok=True)
|
| 152 |
-
|
| 153 |
-
print("β
Models module initialized successfully!")
|
|
|
|
| 4 |
from typing import Dict, Any, List
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# Import custom artifact repository
|
| 8 |
+
import sys
|
| 9 |
+
sys.path.insert(0, "/app")
|
| 10 |
+
try:
|
| 11 |
+
from hf_artifact_repo import HuggingFaceArtifactRepository
|
| 12 |
+
except ImportError:
|
| 13 |
+
print("β οΈ Custom artifact repo not found, using default MLflow")
|
| 14 |
+
|
| 15 |
# ======================
|
| 16 |
# MLflow Configuration
|
| 17 |
# ======================
|
| 18 |
MLFLOW_URI = os.getenv("MLFLOW_TRACKING_URI", "https://omarelrayes-mlflow-server.hf.space")
|
| 19 |
mlflow.set_tracking_uri(MLFLOW_URI)
|
| 20 |
|
| 21 |
+
# HuggingFace Hub Configuration
|
|
|
|
| 22 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 23 |
if not HF_TOKEN:
|
| 24 |
+
raise ValueError("HF_TOKEN environment variable is not set!")
|
|
|
|
|
|
|
|
|
|
| 25 |
os.environ["HF_TOKEN"] = HF_TOKEN
|
| 26 |
|
| 27 |
+
# Model URIs
|
|
|
|
|
|
|
|
|
|
| 28 |
CLASSIFICATION_RUN_ID = os.getenv("CLASSIFICATION_RUN_ID")
|
| 29 |
SEGMENTATION_RUN_ID = os.getenv("SEGMENTATION_RUN_ID")
|
| 30 |
|
| 31 |
if not CLASSIFICATION_RUN_ID:
|
| 32 |
+
raise ValueError("CLASSIFICATION_RUN_ID not set!")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
if not SEGMENTATION_RUN_ID:
|
| 34 |
+
raise ValueError("SEGMENTATION_RUN_ID not set!")
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
CLASSIFICATION_MODEL_URI = f"runs:/{CLASSIFICATION_RUN_ID}/model"
|
| 37 |
SEGMENTATION_MODEL_URI = f"runs:/{SEGMENTATION_RUN_ID}/model"
|
|
|
|
| 61 |
)
|
| 62 |
print("β
Classification Model Loaded Successfully!")
|
| 63 |
except Exception as e:
|
| 64 |
+
print(f"β Error: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
raise
|
| 66 |
|
| 67 |
return _classification_model
|
|
|
|
| 76 |
print("="*60)
|
| 77 |
print("π Loading Segmentation Model via MLflow...")
|
| 78 |
print(f"π URI: {SEGMENTATION_MODEL_URI}")
|
|
|
|
| 79 |
print("="*60)
|
| 80 |
|
| 81 |
try:
|
|
|
|
| 84 |
)
|
| 85 |
print("β
Segmentation Model Loaded Successfully!")
|
| 86 |
except Exception as e:
|
| 87 |
+
print(f"β Error: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
raise
|
| 89 |
|
| 90 |
return _segmentation_model
|
| 91 |
|
| 92 |
+
# ... rest of the code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|