shubham680 commited on
Commit
ed0ad1b
Β·
verified Β·
1 Parent(s): 7a6706a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +200 -109
app.py CHANGED
@@ -1,32 +1,32 @@
1
- import streamlit as st
2
- import pickle
3
- import sklearn
4
- from sklearn.preprocessing import RobustScaler, OneHotEncoder, LabelEncoder
5
- from sklearn.neighbors import KNeighborsClassifier
6
- import pandas as pd
7
- import numpy as np
8
- import matplotlib.pyplot as plt
9
 
10
 
11
 
12
- # st.markdown("""
13
- # <style>
14
- # .stApp {
15
- # background-image: url('https://huggingface.co/spaces/shubham680/DiabetesPrediction/resolve/main/bg.jpg');
16
- # background-size: cover;
17
- # background-repeat: no-repeat;
18
- # background-attachment: fixed;
19
- # }
20
- # .stTitle {
21
- # color: #ffffff;
22
- # font-size: 36px;
23
- # font-weight: bold;
24
- # text-align: center;
25
- # }
26
- # </style>
27
- # """, unsafe_allow_html=True)
28
 
29
- st.title("Introvert/Extrovert Prediction App")
30
 
31
 
32
 
@@ -36,118 +36,209 @@ st.title("Introvert/Extrovert Prediction App")
36
 
37
 
38
 
39
- #with st.sidebar:
40
- #st.header("Patient Information")
41
- time_spent = st.number_input("Enter Time_spent_Alone:",min_value=0,max_value=11,step=1)
42
- stage_fear = st.selectbox("Stage Fear:",["Yes","No"])
43
- social_event = st.number_input("Enter Social Event Frequency:",min_value=0,max_value=10,step=1)
44
- going_outside = st.number_input("Enter Going Frequency:",min_value=0,max_value=7,step=1)
45
- drained = st.selectbox("Drained After Socializing:",["Yes","No"])
46
- friends = st.number_input("Enter Friend Circle Size:",min_value=0,max_value=15,step=1)
47
- post_frequency = st.number_input("Enter Post Frequency:",min_value=0,max_value=10,step=1)
48
 
49
 
50
 
51
- # def plot_health_metrics(inputs):
52
- # labels = ["Age", "BMI", "Urea", "Creatinine", "HbA1c", "Cholesterol", "Triglycerides", "HDL", "LDL", "VLDL"]
53
- # standard_ranges = [
54
- # (20, 80), (18.5, 24.9), (2.3, 7.0), (0.6, 1.2), (4.0, 5.6), (125, 200), (40, 150), (40, 60), (50, 100), (5, 40)
55
- # ]
56
- # fig, ax = plt.subplots(figsize=(10, 6))
57
- # ax.barh(labels, [inputs[i] for i in range(10)], color='skyblue')
58
- # for i, (low, high) in enumerate(standard_ranges):
59
- # ax.plot([low, high], [i, i], color='red', linewidth=2)
60
- # ax.set_xlabel("Value")
61
- # ax.set_title("Health Metrics vs Standard Ranges")
62
- # st.pyplot(fig)
63
 
64
- # plot_health_metrics([age, bmi, urea, cr, HbA1c, chol, tg, hdl, ldl, vldl])
65
 
66
 
67
- with open("rs.pkl", "rb") as f:
68
- rs = pickle.load(f)
69
 
70
- with open("ohe_drain.pkl", "rb") as f:
71
- ohe_drain = pickle.load(f)
72
 
73
- with open("ohe_stage.pkl", "rb") as f:
74
- ohe_stage = pickle.load(f)
75
 
76
- with open("le.pkl", "rb") as f:
77
- le = pickle.load(f)
78
 
79
- with open("knn.pkl", "rb") as f:
80
- knn = pickle.load(f)
81
 
82
- stage_encoded = ohe_stage.transform([[stage_fear]])[0] # gender encoded using one hot encoding
83
- drain_encoded = ohe_drain.transform([[drained]])[0]
84
 
85
 
86
- numeric_features = np.array([[time_spent, social_event, going_outside, friends, post_frequency]])
87
- scaled_features = rs.transform(numeric_features)[0]
88
 
89
- st.write("Scaled Features:", scaled_features)
90
 
91
 
