Spaces:
Sleeping
Sleeping
Update app/configs.py
Browse files- app/configs.py +71 -22
app/configs.py
CHANGED
|
@@ -1,41 +1,66 @@
|
|
|
|
|
| 1 |
import mlflow
|
| 2 |
import mlflow.tensorflow
|
|
|
|
| 3 |
from pathlib import Path
|
| 4 |
from typing import Dict, Any, List
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
try:
|
| 11 |
from hf_artifact_repo import HuggingFaceArtifactRepository
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
# ======================
|
| 16 |
-
#
|
| 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(
|
|
|
|
|
|
|
|
|
|
| 25 |
os.environ["HF_TOKEN"] = HF_TOKEN
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
CLASSIFICATION_MODEL_URI = f"runs:/{CLASSIFICATION_RUN_ID}/model"
|
| 37 |
SEGMENTATION_MODEL_URI = f"runs:/{SEGMENTATION_RUN_ID}/model"
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
# ======================
|
| 40 |
# Cached models
|
| 41 |
# ======================
|
|
@@ -52,7 +77,7 @@ def get_classification_model():
|
|
| 52 |
print("="*60)
|
| 53 |
print("π Loading Classification Model via MLflow...")
|
| 54 |
print(f"π URI: {CLASSIFICATION_MODEL_URI}")
|
| 55 |
-
print(f"π Tracking URI: {
|
| 56 |
print("="*60)
|
| 57 |
|
| 58 |
try:
|
|
@@ -61,7 +86,9 @@ def get_classification_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
|
|
@@ -84,9 +111,31 @@ def get_segmentation_model():
|
|
| 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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# configs.py
|
| 2 |
import mlflow
|
| 3 |
import mlflow.tensorflow
|
| 4 |
+
import tensorflow as tf
|
| 5 |
from pathlib import Path
|
| 6 |
from typing import Dict, Any, List
|
| 7 |
import os
|
| 8 |
|
| 9 |
+
# ======================
|
| 10 |
+
# π₯ CRITICAL: Set PYTHONPATH for sitecustomize.py
|
| 11 |
+
# ======================
|
| 12 |
+
os.environ["PYTHONPATH"] = "/app:" + os.environ.get("PYTHONPATH", "")
|
| 13 |
+
|
| 14 |
+
# ======================
|
| 15 |
+
# π₯ Import HF Plugin (will be auto-registered by sitecustomize.py)
|
| 16 |
+
# ======================
|
| 17 |
try:
|
| 18 |
from hf_artifact_repo import HuggingFaceArtifactRepository
|
| 19 |
+
print("β
HuggingFace Artifact Repository loaded!")
|
| 20 |
+
except ImportError as e:
|
| 21 |
+
print(f"β οΈ Could not load HF plugin: {e}")
|
| 22 |
|
| 23 |
# ======================
|
| 24 |
+
# Environment Variables
|
| 25 |
# ======================
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 27 |
if not HF_TOKEN:
|
| 28 |
+
raise ValueError(
|
| 29 |
+
"β HF_TOKEN environment variable is not set! "
|
| 30 |
+
"Please add it in HuggingFace Space Settings -> Variables and secrets"
|
| 31 |
+
)
|
| 32 |
os.environ["HF_TOKEN"] = HF_TOKEN
|
| 33 |
|
| 34 |
+
MLFLOW_TRACKING_URI = os.getenv(
|
| 35 |
+
"MLFLOW_TRACKING_URI",
|
| 36 |
+
"https://omarelrayes-mlflow-server.hf.space"
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
CLASSIFICATION_RUN_ID = os.getenv(
|
| 40 |
+
"CLASSIFICATION_RUN_ID",
|
| 41 |
+
"330d8a6678574cb3ae2caa296d927a6d" # π₯ Your new Run ID
|
| 42 |
+
)
|
| 43 |
|
| 44 |
+
SEGMENTATION_RUN_ID = os.getenv(
|
| 45 |
+
"SEGMENTATION_RUN_ID",
|
| 46 |
+
"c321c4e944064127a1198e781c83cad8" # π₯ Your new Run ID
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# ======================
|
| 50 |
+
# MLflow Configuration
|
| 51 |
+
# ======================
|
| 52 |
+
mlflow.set_tracking_uri(MLFLOW_TRACKING_URI)
|
| 53 |
|
| 54 |
CLASSIFICATION_MODEL_URI = f"runs:/{CLASSIFICATION_RUN_ID}/model"
|
| 55 |
SEGMENTATION_MODEL_URI = f"runs:/{SEGMENTATION_RUN_ID}/model"
|
| 56 |
|
| 57 |
+
print("="*60)
|
| 58 |
+
print("π§ MLflow Configuration:")
|
| 59 |
+
print(f" - Tracking URI: {MLFLOW_TRACKING_URI}")
|
| 60 |
+
print(f" - Classification Run ID: {CLASSIFICATION_RUN_ID}")
|
| 61 |
+
print(f" - Segmentation Run ID: {SEGMENTATION_RUN_ID}")
|
| 62 |
+
print("="*60)
|
| 63 |
+
|
| 64 |
# ======================
|
| 65 |
# Cached models
|
| 66 |
# ======================
|
|
|
|
| 77 |
print("="*60)
|
| 78 |
print("π Loading Classification Model via MLflow...")
|
| 79 |
print(f"π URI: {CLASSIFICATION_MODEL_URI}")
|
| 80 |
+
print(f"π Tracking URI: {MLFLOW_TRACKING_URI}")
|
| 81 |
print("="*60)
|
| 82 |
|
| 83 |
try:
|
|
|
|
| 86 |
)
|
| 87 |
print("β
Classification Model Loaded Successfully!")
|
| 88 |
except Exception as e:
|
| 89 |
+
print(f"β Error loading classification model: {e}")
|
| 90 |
+
import traceback
|
| 91 |
+
traceback.print_exc()
|
| 92 |
raise
|
| 93 |
|
| 94 |
return _classification_model
|
|
|
|
| 111 |
)
|
| 112 |
print("β
Segmentation Model Loaded Successfully!")
|
| 113 |
except Exception as e:
|
| 114 |
+
print(f"β Error loading segmentation model: {e}")
|
| 115 |
+
import traceback
|
| 116 |
+
traceback.print_exc()
|
| 117 |
raise
|
| 118 |
|
| 119 |
return _segmentation_model
|
| 120 |
|
| 121 |
+
# ======================
|
| 122 |
+
# Model Classes
|
| 123 |
+
# ======================
|
| 124 |
+
model_classes = {
|
| 125 |
+
0: "benign",
|
| 126 |
+
1: "malignant"
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
request_history: List[Dict[str, Any]] = []
|
| 130 |
+
|
| 131 |
+
# ======================
|
| 132 |
+
# Storage Directories
|
| 133 |
+
# ======================
|
| 134 |
+
STORAGE_DIR = Path("storage")
|
| 135 |
+
IMAGES_DIR = STORAGE_DIR / "images"
|
| 136 |
+
SEGMENTS_DIR = STORAGE_DIR / "segments"
|
| 137 |
+
|
| 138 |
+
for d in [STORAGE_DIR, IMAGES_DIR, SEGMENTS_DIR]:
|
| 139 |
+
d.mkdir(parents=True, exist_ok=True)
|
| 140 |
+
|
| 141 |
+
print("β
Models module initialized successfully!")
|