sunil18p31a0101 commited on
Commit
3e6074c
·
verified ·
1 Parent(s): 9a36a53

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -8
app.py CHANGED
@@ -13,12 +13,18 @@ from sklearn.preprocessing import LabelEncoder
13
  model = joblib.load('lgbm_model.pkl') # Replace with actual path
14
  scaler = joblib.load('minmax_scaler.pkl') # Replace with actual path
15
 
16
- # Get expected feature names from the model
17
- expected_features = model.feature_name_ # Extracts trained feature names
 
 
 
 
 
 
18
 
19
  # Initialize LabelEncoder for gender encoding (if used in training)
20
  gender_encoder = LabelEncoder()
21
- gender_encoder.fit(['Female', 'Male']) # Ensure correct mapping
22
 
23
  # Function to extract features
24
  def extract_features(image):
@@ -68,18 +74,22 @@ def predict_hemoglobin(age, gender, image):
68
 
69
  # Extract features
70
  features = extract_features(image)
71
-
72
- # Encode gender only if the model was trained with it
73
- if "Gender" in expected_features:
74
  features["Gender"] = gender_encoder.transform([gender])[0]
75
-
76
  features["Age"] = age
77
 
78
  # Convert to DataFrame
79
  features_df = pd.DataFrame([features])
80
 
81
  # Ensure only model-expected features are used
82
- features_df = features_df[expected_features] # Select only required columns
 
 
 
 
83
 
84
  # Apply scaling
85
  features_scaled = scaler.transform(features_df)
 
13
  model = joblib.load('lgbm_model.pkl') # Replace with actual path
14
  scaler = joblib.load('minmax_scaler.pkl') # Replace with actual path
15
 
16
+ # Define the expected feature names manually
17
+ expected_features = ['meanr', 'meang', 'meanb', 'HHR', 'Ent', 'B',
18
+ 'g1', 'g2', 'g3', 'g4', 'g5', 'Age'] # No 'Hgb'
19
+
20
+ # Include 'Gender' if it was used during training
21
+ use_gender = True # Set to False if your model was not trained with 'Gender'
22
+ if use_gender:
23
+ expected_features.append('Gender')
24
 
25
  # Initialize LabelEncoder for gender encoding (if used in training)
26
  gender_encoder = LabelEncoder()
27
+ gender_encoder.fit(['Female', 'Male'])
28
 
29
  # Function to extract features
30
  def extract_features(image):
 
74
 
75
  # Extract features
76
  features = extract_features(image)
77
+
78
+ # Encode gender only if used in training
79
+ if use_gender:
80
  features["Gender"] = gender_encoder.transform([gender])[0]
81
+
82
  features["Age"] = age
83
 
84
  # Convert to DataFrame
85
  features_df = pd.DataFrame([features])
86
 
87
  # Ensure only model-expected features are used
88
+ for col in expected_features:
89
+ if col not in features_df:
90
+ features_df[col] = 0 # Add missing columns with default value
91
+
92
+ features_df = features_df[expected_features] # Ensure correct column order
93
 
94
  # Apply scaling
95
  features_scaled = scaler.transform(features_df)