92
- final_input = np.concatenate((
93
- scaled_features[:1],
94
- stage_encoded,
95
- scaled_features[1:3],
96
- drain_encoded,
97
- scaled_features[3:]
98
- )).reshape(1, -1)
99
 
100
 
101
- #input_data = np.array([[gender_encoded, age, urea, cr, HbA1c, chol, tg, hdl, ldl, vldl, bmi]])
102
 
103
 
104
 
105
 
106
- prediction_labels = {
107
- 0: "Extrovert",
108
- 1: "Introvert"
109
- }
110
-
111
- # if st.button("Predict"):
112
- # prediction = knn.predict(final_input)[0]
113
- # result_label = prediction_labels.get(prediction, "Unknown")
114
- # st.success(f"Predicted Personality: {result_label}")
115
- # if result_label == "Pre-Diabetic":
116
- # st.warning("You are in the pre-diabetic range. It's advisable to consult a healthcare professional for further evaluation.")
117
- # elif result_label == "Diabetic":
118
- # st.error("You are classified as diabetic. Please seek medical advice for appropriate management.")
119
-
120
-
121
-
122
- if st.button("Predict"):
123
- prediction = knn.predict(final_input)[0]
124
- result_label = prediction_labels.get(prediction, "Unknown")
125
-
126
- # Custom result display
127
- st.markdown(
128
- f"""
129
- <div style='background-color: #1f77b4; padding: 15px; border-radius: 10px;'>
130
- <h4 style='color: white;'>Predicted Result: {result_label}</h4>
131
- </div>
132
- """,
133
- unsafe_allow_html=True
134
- )
135
-
136
- # # Message based on result
137
- # if result_label == "Pre-Diabetic":
138
- # st.warning("You are in the pre-diabetic range. It's advisable to consult a healthcare professional for further evaluation.")
139
- # elif result_label == "Diabetic":
140
- # st.error("You are classified as diabetic. Please seek medical advice for appropriate management.")
141
-
142
 
143
 
144
 
145
 
146
  # if st.button("Predict"):
147
  # prediction = knn.predict(final_input)[0]
148
- # result_label = prediction_labels.get(prediction,"Unknown")
149
- # st.success(f"Predicted Result: {result_label}")
150
- # #st.write("Predicted Diabetes Status :",prediction[0])
151
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
 
153
 
 
1
+ # import streamlit as st
2
+ # import pickle
3
+ # import sklearn
4
+ # from sklearn.preprocessing import RobustScaler, OneHotEncoder, LabelEncoder
5
+ # from sklearn.neighbors import KNeighborsClassifier
6
+ # import pandas as pd
7
+ # import numpy as np
8
+ # import matplotlib.pyplot as plt
9
 
10
 
11
 
12
+ # # st.markdown("""
13
+ # # <style>
14
+ # # .stApp {
15
+ # # background-image: url('https://huggingface.co/spaces/shubham680/DiabetesPrediction/resolve/main/bg.jpg');
16
+ # # background-size: cover;
17
+ # # background-repeat: no-repeat;
18
+ # # background-attachment: fixed;
19
+ # # }
20
+ # # .stTitle {
21
+ # # color: #ffffff;
22
+ # # font-size: 36px;
23
+ # # font-weight: bold;
24
+ # # text-align: center;
25
+ # # }
26
+ # # </style>
27
+ # # """, unsafe_allow_html=True)
28
 
29
+ # st.title("Introvert/Extrovert Prediction App")
30
 
31
 
32
 
 
36
 
37
 
38
 
39
+ # #with st.sidebar:
40
+ # #st.header("Patient Information")
41
+ # time_spent = st.number_input("Enter Time_spent_Alone:",min_value=0,max_value=11,step=1)
42
+ # stage_fear = st.selectbox("Stage Fear:",["Yes","No"])
43
+ # social_event = st.number_input("Enter Social Event Frequency:",min_value=0,max_value=10,step=1)
44
+ # going_outside = st.number_input("Enter Going Frequency:",min_value=0,max_value=7,step=1)
45
+ # drained = st.selectbox("Drained After Socializing:",["Yes","No"])
46
+ # friends = st.number_input("Enter Friend Circle Size:",min_value=0,max_value=15,step=1)
47
+ # post_frequency = st.number_input("Enter Post Frequency:",min_value=0,max_value=10,step=1)
48
 
