Omarelrayes commited on
Commit
77671cb
Β·
verified Β·
1 Parent(s): 902ac87

Update app/configs.py

Browse files
Files changed (1) hide show
  1. app/configs.py +100 -35
app/configs.py CHANGED
@@ -1,73 +1,138 @@
1
  import mlflow
2
  import mlflow.tensorflow
3
-
4
  from pathlib import Path
5
  from typing import Dict, Any, List
 
6
 
7
  # ======================
8
- # MLflow Setup
9
  # ======================
10
-
11
- MLFLOW_URI = "https://omarelrayes-mlflow-server.hf.space"
12
  mlflow.set_tracking_uri(MLFLOW_URI)
13
 
 
 
 
 
 
 
 
 
 
 
14
  # ======================
15
- # Model URIs
16
  # ======================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
- CLASSIFICATION_MODEL_URI = "runs:/2ae2c35a75a14f619fc782a9034b4a16/model"
19
- SEGMENTATION_MODEL_URI = "runs:/0835d2274eb34091aa209ef1800f92f0/model"
20
 
21
  # ======================
22
  # Cached models
23
  # ======================
24
-
25
  _classification_model = None
26
  _segmentation_model = None
27
 
28
-
29
  # ======================
30
- # Classification
31
  # ======================
32
-
33
  def get_classification_model():
34
  global _classification_model
35
 
36
  if _classification_model is None:
37
- print("Loading Classification Model...")
38
-
39
- _classification_model = mlflow.tensorflow.load_model(
40
- CLASSIFICATION_MODEL_URI
41
- )
42
-
43
- print("Classification Model Loaded")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  return _classification_model
46
 
47
-
48
  # ======================
49
- # Segmentation
50
  # ======================
51
-
52
  def get_segmentation_model():
53
  global _segmentation_model
54
 
55
  if _segmentation_model is None:
56
- print("Loading Segmentation Model...")
57
-
58
- _segmentation_model = mlflow.tensorflow.load_model(
59
- SEGMENTATION_MODEL_URI
60
- )
61
-
62
- print("Segmentation Model Loaded")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
  return _segmentation_model
65
 
66
-
67
  # ======================
68
- # Classes
69
  # ======================
70
-
71
  model_classes = {
72
  0: "benign",
73
  1: "malignant"
@@ -75,14 +140,14 @@ model_classes = {
75
 
76
  request_history: List[Dict[str, Any]] = []
77
 
78
-
79
  # ======================
80
- # Storage
81
  # ======================
82
-
83
  STORAGE_DIR = Path("storage")
84
  IMAGES_DIR = STORAGE_DIR / "images"
85
  SEGMENTS_DIR = STORAGE_DIR / "segments"
86
 
87
  for d in [STORAGE_DIR, IMAGES_DIR, SEGMENTS_DIR]:
88
- d.mkdir(parents=True, exist_ok=True)
 
 
 
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
+ # 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 for Artifacts
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"
44
 
45
  # ======================
46
  # Cached models
47
  # ======================
 
48
  _classification_model = None
49
  _segmentation_model = None
50
 
 
51
  # ======================
52
+ # Classification Model Loader
53
  # ======================
 
54
  def get_classification_model():
55
  global _classification_model
56
 
57
  if _classification_model is None:
58
+ print("="*60)
59
+ print("πŸ”„ Loading Classification Model via MLflow...")
60
+ print(f"πŸ”— URI: {CLASSIFICATION_MODEL_URI}")
61
+ print(f"🌐 Tracking URI: {MLFLOW_URI}")
62
+ print("="*60)
63
+
64
+ try:
65
+ _classification_model = mlflow.tensorflow.load_model(
66
+ CLASSIFICATION_MODEL_URI
67
+ )
68
+ print("βœ… Classification Model Loaded Successfully!")
69
+ except Exception as e:
70
+ print(f"❌ Error loading classification model: {e}")
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
91
 
 
92
  # ======================
93
+ # Segmentation Model Loader
94
  # ======================
 
95
  def get_segmentation_model():
96
  global _segmentation_model
97
 
98
  if _segmentation_model is None:
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:
106
+ _segmentation_model = mlflow.tensorflow.load_model(
107
+ SEGMENTATION_MODEL_URI
108
+ )
109
+ print("βœ… Segmentation Model Loaded Successfully!")
110
+ except Exception as e:
111
+ print(f"❌ Error loading segmentation model: {e}")
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"
 
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!")