Omarelrayes commited on
Commit
c008d0d
Β·
verified Β·
1 Parent(s): 82ea932

Update app/configs.py

Browse files
Files changed (1) hide show
  1. app/configs.py +60 -50
app/configs.py CHANGED
@@ -1,11 +1,10 @@
1
  # configs.py
2
  import mlflow
3
- import mlflow.artifacts
4
  import tensorflow as tf
5
  from pathlib import Path
6
  from typing import Dict, Any, List
7
  import os
8
- import glob
9
 
10
  # ======================
11
  # πŸ”₯ Set PYTHONPATH for sitecustomize.py
@@ -13,7 +12,7 @@ import glob
13
  os.environ["PYTHONPATH"] = "/app:" + os.environ.get("PYTHONPATH", "")
14
 
15
  # ======================
16
- # πŸ”₯ Import HF Plugin
17
  # ======================
18
  try:
19
  from hf_artifact_repo import HuggingFaceArtifactRepository
@@ -44,16 +43,26 @@ SEGMENTATION_RUN_ID = os.getenv(
44
  "c321c4e944064127a1198e781c83cad8"
45
  )
46
 
 
 
 
 
 
 
 
 
 
47
  # ======================
48
  # MLflow Configuration
49
  # ======================
50
  mlflow.set_tracking_uri(MLFLOW_TRACKING_URI)
51
 
52
  print("="*60)
53
- print("πŸ”§ MLflow Configuration:")
54
- print(f" - Tracking URI: {MLFLOW_TRACKING_URI}")
55
  print(f" - Classification Run ID: {CLASSIFICATION_RUN_ID}")
56
  print(f" - Segmentation Run ID: {SEGMENTATION_RUN_ID}")
 
57
  print("="*60)
58
 
59
  # ======================
@@ -62,29 +71,6 @@ print("="*60)
62
  _classification_model = None
63
  _segmentation_model = None
64
 
65
- # ======================
66
- # Helper Function: Find model.keras in directory
67
- # ======================
68
- def find_model_file(directory):
69
- """Search for model.keras in directory and subdirectories"""
70
- print(f"πŸ” Searching for model.keras in {directory}...")
71
-
72
- # Search recursively
73
- matches = glob.glob(os.path.join(directory, "**", "model.keras"), recursive=True)
74
-
75
- if matches:
76
- model_file = matches[0]
77
- print(f"βœ… Found model.keras at: {model_file}")
78
- return model_file
79
-
80
- # List all files for debugging
81
- print(f"πŸ“‚ Files in {directory}:")
82
- for root, dirs, files in os.walk(directory):
83
- for file in files:
84
- print(f" - {os.path.join(root, file)}")
85
-
86
- raise FileNotFoundError(f"model.keras not found in {directory}")
87
-
88
  # ======================
89
  # Classification Model Loader
90
  # ======================
@@ -94,28 +80,40 @@ def get_classification_model():
94
  if _classification_model is None:
95
  print("="*60)
96
  print("πŸ”„ Loading Classification Model...")
97
- print(f"πŸ”— Run ID: {CLASSIFICATION_RUN_ID}")
 
 
98
  print("="*60)
99
 
100
  try:
101
- # πŸ”₯ Download ALL artifacts from MLflow
102
- local_path = mlflow.artifacts.download_artifacts(
103
- run_id=CLASSIFICATION_RUN_ID,
104
- dst_path="/tmp/mlflow_models/classification"
 
 
 
105
  )
106
 
107
- print(f"πŸ“₯ Downloaded to: {local_path}")
108
-
109
- # πŸ”₯ Find model.keras in the downloaded files
110
- model_file = find_model_file(local_path)
111
 
112
- # πŸ”₯ Load model from local path
113
  _classification_model = tf.keras.models.load_model(
114
- model_file,
115
  compile=False
116
  )
117
 
118
  print("βœ… Classification Model Loaded Successfully!")
 
 
 
 
 
 
 
 
 
 
119
  except Exception as e:
120
  print(f"❌ Error loading classification model: {e}")
121
  import traceback
@@ -133,28 +131,40 @@ def get_segmentation_model():
133
  if _segmentation_model is None:
134
  print("="*60)
135
  print("πŸ”„ Loading Segmentation Model...")
136
- print(f"πŸ”— Run ID: {SEGMENTATION_RUN_ID}")
 
 
137
  print("="*60)
138
 
139
  try:
140
- # πŸ”₯ Download ALL artifacts from MLflow
141
- local_path = mlflow.artifacts.download_artifacts(
142
- run_id=SEGMENTATION_RUN_ID,
143
- dst_path="/tmp/mlflow_models/segmentation"
 
 
 
144
  )
145
 
146
- print(f"πŸ“₯ Downloaded to: {local_path}")
147
-
148
- # πŸ”₯ Find model.keras in the downloaded files
149
- model_file = find_model_file(local_path)
150
 
151
- # πŸ”₯ Load model from local path
152
  _segmentation_model = tf.keras.models.load_model(
153
- model_file,
154
  compile=False
155
  )
156
 
157
  print("βœ… Segmentation Model Loaded Successfully!")
 
 
 
 
 
 
 
 
 
 
158
  except Exception as e:
159
  print(f"❌ Error loading segmentation model: {e}")
160
  import traceback
 
1
  # configs.py
2
  import mlflow
 
3
  import tensorflow as tf
4
  from pathlib import Path
5
  from typing import Dict, Any, List
6
  import os
7
+ from huggingface_hub import hf_hub_download
8
 
9
  # ======================
10
  # πŸ”₯ Set PYTHONPATH for sitecustomize.py
 
