KK00001 commited on
Commit
fc9ba0e
·
verified ·
1 Parent(s): d350c4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -28
app.py CHANGED
@@ -1,43 +1,38 @@
1
- import gradio as gr
2
  import numpy as np
3
  import joblib
4
  from sklearn import datasets
5
 
6
- # Load diabetes dataset just to get feature names (order must match training)
7
  diabetes = datasets.load_diabetes()
8
- feature_names = diabetes.feature_names # ['age', 'sex', 'bmi', 'bp', 's1', ..., 's6']
9
 
10
- # Load model and scaler ONCE (fast)
11
  model = joblib.load("lr.pkl")
12
  scaler = joblib.load("scaler.pkl")
13
 
14
- def predict(*features):
15
- # features = 10 numbers (age, sex, bmi, bp, s1...s6)
16
- x = np.array(features, dtype=float).reshape(1, -1)
 
 
 
 
 
 
 
 
 
17
  x_scaled = scaler.transform(x)
18
 
19
  y = model.predict(x_scaled)[0]
20
 
21
- # If it's a classification model (LogisticRegression)
22
  if hasattr(model, "predict_proba"):
23
- proba = model.predict_proba(x_scaled)[0]
24
- # assume binary classification
25
- label = "Positive (class 1)" if int(y) == 1 else "Negative (class 0)"
26
- return f"Prediction: {label}\nProbability of class 1: {proba[1]:.3f}"
 
27
  else:
28
- # Regression case (score)
29
- return f"Predicted disease progression score: {float(y):.2f}"
30
-
31
- # 10 numeric inputs (same order as training)
32
- inputs = [gr.Number(label=name) for name in feature_names]
33
-
34
- demo = gr.Interface(
35
- fn=predict,
36
- inputs=inputs,
37
- outputs=gr.Textbox(label="Result"),
38
- title="Diabetes Prediction (load_diabetes + lr + scaler)",
39
- description="Enter the 10 feature values in the same format used during training."
40
- )
41
-
42
- if __name__ == "__main__":
43
- demo.launch()
 
1
+ import streamlit as st
2
  import numpy as np
3
  import joblib
4
  from sklearn import datasets
5
 
6
+ # Get feature names from load_diabetes
7
  diabetes = datasets.load_diabetes()
8
+ feature_names = diabetes.feature_names
9
 
10
+ # Load model and scaler
11
  model = joblib.load("lr.pkl")
12
  scaler = joblib.load("scaler.pkl")
13
 
14
+ st.title("Diabetes Prediction (load_diabetes + lr + scaler)")
15
+ st.write("This app uses a saved model (lr.pkl) and scaler (scaler.pkl) to make predictions.")
16
+
17
+ inputs = []
18
+
19
+ st.subheader("Enter feature values")
20
+ for name in feature_names:
21
+ val = st.number_input(f"{name}", value=0.0, format="%.4f")
22
+ inputs.append(val)
23
+
24
+ if st.button("Predict"):
25
+ x = np.array(inputs, dtype=float).reshape(1, -1)
26
  x_scaled = scaler.transform(x)
27
 
28
  y = model.predict(x_scaled)[0]
29
 
 
30
  if hasattr(model, "predict_proba"):
31
+ proba = model.predict_proba(x_scaled)[0][1]
32
+ label = "Class 1 (Positive)" if int(y) == 1 else "Class 0 (Negative)"
33
+ st.subheader("Prediction")
34
+ st.write(label)
35
+ st.write(f"Probability of class 1: {proba:.3f}")
36
  else:
37
+ st.subheader("Predicted Disease Progression Score")
38
+ st.write(f"{float(y):.2f}")