EdgarDataScientist commited on
Commit
69b2866
·
verified ·
1 Parent(s): be6bdd3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -0
app.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import required libraries
2
+ import streamlit as st
3
+ import pandas as pd
4
+ import numpy as np
5
+ from ucimlrepo import fetch_ucirepo
6
+ from sklearn.model_selection import train_test_split
7
+ from sklearn.preprocessing import StandardScaler
8
+ import torch
9
+ import torch.nn as nn
10
+ import torch.optim as optim
11
+
12
+ # Fetch the diabetes dataset (ID 891)
13
+ cdc_diabetes_health_indicators = fetch_ucirepo(id=891)
14
+
15
+ # Extract features and targets
16
+ X_raw = cdc_diabetes_health_indicators.data.features
17
+ y = cdc_diabetes_health_indicators.data.targets
18
+
19
+ # Combine features and targets into a single DataFrame
20
+ df = pd.concat([X_raw, y], axis=1)
21
+
22
+ # Drop duplicates if there are any
23
+ df.drop_duplicates(inplace=True)
24
+
25
+ # Features and target selection
26
+ X_raw = df[['AnyHealthcare', 'Sex', 'Smoker', 'MentHlth', 'CholCheck', 'Stroke',
27
+ 'PhysHlth', 'HeartDiseaseorAttack', 'Age', 'HighChol', 'DiffWalk',
28
+ 'BMI', 'HighBP', 'GenHlth']]
29
+ y = df['Diabetes_binary']
30
+
31
+ # Data Preprocessing
32
+ scaler = StandardScaler()
33
+ X = scaler.fit_transform(X_raw)
34
+
35
+ # Train-test split
36
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y, random_state=123)
37
+
38
+ # Convert to PyTorch tensors
39
+ X_train_tensor = torch.tensor(X_train, dtype=torch.float32)
40
+ X_test_tensor = torch.tensor(X_test, dtype=torch.float32)
41
+ y_train_tensor = torch.tensor(y_train.values, dtype=torch.float32).view(-1, 1)
42
+ y_test_tensor = torch.tensor(y_test.values, dtype=torch.float32).view(-1, 1)
43
+
44
+ # Define the PyTorch model
45
+ class NeuralNet(nn.Module):
46
+ def __init__(self, input_size, hidden_size, output_size):
47
+ super(NeuralNet, self).__init__()
48
+ self.fc1 = nn.Linear(input_size, hidden_size)
49
+ self.fc2 = nn.Linear(hidden_size, hidden_size)
50
+ self.fc3 = nn.Linear(hidden_size, output_size)
51
+ self.relu = nn.ReLU()
52
+ self.sigmoid = nn.Sigmoid()
53
+
54
+ def forward(self, x):
55
+ x = self.relu(self.fc1(x))
56
+ x = self.relu(self.fc2(x))
57
+ x = self.sigmoid(self.fc3(x))
58
+ return x
59
+
60
+ # Instantiate PyTorch model
61
+ input_size = X_train.shape[1]
62
+ hidden_size = 64
63
+ output_size = 1
64
+ deep_model = NeuralNet(input_size, hidden_size, output_size)
65
+
66
+ # Define loss and optimizer
67
+ criterion = nn.BCELoss()
68
+ optimizer = optim.Adam(deep_model.parameters(), lr=0.001)
69
+
70
+ # Skip training loop here for real-time prediction
71
+
72
+ # Title and description in Streamlit
73
+ st.title("BERTO AI😊 : Personalized Diabetes Treatment Predictor")
74
+ st.write("""
75
+ BERTO AI is a personalized diabetes prediction tool built to assist users in better understanding their health risks. Berto honors the legacy
76
+ of one of our founders dad ,Roberto Ferrer who unfortunately succumbed to Type 2 Diabetes .
77
+ This application uses a deep learning model powered by PyTorch to predict whether you may have diabetes based on various health indicators.
78
+ We aim to provide insights and suggestions for personalizing diabetes treatment. The Application allows you to choose between two options ,Yes and No
79
+ which have been give binary values .Also there is a slider for various features which have ranges as indicated . Berto is part of DiabeTrek Health
80
+ mission to help manage ,prevent and provide personalized treatment to Type 2 Diabetic Patients.
81
+ """)
82
+
83
+ # Input form for user to enter health information with descriptions
84
+ st.subheader("Enter Your Health Information")
85
+
86
+ AnyHealthcare = st.selectbox("Any Healthcare (1: Yes, 0: No)", [0, 1])
87
+ Sex = st.selectbox("Sex (1: Male, 0: Female)", [0, 1])
88
+ Smoker = st.selectbox("Smoker (1: Yes, 0: No)", [0, 1])
89
+ MentHlth = st.slider("Mental Health (Bad days in last 30 days)", 0, 30, 0)
90
+ CholCheck = st.selectbox("Cholesterol Check in Last 5 Years (1: Yes, 0: No)", [0, 1])
91
+ Stroke = st.selectbox("History of Stroke (1: Yes, 0: No)", [0, 1])
92
+ PhysHlth = st.slider("Physical Health (Bad days in last 30 days)", 0, 30, 0)
93
+ HeartDiseaseorAttack = st.selectbox("History of Heart Disease or Attack (1: Yes, 0: No)", [0, 1])
94
+ Age = st.slider("Age", 18, 100, 30)
95
+ HighChol = st.selectbox("High Cholesterol (1: Yes, 0: No)", [0, 1])
96
+ DiffWalk = st.selectbox("Difficulty Walking (1: Yes, 0: No)", [0, 1])
97
+ BMI = st.slider("BMI (Body Mass Index)", 10.0, 50.0, 25.0)
98
+ HighBP = st.selectbox("High Blood Pressure (1: Yes, 0: No)", [0, 1])
99
+ GenHlth = st.slider("General Health (1=Excellent, 5=Poor)", 1, 5, 3)
100
+
101
+ # Create a feature array from the inputs
102
+ user_input = np.array([[AnyHealthcare, Sex, Smoker, MentHlth, CholCheck, Stroke,
103
+ PhysHlth, HeartDiseaseorAttack, Age, HighChol, DiffWalk,
104
+ BMI, HighBP, GenHlth]])
105
+
106
+ # Convert the NumPy array to a DataFrame with the same column names used in the scaler
107
+ user_input_df = pd.DataFrame(user_input, columns=X_raw.columns)
108
+
109
+ # Standardize the user input using the same scaler
110
+ user_input_scaled = scaler.transform(user_input_df)
111
+
112
+ # Convert user input to PyTorch tensor
113
+ user_input_tensor = torch.tensor(user_input_scaled, dtype=torch.float32)
114
+
115
+ # Perform prediction when the user clicks "Predict"
116
+ if st.button("Predict"):
117
+ with torch.no_grad():
118
+ deep_model.eval() # Set model to evaluation mode
119
+ prediction = deep_model(user_input_tensor).round().numpy()
120
+
121
+ # Display prediction result
122
+ if prediction == 1:
123
+ st.success("The model predicts that you **may have diabetes**.")
124
+ else:
125
+ st.success("The model predicts that you are **unlikely to have diabetes**.")