49
 
50
 
51
+ # # def plot_health_metrics(inputs):
52
+ # # labels = ["Age", "BMI", "Urea", "Creatinine", "HbA1c", "Cholesterol", "Triglycerides", "HDL", "LDL", "VLDL"]
53
+ # # standard_ranges = [
54
+ # # (20, 80), (18.5, 24.9), (2.3, 7.0), (0.6, 1.2), (4.0, 5.6), (125, 200), (40, 150), (40, 60), (50, 100), (5, 40)
55
+ # # ]
56
+ # # fig, ax = plt.subplots(figsize=(10, 6))
57
+ # # ax.barh(labels, [inputs[i] for i in range(10)], color='skyblue')
58
+ # # for i, (low, high) in enumerate(standard_ranges):
59
+ # # ax.plot([low, high], [i, i], color='red', linewidth=2)
60
+ # # ax.set_xlabel("Value")
61
+ # # ax.set_title("Health Metrics vs Standard Ranges")
62
+ # # st.pyplot(fig)
63
 
64
+ # # plot_health_metrics([age, bmi, urea, cr, HbA1c, chol, tg, hdl, ldl, vldl])
65
 
66
 
67
+ # with open("rs.pkl", "rb") as f:
68
+ # rs = pickle.load(f)
69
 
70
+ # with open("ohe_drain.pkl", "rb") as f:
71
+ # ohe_drain = pickle.load(f)
72
 
73
+ # with open("ohe_stage.pkl", "rb") as f:
74
+ # ohe_stage = pickle.load(f)
75
 
76
+ # with open("le.pkl", "rb") as f:
77
+ # le = pickle.load(f)
78
 
79
+ # with open("knn.pkl", "rb") as f:
80
+ # knn = pickle.load(f)
81
 
82
+ # stage_encoded = ohe_stage.transform([[stage_fear]])[0] # gender encoded using one hot encoding
83
+ # drain_encoded = ohe_drain.transform([[drained]])[0]
84
 
85
 
86
+ # numeric_features = np.array([[time_spent, social_event, going_outside, friends, post_frequency]])
87
+ # scaled_features = rs.transform(numeric_features)[0]
88
 
89
+ # st.write("Scaled Features:", scaled_features)
90
 
91
 
92
+ # final_input = np.concatenate((
93
+ # scaled_features[:1],
94
+ # stage_encoded,
95
+ # scaled_features[1:3],
96
+ # drain_encoded,
97
+ # scaled_features[3:]
98
+ # )).reshape(1, -1)
99
 
100
 
101
+ # #input_data = np.array([[gender_encoded, age, urea, cr, HbA1c, chol, tg, hdl, ldl, vldl, bmi]])
102
 
103
 
104
 
105
 
106
+ # prediction_labels = {
107
+ # 0: "Extrovert",
108
+ # 1: "Introvert"
109
+ # }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
 
112
 
113
 
114
  # if st.button("Predict"):
115
  # prediction = knn.predict(final_input)[0]
116
+ # result_label = prediction_labels.get(prediction, "Unknown")
 
 
117
 
