Omarelrayes commited on
Commit
b2640bb
·
verified ·
1 Parent(s): 6003eae

Update app/configs.py

Browse files
Files changed (1) hide show
  1. app/configs.py +16 -89
app/configs.py CHANGED
@@ -1,134 +1,61 @@
1
- from pathlib import Path
2
- from typing import Dict, Any, List, Optional
3
-
4
  import mlflow
5
  import mlflow.pyfunc
6
-
7
-
8
- # =========================
9
- # MLflow Config
10
- # =========================
11
 
12
  MLFLOW_URI = "https://omarelrayes-mlflow-server.hf.space"
13
 
14
  mlflow.set_tracking_uri(MLFLOW_URI)
15
 
 
 
16
 
17
- # =========================
18
- # Run IDs from MLflow
19
- # =========================
20
-
21
- CLASSIFICATION_MODEL_URI = (
22
- "runs:/efcf3f12eacc409daffe6a50888fa759/model"
23
- )
24
-
25
- SEGMENTATION_MODEL_URI = (
26
- "runs:/d5de5322822b45349b7bd311e747c794/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
-
72
  global _classification_model
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
-
91
  global _segmentation_model
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
134
- )
 
 
 
 
1
  import mlflow
2
  import mlflow.pyfunc
3
+ from typing import Optional, Dict, Any, List
 
 
 
 
4
 
5
  MLFLOW_URI = "https://omarelrayes-mlflow-server.hf.space"
6
 
7
  mlflow.set_tracking_uri(MLFLOW_URI)
8
 
9
+ CLASSIFICATION_MODEL_URI = "runs:/efcf3f12eacc409daffe6a50888fa759/model"
10
+ SEGMENTATION_MODEL_URI = "runs:/d5de5322822b45349b7bd311e747c794/model"
11
 
12
+ _classification_model = None
13
+ _segmentation_model = None
 
 
 
 
 
 
 
 
 
 
14
 
 
 
 
15
 
16
+ def load_model(uri: str):
17
+ print(f"Downloading model: {uri}")
 
18
 
19
+ local_path = mlflow.artifacts.download_artifacts(uri)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  print(f"Model downloaded to: {local_path}")
22
 
23
+ return mlflow.pyfunc.load_model(local_path)
 
 
24
 
 
 
 
 
 
 
25
 
26
  def get_classification_model():
 
27
  global _classification_model
28
 
29
  if _classification_model is None:
 
30
  print("Loading Classification Model...")
31
+ _classification_model = load_model(CLASSIFICATION_MODEL_URI)
 
 
 
32
 
33
  return _classification_model
34
 
35
 
 
 
 
 
36
  def get_segmentation_model():
 
37
  global _segmentation_model
38
 
39
  if _segmentation_model is None:
 
40
  print("Loading Segmentation Model...")
41
+ _segmentation_model = load_model(SEGMENTATION_MODEL_URI)
 
 
 
42
 
43
  return _segmentation_model
44
 
45
 
46
+ model_classes = {
 
 
 
 
47
  0: "benign",
48
+ 1: "malignant"
49
  }
50
 
 
51
  request_history: List[Dict[str, Any]] = []
52
 
53
 
54
+ from pathlib import Path
 
 
55
 
56
  STORAGE_DIR = Path("storage")
57
  IMAGES_DIR = STORAGE_DIR / "images"
58
  SEGMENTS_DIR = STORAGE_DIR / "segments"
59
 
60
+ for d in [STORAGE_DIR, IMAGES_DIR, SEGMENTS_DIR]:
61
+ d.mkdir(parents=True, exist_ok=True)