12
  os.environ["PYTHONPATH"] = "/app:" + os.environ.get("PYTHONPATH", "")
13
 
14
  # ======================
15
+ # πŸ”₯ Import HF Plugin (for MLflow compatibility)
16
  # ======================
17
  try:
18
  from hf_artifact_repo import HuggingFaceArtifactRepository
 
43
  "c321c4e944064127a1198e781c83cad8"
44
  )
45
 
46
+ # ======================
47
+ # HuggingFace Hub Configuration
48
+ # ======================
49
+ HF_REPO = "omarelrayes/mlflow-artifacts"
50
+
51
+ # πŸ”₯ Ψ§Ω„Ω…Ψ³Ψ§Ψ±Ψ§Ψͺ Ψ§Ω„Ψ­Ω‚ΩŠΩ‚ΩŠΨ© في HF Hub
52
+ CLASSIFICATION_MODEL_PATH = "1/models/m-7152eeee57b24d1eb3b0312c9de6354c/artifacts/data/model.keras"
53
+ SEGMENTATION_MODEL_PATH = "2/models/m-7c6aea2dc3f64395a63a8b5ea90d6994/artifacts/data/model.keras"
54
+
55
  # ======================
56
  # MLflow Configuration
57
  # ======================
58
  mlflow.set_tracking_uri(MLFLOW_TRACKING_URI)
59
 
60
  print("="*60)
61
+ print("πŸ”§ Configuration:")
62
+ print(f" - MLflow Tracking URI: {MLFLOW_TRACKING_URI}")
63
  print(f" - Classification Run ID: {CLASSIFICATION_RUN_ID}")
64
  print(f" - Segmentation Run ID: {SEGMENTATION_RUN_ID}")
65
+ print(f" - HF Repository: {HF_REPO}")
66
  print("="*60)
67
 
68
  # ======================
 
71
  _classification_model = None
72
  _segmentation_model = None
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  # ======================
75
  # Classification Model Loader
76
  # ======================
 
80
  if _classification_model is None:
81
  print("="*60)
82
  print("πŸ”„ Loading Classification Model...")
83
+ print(f"πŸ“¦ From HuggingFace Hub: {HF_REPO}")
84
+ print(f"πŸ“ Path: {CLASSIFICATION_MODEL_PATH}")
85
+ print(f"πŸ”— MLflow Run ID: {CLASSIFICATION_RUN_ID}")
86
  print("="*60)
87
 
88
  try:
89
+ # πŸ”₯ Step 1: Download from HuggingFace Hub
90
+ model_path = hf_hub_download(
91
+ repo_id=HF_REPO,
92
+ filename=CLASSIFICATION_MODEL_PATH,
93
+ repo_type="model",
94
+ token=HF_TOKEN,
95
+ cache_dir="/tmp/hf_cache"
96
  )
97
 
98
+ print(f"πŸ“₯ Model downloaded to: {model_path}")
 
 
 
99
 
100
+ # πŸ”₯ Step 2: Load model
101
  _classification_model = tf.keras.models.load_model(
102
+ model_path,
103
  compile=False
104
  )
105
 
106
  print("βœ… Classification Model Loaded Successfully!")
107
+
108
+ # πŸ”₯ Step 3: Log to MLflow (for tracking)
109
+ with mlflow.start_run(run_id=CLASSIFICATION_RUN_ID, nested=True):
110
+ mlflow.log_param("model_source", "huggingface_hub")
111
+ mlflow.log_param("hf_repo", HF_REPO)
112
+ mlflow.log_param("model_path", CLASSIFICATION_MODEL_PATH)
113
+ mlflow.log_param("status", "loaded")
114
+
115
+ print("βœ… Logged to MLflow for tracking!")
116
+
117
  except Exception as e:
118
  print(f"❌ Error loading classification model: {e}")
119
  import traceback
 
131
  if _segmentation_model is None:
132
  print("="*60)
133
  print("πŸ”„ Loading Segmentation Model...")
134
+ print(f"πŸ“¦ From HuggingFace Hub: {HF_REPO}")
135
+ print(f"πŸ“ Path: {SEGMENTATION_MODEL_PATH}")
136
+ print(f"πŸ”— MLflow Run ID: {SEGMENTATION_RUN_ID}")
137
  print("="*60)
138
 
139
  try:
140
+ # πŸ”₯ Step 1: Download from HuggingFace Hub
141
+ model_path = hf_hub_download(
142
+ repo_id=HF_REPO,
143
+ filename=SEGMENTATION_MODEL_PATH,
144
+ repo_type="model",
145
+ token=HF_TOKEN,
146
+ cache_dir="/tmp/hf_cache"
147
  )
148
 
149
+ print(f"πŸ“₯ Model downloaded to: {model_path}")
 
 
 
150
 
151
+ # πŸ”₯ Step 2: Load model
152
  _segmentation_model = tf.keras.models.load_model(
153
+ model_path,
154
  compile=False
155
  )
156
 
157
  print("βœ… Segmentation Model Loaded Successfully!")
158
+
159
+ # πŸ”₯ Step 3: Log to MLflow (for tracking)
160
+ with mlflow.start_run(run_id=SEGMENTATION_RUN_ID, nested=True):
161
+ mlflow.log_param("model_source", "huggingface_hub")
162
+ mlflow.log_param("hf_repo", HF_REPO)
163
+ mlflow.log_param("model_path", SEGMENTATION_MODEL_PATH)
164
+ mlflow.log_param("status", "loaded")
165
+
166
+ print("βœ… Logged to MLflow for tracking!")
167
+
168
  except Exception as e:
169
  print(f"❌ Error loading segmentation model: {e}")
170
  import traceback