Spaces:
Sleeping
Sleeping
Update app/configs.py
Browse files- app/configs.py +15 -64
app/configs.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 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
|
|
@@ -33,33 +33,29 @@ MLFLOW_TRACKING_URI = os.getenv(
|
|
| 33 |
"https://omarelrayes-mlflow-server.hf.space"
|
| 34 |
)
|
| 35 |
|
| 36 |
-
# ๐ฅ ุญุฏูุซ Run IDs ุจุนุฏ ุงูุชุณุฌูู ุงูุฌุฏูุฏ
|
| 37 |
CLASSIFICATION_RUN_ID = os.getenv(
|
| 38 |
"CLASSIFICATION_RUN_ID",
|
| 39 |
-
"3f36062325d147cb8d0865e271dee4f2"
|
| 40 |
)
|
| 41 |
|
| 42 |
SEGMENTATION_RUN_ID = os.getenv(
|
| 43 |
"SEGMENTATION_RUN_ID",
|
| 44 |
-
"07b39a864ffb4b7eb0b9b42fd2e42705"
|
| 45 |
)
|
| 46 |
|
| 47 |
-
# ======================
|
| 48 |
-
# HuggingFace Hub Configuration
|
| 49 |
-
# ======================
|
| 50 |
-
HF_REPO = "omarelrayes/mlflow-artifacts"
|
| 51 |
-
|
| 52 |
# ======================
|
| 53 |
# MLflow Configuration
|
| 54 |
# ======================
|
| 55 |
mlflow.set_tracking_uri(MLFLOW_TRACKING_URI)
|
| 56 |
|
|
|
|
|
|
|
|
|
|
| 57 |
print("="*60)
|
| 58 |
print("๐ง Configuration:")
|
| 59 |
print(f" - MLflow 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(f" - HF Repository: {HF_REPO}")
|
| 63 |
print(f" - TensorFlow version: {tf.__version__}")
|
| 64 |
print("="*60)
|
| 65 |
|
|
@@ -69,29 +65,6 @@ print("="*60)
|
|
| 69 |
_classification_model = None
|
| 70 |
_segmentation_model = None
|
| 71 |
|
| 72 |
-
# ======================
|
| 73 |
-
# Helper Function: Find model.h5 in directory
|
| 74 |
-
# ======================
|
| 75 |
-
def find_h5_model_file(directory):
|
| 76 |
-
"""Search for model.h5 in directory and subdirectories"""
|
| 77 |
-
print(f"๐ Searching for model.h5 in {directory}...")
|
| 78 |
-
|
| 79 |
-
import glob
|
| 80 |
-
matches = glob.glob(os.path.join(directory, "**", "*.h5"), recursive=True)
|
| 81 |
-
|
| 82 |
-
if matches:
|
| 83 |
-
model_file = matches[0]
|
| 84 |
-
print(f"โ
Found model.h5 at: {model_file}")
|
| 85 |
-
return model_file
|
| 86 |
-
|
| 87 |
-
# List all files for debugging
|
| 88 |
-
print(f"๐ Files in {directory}:")
|
| 89 |
-
for root, dirs, files in os.walk(directory):
|
| 90 |
-
for file in files:
|
| 91 |
-
print(f" - {os.path.join(root, file)}")
|
| 92 |
-
|
| 93 |
-
raise FileNotFoundError(f"model.h5 not found in {directory}")
|
| 94 |
-
|
| 95 |
# ======================
|
| 96 |
# Classification Model Loader
|
| 97 |
# ======================
|
|
@@ -101,24 +74,13 @@ def get_classification_model():
|
|
| 101 |
if _classification_model is None:
|
| 102 |
print("="*60)
|
| 103 |
print("๐ Loading Classification Model...")
|
| 104 |
-
print(f"๐
|
| 105 |
print("="*60)
|
| 106 |
|
| 107 |
try:
|
| 108 |
-
# ๐ฅ
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
dst_path="/tmp/mlflow_models/classification"
|
| 112 |
-
)
|
| 113 |
-
|
| 114 |
-
print(f"๐ฅ Downloaded to: {local_path}")
|
| 115 |
-
|
| 116 |
-
# ๐ฅ Find model.h5 in the downloaded files
|
| 117 |
-
model_file = find_h5_model_file(local_path)
|
| 118 |
-
|
| 119 |
-
# ๐ฅ Load model from HDF5
|
| 120 |
-
_classification_model = tf.keras.models.load_model(
|
| 121 |
-
model_file,
|
| 122 |
compile=False
|
| 123 |
)
|
| 124 |
|
|
@@ -140,24 +102,13 @@ def get_segmentation_model():
|
|
| 140 |
if _segmentation_model is None:
|
| 141 |
print("="*60)
|
| 142 |
print("๐ Loading Segmentation Model...")
|
| 143 |
-
print(f"๐
|
| 144 |
print("="*60)
|
| 145 |
|
| 146 |
try:
|
| 147 |
-
# ๐ฅ
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
dst_path="/tmp/mlflow_models/segmentation"
|
| 151 |
-
)
|
| 152 |
-
|
| 153 |
-
print(f"๐ฅ Downloaded to: {local_path}")
|
| 154 |
-
|
| 155 |
-
# ๐ฅ Find model.h5 in the downloaded files
|
| 156 |
-
model_file = find_h5_model_file(local_path)
|
| 157 |
-
|
| 158 |
-
# ๐ฅ Load model from HDF5
|
| 159 |
-
_segmentation_model = tf.keras.models.load_model(
|
| 160 |
-
model_file,
|
| 161 |
compile=False
|
| 162 |
)
|
| 163 |
|
|
|
|
| 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
|
|
|
|
| 33 |
"https://omarelrayes-mlflow-server.hf.space"
|
| 34 |
)
|
| 35 |
|
|
|
|
| 36 |
CLASSIFICATION_RUN_ID = os.getenv(
|
| 37 |
"CLASSIFICATION_RUN_ID",
|
| 38 |
+
"3f36062325d147cb8d0865e271dee4f2"
|
| 39 |
)
|
| 40 |
|
| 41 |
SEGMENTATION_RUN_ID = os.getenv(
|
| 42 |
"SEGMENTATION_RUN_ID",
|
| 43 |
+
"07b39a864ffb4b7eb0b9b42fd2e42705"
|
| 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 |
_classification_model = None
|
| 66 |
_segmentation_model = None
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
# ======================
|
| 69 |
# Classification Model Loader
|
| 70 |
# ======================
|
|
|
|
| 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 |
+
# ๐ฅ ุงุณุชุฎุฏุงู
mlflow.keras.load_model ู
ุจุงุดุฑุฉ
|
| 82 |
+
_classification_model = mlflow.keras.load_model(
|
| 83 |
+
CLASSIFICATION_MODEL_URI,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
compile=False
|
| 85 |
)
|
| 86 |
|
|
|
|
| 102 |
if _segmentation_model is None:
|
| 103 |
print("="*60)
|
| 104 |
print("๐ Loading Segmentation Model...")
|
| 105 |
+
print(f"๐ URI: {SEGMENTATION_MODEL_URI}")
|
| 106 |
print("="*60)
|
| 107 |
|
| 108 |
try:
|
| 109 |
+
# ๐ฅ ุงุณุชุฎุฏุงู
mlflow.keras.load_model ู
ุจุงุดุฑุฉ
|
| 110 |
+
_segmentation_model = mlflow.keras.load_model(
|
| 111 |
+
SEGMENTATION_MODEL_URI,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
compile=False
|
| 113 |
)
|
| 114 |
|