118
+ # # Custom result display
119
+ # st.markdown(
120
+ # f"""
121
+ # <div style='background-color: #1f77b4; padding: 15px; border-radius: 10px;'>
122
+ # <h4 style='color: white;'>Predicted Result: {result_label}</h4>
123
+ # </div>
124
+ # """,
125
+ # unsafe_allow_html=True
126
+ # )
127
+
128
+
129
+
130
+ import streamlit as st
131
+ import pickle
132
+ import numpy as np
133
+ import pandas as pd
134
+
135
+ # Inject custom CSS
136
+ st.markdown("""
137
+ <style>
138
+ .stApp {
139
+ background-image: url('https://huggingface.co/spaces/shubham680/DiabetesPrediction/resolve/main/bg.jpg');
140
+ background-size: cover;
141
+ background-attachment: fixed;
142
+ }
143
+ .main-container {
144
+ background-color: rgba(255, 255, 255, 0.9);
145
+ padding: 2rem;
146
+ border-radius: 15px;
147
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
148
+ }
149
+ .stButton>button {
150
+ background-color: #4CAF50;
151
+ color: white;
152
+ font-weight: bold;
153
+ padding: 0.5em 1em;
154
+ border-radius: 10px;
155
+ }
156
+ .prediction-box {
157
+ background-color: #1f77b4;
158
+ padding: 1em;
159
+ border-radius: 10px;
160
+ color: white;
161
+ font-size: 18px;
162
+ text-align: center;
163
+ }
164
+ </style>
165
+ """, unsafe_allow_html=True)
166
+
167
+ # Title
168
+ st.markdown("<h1 style='text-align: center;'>Introvert vs Extrovert Predictor</h1>", unsafe_allow_html=True)
169
+
170
+ with st.container():
171
+ st.markdown("<div class='main-container'>", unsafe_allow_html=True)
172
+
173
+ # Create input layout with columns
174
+ col1, col2 = st.columns(2)
175
+
176
+ with col1:
177
+ time_spent = st.number_input("πŸ•’ Time Spent Alone", min_value=0, max_value=11, step=1)
178
+ social_event = st.number_input("πŸŽ‰ Social Event Attendance", min_value=0, max_value=10, step=1)
179
+ going_outside = st.number_input("πŸšΆβ€β™‚οΈ Going Outside Frequency", min_value=0, max_value=7, step=1)
180
+
181
+ with col2:
182
+ stage_fear = st.selectbox("🎀 Stage Fear", ["Yes", "No"])
183
+ drained = st.selectbox("πŸ˜“ Drained After Socializing", ["Yes", "No"])
184
+ friends = st.number_input("πŸ‘₯ Friend Circle Size", min_value=0, max_value=15, step=1)
185
+ post_frequency = st.number_input("πŸ“± Post Frequency on Social Media", min_value=0, max_value=10, step=1)
186
+
187
+ # Load models and encoders
188
+ with open("rs.pkl", "rb") as f:
189
+ rs = pickle.load(f)
190
+
191
+ with open("ohe_drain.pkl", "rb") as f:
192
+ ohe_drain = pickle.load(f)
193
+
194
+ with open("ohe_stage.pkl", "rb") as f:
195
+ ohe_stage = pickle.load(f)
196
+
197
+ with open("le.pkl", "rb") as f:
198
+ le = pickle.load(f)
199
+
200
+ with open("knn.pkl", "rb") as f:
201
+ knn = pickle.load(f)
202
+
203
+ # Encode categorical values
204
+ stage_encoded = ohe_stage.transform([[stage_fear]])[0] # shape (1,)
205
+ drain_encoded = ohe_drain.transform([[drained]])[0]
206
+
207
+ # Scale numeric input
208
+ numeric_features = np.array([[time_spent, social_event, going_outside, friends, post_frequency]])
209
+ scaled_features = rs.transform(numeric_features)[0]
210
+
211
+ # Debug: show scaled values
212
+ st.markdown("### πŸ§ͺ Scaled Feature Values")
213
+ feature_names = ["Time_spent_Alone", "Social_event", "Going_outside", "Friends", "Post_frequency"]
214
+ st.json({name: val for name, val in zip(feature_names, scaled_features)})
215
+
216
+ # Final input
217
+ final_input = np.concatenate((
218
+ scaled_features[:1], # Time_spent_Alone
219
+ stage_encoded, # Stage_fear (1 col)
220
+ scaled_features[1:3], # Social_event, Going_outside
221
+ drain_encoded, # Drained_after_socializing (1 col)
222
+ scaled_features[3:] # Friends, Post_frequency
223
+ )).reshape(1, -1)
224
+
225
+ # Prediction labels
226
+ prediction_labels = {
227
+ 0: "Extrovert",
228
+ 1: "Introvert"
229
+ }
230
+
231
+ # Prediction trigger
232
+ if st.button("πŸ” Predict"):
233
+ prediction = knn.predict(final_input)[0]
234
+ result_label = prediction_labels.get(prediction, "Unknown")
235
+
236
+ # Styled result box
237
+ st.markdown(
238
+ f"<div class='prediction-box'><strong>Predicted Personality:</strong> {result_label}</div>",
239
+ unsafe_allow_html=True
240
+ )
241
+
242
+ st.markdown("</div>", unsafe_allow_html=True)
243
 
244