Spaces:
Sleeping
Sleeping
Update app/configs.py
Browse files- 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
|
| 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("π§
|
| 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"
|
|
|
|
|
|
|
| 98 |
print("="*60)
|
| 99 |
|
| 100 |
try:
|
| 101 |
-
# π₯
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
| 105 |
)
|
| 106 |
|
| 107 |
-
print(f"π₯
|
| 108 |
-
|
| 109 |
-
# π₯ Find model.keras in the downloaded files
|
| 110 |
-
model_file = find_model_file(local_path)
|
| 111 |
|
| 112 |
-
# π₯
|
| 113 |
_classification_model = tf.keras.models.load_model(
|
| 114 |
-
|
| 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"
|
|
|
|
|
|
|
| 137 |
print("="*60)
|
| 138 |
|
| 139 |
try:
|
| 140 |
-
# π₯
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
| 144 |
)
|
| 145 |
|
| 146 |
-
print(f"π₯
|
| 147 |
-
|
| 148 |
-
# π₯ Find model.keras in the downloaded files
|
| 149 |
-
model_file = find_model_file(local_path)
|
| 150 |
|
| 151 |
-
# π₯
|
| 152 |
_segmentation_model = tf.keras.models.load_model(
|
| 153 |
-
|
| 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
|