Spaces:
Sleeping
Sleeping
Update app/configs.py
Browse files- app/configs.py +18 -36
app/configs.py
CHANGED
|
@@ -1,17 +1,15 @@
|
|
| 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 |
|
| 8 |
# ======================
|
| 9 |
-
# ๐ฅ
|
| 10 |
-
# ======================
|
| 11 |
-
os.environ["PYTHONPATH"] = "/app:" + os.environ.get("PYTHONPATH", "")
|
| 12 |
-
|
| 13 |
-
# ======================
|
| 14 |
-
# ๐ฅ Import HF Plugin (for MLflow compatibility)
|
| 15 |
# ======================
|
| 16 |
try:
|
| 17 |
from hf_artifact_repo import HuggingFaceArtifactRepository
|
|
@@ -32,7 +30,7 @@ os.environ["HF_TOKEN"] = HF_TOKEN
|
|
| 32 |
# ======================
|
| 33 |
HF_REPO = "omarelrayes/mlflow-artifacts"
|
| 34 |
|
| 35 |
-
# ๐ฅ ุงูู
ุณุงุฑุงุช ุงูุซุงุจุชุฉ ู
ู ุงูุชุณุฌูู ุงูุณุงุจู
|
| 36 |
CLASSIFICATION_MODEL_PATH = "1/models/m-2fa9b990eeac4e18b925616422228223/artifacts/data/model.keras"
|
| 37 |
SEGMENTATION_MODEL_PATH = "2/models/m-21ba32f804234073952d1a8e590958ec/artifacts/data/model.keras"
|
| 38 |
|
|
@@ -42,6 +40,7 @@ print(f" - HF Repository: {HF_REPO}")
|
|
| 42 |
print(f" - Classification Path: {CLASSIFICATION_MODEL_PATH}")
|
| 43 |
print(f" - Segmentation Path: {SEGMENTATION_MODEL_PATH}")
|
| 44 |
print(f" - TensorFlow version: {tf.__version__}")
|
|
|
|
| 45 |
print("="*60)
|
| 46 |
|
| 47 |
# ======================
|
|
@@ -59,12 +58,10 @@ def get_classification_model():
|
|
| 59 |
if _classification_model is None:
|
| 60 |
print("="*60)
|
| 61 |
print("๐ Loading Classification Model...")
|
| 62 |
-
print(f"๐ฆ Repository: {HF_REPO}")
|
| 63 |
-
print(f"๐ Path: {CLASSIFICATION_MODEL_PATH}")
|
| 64 |
print("="*60)
|
| 65 |
|
| 66 |
try:
|
| 67 |
-
# ๐ฅ Download
|
| 68 |
model_file = hf_hub_download(
|
| 69 |
repo_id=HF_REPO,
|
| 70 |
filename=CLASSIFICATION_MODEL_PATH,
|
|
@@ -75,10 +72,11 @@ def get_classification_model():
|
|
| 75 |
|
| 76 |
print(f"๐ฅ Model downloaded to: {model_file}")
|
| 77 |
|
| 78 |
-
# ๐ฅ
|
| 79 |
-
_classification_model =
|
| 80 |
model_file,
|
| 81 |
-
compile=False
|
|
|
|
| 82 |
)
|
| 83 |
|
| 84 |
print("โ
Classification Model Loaded Successfully!")
|
|
@@ -99,12 +97,10 @@ def get_segmentation_model():
|
|
| 99 |
if _segmentation_model is None:
|
| 100 |
print("="*60)
|
| 101 |
print("๐ Loading Segmentation Model...")
|
| 102 |
-
print(f"๐ฆ Repository: {HF_REPO}")
|
| 103 |
-
print(f"๐ Path: {SEGMENTATION_MODEL_PATH}")
|
| 104 |
print("="*60)
|
| 105 |
|
| 106 |
try:
|
| 107 |
-
# ๐ฅ Download
|
| 108 |
model_file = hf_hub_download(
|
| 109 |
repo_id=HF_REPO,
|
| 110 |
filename=SEGMENTATION_MODEL_PATH,
|
|
@@ -115,10 +111,11 @@ def get_segmentation_model():
|
|
| 115 |
|
| 116 |
print(f"๐ฅ Model downloaded to: {model_file}")
|
| 117 |
|
| 118 |
-
# ๐ฅ
|
| 119 |
-
_segmentation_model =
|
| 120 |
model_file,
|
| 121 |
-
compile=False
|
|
|
|
| 122 |
)
|
| 123 |
|
| 124 |
print("โ
Segmentation Model Loaded Successfully!")
|
|
@@ -135,19 +132,4 @@ def get_segmentation_model():
|
|
| 135 |
# ======================
|
| 136 |
model_classes = {
|
| 137 |
0: "benign",
|
| 138 |
-
|
| 139 |
-
}
|
| 140 |
-
|
| 141 |
-
request_history: List[Dict[str, Any]] = []
|
| 142 |
-
|
| 143 |
-
# ======================
|
| 144 |
-
# Storage Directories
|
| 145 |
-
# ======================
|
| 146 |
-
STORAGE_DIR = Path("storage")
|
| 147 |
-
IMAGES_DIR = STORAGE_DIR / "images"
|
| 148 |
-
SEGMENTS_DIR = STORAGE_DIR / "segments"
|
| 149 |
-
|
| 150 |
-
for d in [STORAGE_DIR, IMAGES_DIR, SEGMENTS_DIR]:
|
| 151 |
-
d.mkdir(parents=True, exist_ok=True)
|
| 152 |
-
|
| 153 |
-
print("โ
Models module initialized successfully!")
|
|
|
|
| 1 |
# configs.py
|
| 2 |
+
import os
|
| 3 |
+
os.environ["PYTHONPATH"] = "/app:" + os.environ.get("PYTHONPATH", "")
|
| 4 |
+
|
| 5 |
import tensorflow as tf
|
| 6 |
+
import keras
|
| 7 |
from pathlib import Path
|
| 8 |
from typing import Dict, Any, List
|
|
|
|
| 9 |
from huggingface_hub import hf_hub_download
|
| 10 |
|
| 11 |
# ======================
|
| 12 |
+
# ๐ฅ Import HF Plugin
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# ======================
|
| 14 |
try:
|
| 15 |
from hf_artifact_repo import HuggingFaceArtifactRepository
|
|
|
|
| 30 |
# ======================
|
| 31 |
HF_REPO = "omarelrayes/mlflow-artifacts"
|
| 32 |
|
| 33 |
+
# ๐ฅ ุงูู
ุณุงุฑุงุช ุงูุซุงุจุชุฉ (ู
ู ุงูุชุณุฌูู ุงูุณุงุจู)
|
| 34 |
CLASSIFICATION_MODEL_PATH = "1/models/m-2fa9b990eeac4e18b925616422228223/artifacts/data/model.keras"
|
| 35 |
SEGMENTATION_MODEL_PATH = "2/models/m-21ba32f804234073952d1a8e590958ec/artifacts/data/model.keras"
|
| 36 |
|
|
|
|
| 40 |
print(f" - Classification Path: {CLASSIFICATION_MODEL_PATH}")
|
| 41 |
print(f" - Segmentation Path: {SEGMENTATION_MODEL_PATH}")
|
| 42 |
print(f" - TensorFlow version: {tf.__version__}")
|
| 43 |
+
print(f" - Keras version: {keras.__version__}")
|
| 44 |
print("="*60)
|
| 45 |
|
| 46 |
# ======================
|
|
|
|
| 58 |
if _classification_model is None:
|
| 59 |
print("="*60)
|
| 60 |
print("๐ Loading Classification Model...")
|
|
|
|
|
|
|
| 61 |
print("="*60)
|
| 62 |
|
| 63 |
try:
|
| 64 |
+
# ๐ฅ Download from HuggingFace Hub
|
| 65 |
model_file = hf_hub_download(
|
| 66 |
repo_id=HF_REPO,
|
| 67 |
filename=CLASSIFICATION_MODEL_PATH,
|
|
|
|
| 72 |
|
| 73 |
print(f"๐ฅ Model downloaded to: {model_file}")
|
| 74 |
|
| 75 |
+
# ๐ฅ ๐ฅ ๐ฅ CRITICAL: Use keras.saving.load_model with safe_mode=False
|
| 76 |
+
_classification_model = keras.saving.load_model(
|
| 77 |
model_file,
|
| 78 |
+
compile=False,
|
| 79 |
+
safe_mode=False # ๐ฅ ุชุฌุงูุฒ ู
ุดููุฉ quantization_config
|
| 80 |
)
|
| 81 |
|
| 82 |
print("โ
Classification Model Loaded Successfully!")
|
|
|
|
| 97 |
if _segmentation_model is None:
|
| 98 |
print("="*60)
|
| 99 |
print("๐ Loading Segmentation Model...")
|
|
|
|
|
|
|
| 100 |
print("="*60)
|
| 101 |
|
| 102 |
try:
|
| 103 |
+
# ๐ฅ Download from HuggingFace Hub
|
| 104 |
model_file = hf_hub_download(
|
| 105 |
repo_id=HF_REPO,
|
| 106 |
filename=SEGMENTATION_MODEL_PATH,
|
|
|
|
| 111 |
|
| 112 |
print(f"๐ฅ Model downloaded to: {model_file}")
|
| 113 |
|
| 114 |
+
# ๐ฅ ๐ฅ ๐ฅ CRITICAL: Use keras.saving.load_model with safe_mode=False
|
| 115 |
+
_segmentation_model = keras.saving.load_model(
|
| 116 |
model_file,
|
| 117 |
+
compile=False,
|
| 118 |
+
safe_mode=False # ๐ฅ ุชุฌุงูุฒ ู
ุดููุฉ quantization_config
|
| 119 |
)
|
| 120 |
|
| 121 |
print("โ
Segmentation Model Loaded Successfully!")
|
|
|
|
| 132 |
# ======================
|
| 133 |
model_classes = {
|
| 134 |
0: "benign",
|
| 135 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|