Omarelrayes commited on
Commit
98b9414
Β·
verified Β·
1 Parent(s): c1df100

Update app/configs.py

Browse files
Files changed (1) hide show
  1. app/configs.py +73 -15
app/configs.py CHANGED
@@ -1,10 +1,10 @@
1
  # configs.py
2
- import mlflow
3
- import mlflow.keras
4
  import tensorflow as tf
5
  from pathlib import Path
6
  from typing import Dict, Any, List
7
  import os
 
 
8
 
9
  # ======================
10
  # πŸ”₯ Set PYTHONPATH for sitecustomize.py
@@ -44,18 +44,16 @@ SEGMENTATION_RUN_ID = os.getenv(
44
  )
45
 
46
  # ======================
47
- # MLflow Configuration
48
  # ======================
49
- mlflow.set_tracking_uri(MLFLOW_TRACKING_URI)
50
-
51
- CLASSIFICATION_MODEL_URI = f"runs:/{CLASSIFICATION_RUN_ID}/model"
52
- SEGMENTATION_MODEL_URI = f"runs:/{SEGMENTATION_RUN_ID}/model"
53
 
54
  print("="*60)
55
  print("πŸ”§ Configuration:")
56
  print(f" - MLflow Tracking URI: {MLFLOW_TRACKING_URI}")
57
  print(f" - Classification Run ID: {CLASSIFICATION_RUN_ID}")
58
  print(f" - Segmentation Run ID: {SEGMENTATION_RUN_ID}")
 
59
  print(f" - TensorFlow version: {tf.__version__}")
60
  print("="*60)
61
 
@@ -65,6 +63,30 @@ print("="*60)
65
  _classification_model = None
66
  _segmentation_model = None
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  # ======================
69
  # Classification Model Loader
70
  # ======================
@@ -74,13 +96,31 @@ def get_classification_model():
74
  if _classification_model is None:
75
  print("="*60)
76
  print("πŸ”„ Loading Classification Model...")
77
- print(f"πŸ”— URI: {CLASSIFICATION_MODEL_URI}")
78
  print("="*60)
79
 
80
  try:
81
- # πŸ”₯ Ψ¨Ψ―ΩˆΩ† compile=False
82
- _classification_model = mlflow.keras.load_model(
83
- CLASSIFICATION_MODEL_URI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  )
85
 
86
  print("βœ… Classification Model Loaded Successfully!")
@@ -101,13 +141,31 @@ def get_segmentation_model():
101
  if _segmentation_model is None:
102
  print("="*60)
103
  print("πŸ”„ Loading Segmentation Model...")
104
- print(f"πŸ”— URI: {SEGMENTATION_MODEL_URI}")
105
  print("="*60)
106
 
107
  try:
108
- # πŸ”₯ Ψ¨Ψ―ΩˆΩ† compile=False
109
- _segmentation_model = mlflow.keras.load_model(
110
- SEGMENTATION_MODEL_URI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  )
112
 
113
  print("βœ… Segmentation Model Loaded Successfully!")
 
1
  # configs.py
 
 
2
  import tensorflow as tf
3
  from pathlib import Path
4
  from typing import Dict, Any, List
5
  import os
6
+ from huggingface_hub import hf_hub_download
7
+ import mlflow
8
 
9
  # ======================
10
  # πŸ”₯ Set PYTHONPATH for sitecustomize.py
 
44
  )
45
 
46
  # ======================
47
+ # HuggingFace Hub Configuration
48
  # ======================
49
+ HF_REPO = "omarelrayes/mlflow-artifacts"
 
 
 
50
 
51
  print("="*60)
52
  print("πŸ”§ Configuration:")
53
  print(f" - MLflow Tracking URI: {MLFLOW_TRACKING_URI}")
54
  print(f" - Classification Run ID: {CLASSIFICATION_RUN_ID}")
55
  print(f" - Segmentation Run ID: {SEGMENTATION_RUN_ID}")
56
+ print(f" - HF Repository: {HF_REPO}")
57
  print(f" - TensorFlow version: {tf.__version__}")
