abreza commited on
Commit
ea52793
·
1 Parent(s): 14aebdf

refactor: replace load_model with get_model for improved model loading

Browse files
Files changed (2) hide show
  1. app.py +2 -2
  2. model_utils.py +10 -18
app.py CHANGED
@@ -1,9 +1,9 @@
1
  from interface import create_interface
2
- from model_utils import load_model
3
 
4
  def main():
5
  print("Loading model...")
6
- model = load_model()
7
 
8
  if model is None:
9
  print("Warning: Model could not be loaded. Please ensure 'best_model.pkl' exists.")
 
1
  from interface import create_interface
2
+ from model_utils import get_model
3
 
4
  def main():
5
  print("Loading model...")
6
+ model = get_model()
7
 
8
  if model is None:
9
  print("Warning: Model could not be loaded. Please ensure 'best_model.pkl' exists.")
model_utils.py CHANGED
@@ -5,17 +5,6 @@ from config import MODEL_PATH
5
 
6
  warnings.filterwarnings('ignore')
7
 
8
- model = None
9
-
10
- def load_model():
11
- global model
12
- try:
13
- model = joblib.load(MODEL_PATH)
14
- return model
15
- except Exception as e:
16
- print(f"Error loading model: {e}")
17
- return None
18
-
19
  def calculate_derived_features(age, weight, height, neutrophil, lymphocyte, platelet):
20
  height_m = height / 100
21
  bmi = weight / (height_m ** 2)
@@ -26,12 +15,7 @@ def calculate_derived_features(age, weight, height, neutrophil, lymphocyte, plat
26
  def predict_outcome(age, weight, height, gravidity, parity, h_abortion,
27
  living_child, gestational_age, hemoglobin, hematocrit,
28
  platelet, mpv, pdw, neutrophil, lymphocyte):
29
- global model
30
-
31
- if model is None:
32
- model = load_model()
33
- if model is None:
34
- return "خطا: مدل بارگذاری نشد", ""
35
 
36
  try:
37
  bmi, nlr, plr = calculate_derived_features(age, weight, height, neutrophil, lymphocyte, platelet)
@@ -72,8 +56,16 @@ def predict_outcome(age, weight, height, gravidity, parity, h_abortion,
72
  except Exception as e:
73
  return f"خطا در پردازش: {str(e)}", ""
74
 
 
 
 
75
  def get_model():
76
  global model
77
  if model is None:
78
- model = load_model()
 
 
 
 
 
79
  return model
 
5
 
6
  warnings.filterwarnings('ignore')
7
 
 
 
 
 
 
 
 
 
 
 
 
8
  def calculate_derived_features(age, weight, height, neutrophil, lymphocyte, platelet):
9
  height_m = height / 100
10
  bmi = weight / (height_m ** 2)
 
15
  def predict_outcome(age, weight, height, gravidity, parity, h_abortion,
16
  living_child, gestational_age, hemoglobin, hematocrit,
17
  platelet, mpv, pdw, neutrophil, lymphocyte):
18
+ model = get_model()
 
 
 
 
 
19
 
20
  try:
21
  bmi, nlr, plr = calculate_derived_features(age, weight, height, neutrophil, lymphocyte, platelet)
 
56
  except Exception as e:
57
  return f"خطا در پردازش: {str(e)}", ""
58
 
59
+
60
+ model = None
61
+
62
  def get_model():
63
  global model
64
  if model is None:
65
+ try:
66
+ model = joblib.load(MODEL_PATH)
67
+ return model
68
+ except Exception as e:
69
+ print(f"Error loading model: {e}")
70
+ return None
71
  return model