Omarelrayes commited on
Commit
4fcd9ff
Β·
verified Β·
1 Parent(s): bc61eae

Update app/configs.py

Browse files
Files changed (1) hide show
  1. 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
- # 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"
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: {MLFLOW_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
- # ... rest of the code
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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!")