high77 commited on
Commit
3f0ded1
·
verified ·
1 Parent(s): 110da2a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -24
app.py CHANGED
@@ -2,9 +2,9 @@ import streamlit as st
2
  import pandas as pd
3
  import joblib
4
 
5
- # Load model and scaler with correct filenames
6
- model = joblib.load("model/tuned_xgboost_model.pkl")
7
- scaler = joblib.load("model/scaler.pkl")
8
 
9
  st.title("Hemoglobin Level Predictor")
10
 
@@ -22,32 +22,40 @@ st.markdown(
22
  unsafe_allow_html=True
23
  )
24
 
25
- # Input fields (ensure the feature names match your trained model's expected features)
26
  age = st.number_input("Age", min_value=0, max_value=120, value=30)
 
 
 
 
 
 
27
  gender = st.selectbox("Gender", options=["Male", "Female"])
28
- o2_saturation = st.slider("Oxygen Saturation (%)", min_value=50.0, max_value=100.0, value=98.0)
29
- bp_systolic = st.number_input("Systolic BP", min_value=50, max_value=200, value=120)
30
- bp_diastolic = st.number_input("Diastolic BP", min_value=30, max_value=130, value=80)
31
- respiratory_rate = st.number_input("Respiratory Rate (breaths/min)", min_value=5, max_value=60, value=18)
32
 
33
- # Map gender to expected model encoding
34
- gender_male = 1 if gender == "Male" else 0
35
- gender_female = 1 - gender_male # if needed for one-hot features
 
36
 
37
- # Build input dataframe with exact feature names your model expects
38
- input_df = pd.DataFrame([{
39
  "Age": age,
40
- "Gender_Encoded": gender_male, # or "Gender_Male" if that’s the column name
41
- "OxygenSaturation(%)": o2_saturation, # exact feature name from training
42
- "BP_Systolic": bp_systolic,
43
- "DBP": bp_diastolic,
44
- "Respiratory_Rate": respiratory_rate,
45
- # Add any other features your model needs here, with correct names!
 
 
 
46
  }])
47
 
 
48
  if st.button("Predict Hemoglobin Level"):
49
- # Scale inputs
50
- input_scaled = scaler.transform(input_df)
51
- # Predict using model
52
- prediction = model.predict(input_scaled)[0]
53
- st.success(f"Predicted Hemoglobin Level: {prediction:.2f} g/dL")
 
 
2
  import pandas as pd
3
  import joblib
4
 
5
+ # Load model and scaler (assumed to be in root directory)
6
+ model = joblib.load("tuned_xgboost_model.pkl")
7
+ scaler = joblib.load("scaler.pkl")
8
 
9
  st.title("Hemoglobin Level Predictor")
10
 
 
22
  unsafe_allow_html=True
23
  )
24
 
25
+ # Collect user inputs
26
  age = st.number_input("Age", min_value=0, max_value=120, value=30)
27
+ sbp = st.number_input("Systolic Blood Pressure (SBP)", min_value=50, max_value=200, value=120)
28
+ dbp = st.number_input("Diastolic Blood Pressure (DBP)", min_value=30, max_value=130, value=80)
29
+ heart_rate = st.number_input("Heart Rate", min_value=30, max_value=200, value=75)
30
+ respiratory_rate = st.number_input("Respiratory Rate", min_value=5, max_value=60, value=18)
31
+ temperature_c = st.number_input("Temperature (°C)", min_value=30.0, max_value=45.0, value=36.5)
32
+ oxygen_saturation = st.slider("Oxygen Saturation (%)", min_value=50.0, max_value=100.0, value=98.0)
33
  gender = st.selectbox("Gender", options=["Male", "Female"])
 
 
 
 
34
 
35
+ # Auto-derived features
36
+ gender_encoded = 1 if gender == "Male" else 0
37
+ gender_male = gender_encoded
38
+ pulse_pressure = sbp - dbp
39
 
40
+ # Create input DataFrame with exactly the features expected by the model
41
+ input_data = pd.DataFrame([{
42
  "Age": age,
43
+ "SBP": sbp,
44
+ "DBP": dbp,
45
+ "HeartRate": heart_rate,
46
+ "RespiratoryRate": respiratory_rate,
47
+ "TemperatureC": temperature_c,
48
+ "OxygenSaturation(%)": oxygen_saturation,
49
+ "Gender_Encoded": gender_encoded,
50
+ "Pulse_Pressure": pulse_pressure,
51
+ "Gender_Male": gender_male
52
  }])
53
 
54
+ # Run prediction
55
  if st.button("Predict Hemoglobin Level"):
56
+ try:
57
+ input_scaled = scaler.transform(input_data)
58
+ prediction = model.predict(input_scaled)[0]
59
+ st.success(f"Predicted Hemoglobin Level: {prediction:.2f} g/dL")
60
+ except Exception as e:
61
+ st.error(f"Prediction failed: {e}")