58
  print("="*60)
59
 
 
63
  _classification_model = None
64
  _segmentation_model = None
65
 
66
+ # ======================
67
+ # Helper: Get model path from MLflow UI
68
+ # ======================
69
+ def get_model_path_from_mlflow(run_id, experiment_id):
70
+ """Get the actual model path from MLflow server"""
71
+ try:
72
+ mlflow.set_tracking_uri(MLFLOW_TRACKING_URI)
73
+ run = mlflow.get_run(run_id)
74
+
75
+ # Get the logged model info
76
+ if 'mlflow.logged_models' in run.data.tags:
77
+ import json
78
+ logged_models = json.loads(run.data.tags['mlflow.logged_models'])
79
+ if logged_models:
80
+ model_id = logged_models[0].get('model_id')
81
+ if model_id:
82
+ return f"{experiment_id}/models/{model_id}/artifacts"
83
+
84
+ # Fallback to old path
85
+ return f"{experiment_id}/{run_id}/artifacts"
86
+ except Exception as e:
87
+ print(f"⚠️ Could not get model path from MLflow: {e}")
88
+ return f"{experiment_id}/{run_id}/artifacts"
89
+
90
  # ======================
91
  # Classification Model Loader
92
  # ======================
 
96
  if _classification_model is None:
97
  print("="*60)
98
  print("πŸ”„ Loading Classification Model...")
99
+ print(f"πŸ”— Run ID: {CLASSIFICATION_RUN_ID}")
100
  print("="*60)
101
 
102
  try:
103
+ # πŸ”₯ Try to get the actual model path
104
+ model_path_in_repo = get_model_path_from_mlflow(CLASSIFICATION_RUN_ID, "1")
105
+
106
+ # πŸ”₯ Download model.keras directly from HF Hub
107
+ print(f"πŸ“¦ Downloading from HF Hub: {HF_REPO}")
108
+ print(f"πŸ“ Path: {model_path_in_repo}/data/model.keras")
109
+
110
+ model_file = hf_hub_download(
111
+ repo_id=HF_REPO,
112
+ filename=f"{model_path_in_repo}/data/model.keras",
113
+ repo_type="model",
114
+ token=HF_TOKEN,
115
+ cache_dir="/tmp/hf_cache"
116
+ )
117
+
118
+ print(f"πŸ“₯ Model downloaded to: {model_file}")
119
+
120
+ # πŸ”₯ Load model with Keras
121
+ _classification_model = tf.keras.models.load_model(
122
+ model_file,
123
+ compile=False
124
  )
125
 
126
  print("βœ… Classification Model Loaded Successfully!")
 
141
  if _segmentation_model is None:
142
  print("="*60)
143
  print("πŸ”„ Loading Segmentation Model...")
144
+ print(f"πŸ”— Run ID: {SEGMENTATION_RUN_ID}")
145
  print("="*60)
146
 
147
  try:
148
+ # πŸ”₯ Try to get the actual model path
149
+ model_path_in_repo = get_model_path_from_mlflow(SEGMENTATION_RUN_ID, "2")
150
+
151
+ # πŸ”₯ Download model.keras directly from HF Hub
152
+ print(f"πŸ“¦ Downloading from HF Hub: {HF_REPO}")
153
+ print(f"πŸ“ Path: {model_path_in_repo}/data/model.keras")
154
+
155
+ model_file = hf_hub_download(
156
+ repo_id=HF_REPO,
157
+ filename=f"{model_path_in_repo}/data/model.keras",
158
+ repo_type="model",
159
+ token=HF_TOKEN,
160
+ cache_dir="/tmp/hf_cache"
161
+ )
162
+
163
+ print(f"πŸ“₯ Model downloaded to: {model_file}")
164
+
165
+ # πŸ”₯ Load model with Keras
166
+ _segmentation_model = tf.keras.models.load_model(
167
+ model_file,
168
+ compile=False
169
  )
170
 
171
  print("βœ… Segmentation Model Loaded Successfully!")