Omarelrayes commited on
Commit
c4e33f4
·
verified ·
1 Parent(s): a8ad77b

Update app/configs.py

Browse files
Files changed (1) hide show
  1. app/configs.py +35 -22
app/configs.py CHANGED
@@ -11,9 +11,7 @@ import mlflow.pyfunc
11
 
12
  MLFLOW_URI = "https://omarelrayes-mlflow-server.hf.space"
13
 
14
- mlflow.set_tracking_uri(
15
- MLFLOW_URI
16
- )
17
 
18
 
19
  # =========================
@@ -24,26 +22,50 @@ CLASSIFICATION_MODEL_URI = (
24
  "runs:/9893b5af3e414a75863d18340391078c/model"
25
  )
26
 
27
-
28
  SEGMENTATION_MODEL_URI = (
29
  "runs:/85a78da18bc94263848e61825b9e5104/model"
30
  )
31
 
32
 
33
  # =========================
34
- # Lazy Models
35
  # =========================
36
 
37
  _classification_model: Optional[
38
  mlflow.pyfunc.PyFuncModel
39
  ] = None
40
 
41
-
42
  _segmentation_model: Optional[
43
  mlflow.pyfunc.PyFuncModel
44
  ] = None
45
 
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  def get_classification_model():
49
 
@@ -51,17 +73,18 @@ def get_classification_model():
51
 
52
  if _classification_model is None:
53
 
54
- print(
55
- "Loading Classification Model..."
56
- )
57
 
58
- _classification_model = mlflow.pyfunc.load_model(
59
  CLASSIFICATION_MODEL_URI
60
  )
61
 
62
  return _classification_model
63
 
64
 
 
 
 
65
 
66
  def get_segmentation_model():
67
 
@@ -69,52 +92,42 @@ def get_segmentation_model():
69
 
70
  if _segmentation_model is None:
71
 
72
- print(
73
- "Loading Segmentation Model..."
74
- )
75
 
76
- _segmentation_model = mlflow.pyfunc.load_model(
77
  SEGMENTATION_MODEL_URI
78
  )
79
 
80
  return _segmentation_model
81
 
82
 
83
-
84
  # =========================
85
  # Metadata
86
  # =========================
87
 
88
  model_classes: Dict[int, str] = {
89
-
90
  0: "benign",
91
  1: "malicious"
92
-
93
  }
94
 
95
 
96
  request_history: List[Dict[str, Any]] = []
97
 
98
 
99
-
100
  # =========================
101
  # Storage
102
  # =========================
103
 
104
  STORAGE_DIR = Path("storage")
105
-
106
  IMAGES_DIR = STORAGE_DIR / "images"
107
-
108
  SEGMENTS_DIR = STORAGE_DIR / "segments"
109
 
110
 
111
-
112
  for dir_path in [
113
  STORAGE_DIR,
114
  IMAGES_DIR,
115
  SEGMENTS_DIR
116
  ]:
117
-
118
  dir_path.mkdir(
119
  parents=True,
120
  exist_ok=True
 
11
 
12
  MLFLOW_URI = "https://omarelrayes-mlflow-server.hf.space"
13
 
14
+ mlflow.set_tracking_uri(MLFLOW_URI)
 
 
15
 
16
 
17
  # =========================
 
22
  "runs:/9893b5af3e414a75863d18340391078c/model"
23
  )
24
 
 
25
  SEGMENTATION_MODEL_URI = (
26
  "runs:/85a78da18bc94263848e61825b9e5104/model"
27
  )
28
 
29
 
30
  # =========================
31
+ # Lazy Models (Cache)
32
  # =========================
33
 
34
  _classification_model: Optional[
35
  mlflow.pyfunc.PyFuncModel
36
  ] = None
37
 
 
38
  _segmentation_model: Optional[
39
  mlflow.pyfunc.PyFuncModel
40
  ] = None
41
 
42
 
43
+ # =========================
44
+ # Helpers (IMPORTANT FIX)
45
+ # =========================
46
+
47
+ def _load_mlflow_model(model_uri: str):
48
+ """
49
+ Safe MLflow loader for Hugging Face / Docker environments:
50
+ runs:/ -> download_artifacts -> local path -> load_model
51
+ """
52
+
53
+ print(f"Downloading model: {model_uri}")
54
+
55
+ local_path = mlflow.artifacts.download_artifacts(model_uri)
56
+
57
+ print(f"Model downloaded to: {local_path}")
58
+
59
+ model = mlflow.pyfunc.load_model(local_path)
60
+
61
+ print("Model loaded successfully")
62
+
63
+ return model
64
+
65
+
66
+ # =========================
67
+ # Classification Model
68
+ # =========================
69
 
70
  def get_classification_model():
71
 
 
73
 
74
  if _classification_model is None:
75
 
76
+ print("Loading Classification Model...")
 
 
77
 
78
+ _classification_model = _load_mlflow_model(
79
  CLASSIFICATION_MODEL_URI
80
  )
81
 
82
  return _classification_model
83
 
84
 
85
+ # =========================
86
+ # Segmentation Model
87
+ # =========================
88
 
89
  def get_segmentation_model():
90
 
 
92
 
93
  if _segmentation_model is None:
94
 
95
+ print("Loading Segmentation Model...")
 
 
96
 
97
+ _segmentation_model = _load_mlflow_model(
98
  SEGMENTATION_MODEL_URI
99
  )
100
 
101
  return _segmentation_model
102
 
103
 
 
104
  # =========================
105
  # Metadata
106
  # =========================
107
 
108
  model_classes: Dict[int, str] = {
 
109
  0: "benign",
110
  1: "malicious"
 
111
  }
112
 
113
 
114
  request_history: List[Dict[str, Any]] = []
115
 
116
 
 
117
  # =========================
118
  # Storage
119
  # =========================
120
 
121
  STORAGE_DIR = Path("storage")
 
122
  IMAGES_DIR = STORAGE_DIR / "images"
 
123
  SEGMENTS_DIR = STORAGE_DIR / "segments"
124
 
125
 
 
126
  for dir_path in [
127
  STORAGE_DIR,
128
  IMAGES_DIR,
129
  SEGMENTS_DIR
130
  ]:
 
131
  dir_path.mkdir(
132
  parents=True,
133
  exist_ok=True