shubham680 commited on
Commit
3d0c5f4
ยท
verified ยท
1 Parent(s): 2515a07

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +193 -208
app.py CHANGED
@@ -1,254 +1,239 @@
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
 
 
33
 
34
 
35
 
36
 
37
 
38
 
39
- #with st.sidebar:
40
- #st.header("Patient Information")
41
- time_spent = st.number_input("๐Ÿ•’ 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("๐ŸŽ‰ Social Event Attendance",min_value=0,max_value=10,step=1)
44
- going_outside = st.number_input("๐Ÿšถโ€โ™‚๏ธ Going Outside Frequency",min_value=0,max_value=7,step=1)
45
- drained = st.selectbox("๐Ÿ˜“ Drained After Socializing",["Yes","No"])
46
- friends = st.number_input("๐Ÿ‘ฅ Friend Circle Size",min_value=0,max_value=15,step=1)
47
- post_frequency = st.number_input("๐Ÿ“ฑ Post Frequency on Social Media",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
- if st.button("๐Ÿ” Predict"):
131
- prediction = knn.predict(final_input)[0]
132
- result_label = prediction_labels.get(prediction, "Unknown")
133
-
134
- # Styled result box
135
- st.markdown(
136
- f"<div class='prediction-box'><strong>Predicted Personality:</strong> {result_label}</div>",
137
- unsafe_allow_html=True
138
- )
139
-
140
-
141
-
142
-
143
- # import streamlit as st
144
- # import pickle
145
- # import numpy as np
146
- # import pandas as pd
147
-
148
- # # Inject custom CSS
149
- # st.markdown("""
150
- # <style>
151
- # .stApp {
152
- # background-image: url('https://huggingface.co/spaces/shubham680/DiabetesPrediction/resolve/main/bg.jpg');
153
- # background-size: cover;
154
- # background-attachment: fixed;
155
- # }
156
- # .main-container {
157
- # background-color: rgba(255, 255, 255, 0.9);
158
- # padding: 2rem;
159
- # border-radius: 15px;
160
- # box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
161
- # }
162
- # .stButton>button {
163
- # background-color: #4CAF50;
164
- # color: white;
165
- # font-weight: bold;
166
- # padding: 0.5em 1em;
167
- # border-radius: 10px;
168
- # }
169
- # .prediction-box {
170
- # background-color: #1f77b4;
171
- # padding: 1em;
172
- # border-radius: 10px;
173
- # color: white;
174
- # font-size: 18px;
175
- # text-align: center;
176
- # }
177
- # </style>
178
- # """, unsafe_allow_html=True)
179
-
180
- # # Title
181
- # st.markdown("<h1 style='text-align: center;'>Introvert vs Extrovert Predictor</h1>", unsafe_allow_html=True)
182
-
183
- # with st.container():
184
- # #st.markdown("<div class='main-container'>", unsafe_allow_html=True)
185
-
186
- # # Create input layout with columns
187
- # col1, col2 = st.columns(2)
188
-
189
- # with col1:
190
- # time_spent = st.number_input("๐Ÿ•’ Time Spent Alone", min_value=0, max_value=11, step=1)
191
- # social_event = st.number_input("๐ŸŽ‰ Social Event Attendance", min_value=0, max_value=10, step=1)
192
- # going_outside = st.number_input("๐Ÿšถโ€โ™‚๏ธ Going Outside Frequency", min_value=0, max_value=7, step=1)
193
-
194
- # with col2:
195
- # stage_fear = st.selectbox("๐ŸŽค Stage Fear", ["Yes", "No"])
196
- # drained = st.selectbox("๐Ÿ˜“ Drained After Socializing", ["Yes", "No"])
197
- # friends = st.number_input("๐Ÿ‘ฅ Friend Circle Size", min_value=0, max_value=15, step=1)
198
- # post_frequency = st.number_input("๐Ÿ“ฑ Post Frequency on Social Media", min_value=0, max_value=10, step=1)
199
-
200
- # # Load models and encoders
201
- # with open("rs.pkl", "rb") as f:
202
- # rs = pickle.load(f)
203
-
204
- # with open("ohe_drain.pkl", "rb") as f:
205
- # ohe_drain = pickle.load(f)
206
-
207
- # with open("ohe_stage.pkl", "rb") as f:
208
- # ohe_stage = pickle.load(f)
209
-
210
- # with open("le.pkl", "rb") as f:
211
- # le = pickle.load(f)
212
-
213
- # with open("knn.pkl", "rb") as f:
214
- # knn = pickle.load(f)
215
-
216
- # # Encode categorical values
217
- # stage_encoded = ohe_stage.transform([[stage_fear]])[0] # shape (1,)
218
- # drain_encoded = ohe_drain.transform([[drained]])[0]
219
-
220
- # # Scale numeric input
221
- # numeric_features = np.array([[time_spent, social_event, going_outside, friends, post_frequency]])
222
- # scaled_features = rs.transform(numeric_features)[0]
223
-
224
-
225
-
226
- # # Final input
227
- # final_input = np.concatenate((
228
- # scaled_features[:1], # Time_spent_Alone
229
- # stage_encoded, # Stage_fear (1 col)
230
- # scaled_features[1:3], # Social_event, Going_outside
231
- # drain_encoded, # Drained_after_socializing (1 col)
232
- # scaled_features[3:] # Friends, Post_frequency
233
- # )).reshape(1, -1)
234
-
235
- # # Prediction labels
236
- # prediction_labels = {
237
- # 0: "Extrovert",
238
- # 1: "Introvert"
239
- # }
240
-
241
- # # Prediction trigger
242
- # if st.button("๐Ÿ” Predict"):
243
- # prediction = knn.predict(final_input)[0]
244
- # result_label = prediction_labels.get(prediction, "Unknown")
245
-
246
- # # Styled result box
247
- # st.markdown(
248
- # f"<div class='prediction-box'><strong>Predicted Personality:</strong> {result_label}</div>",
249
- # unsafe_allow_html=True
250
- # )
251
-
252
- # st.markdown("</div>", unsafe_allow_html=True)
253
 
 
 
 
254
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
 
33
 
34
 
35
 
 
 
 
 
 
 
 
 
 
36
 
37
 
38
 
39
+ # #with st.sidebar:
40
+ # #st.header("Patient Information")
41
+ # time_spent = st.number_input("๐Ÿ•’ 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("๐ŸŽ‰ Social Event Attendance",min_value=0,max_value=10,step=1)
44
+ # going_outside = st.number_input("๐Ÿšถโ€โ™‚๏ธ Going Outside Frequency",min_value=0,max_value=7,step=1)
45
+ # drained = st.selectbox("๐Ÿ˜“ Drained After Socializing",["Yes","No"])
46
+ # friends = st.number_input("๐Ÿ‘ฅ Friend Circle Size",min_value=0,max_value=15,step=1)
47
+ # post_frequency = st.number_input("๐Ÿ“ฑ Post Frequency on Social Media",min_value=0,max_value=10,step=1)
 
 
 
48
 
 
49
 
50
 
 
 
51
 
52
+ # with open("rs.pkl", "rb") as f:
53
+ # rs = pickle.load(f)
54
 
55
+ # with open("ohe_drain.pkl", "rb") as f:
56
+ # ohe_drain = pickle.load(f)
57
 
58
+ # with open("ohe_stage.pkl", "rb") as f:
59
+ # ohe_stage = pickle.load(f)
60
 
61
+ # with open("le.pkl", "rb") as f:
62
+ # le = pickle.load(f)
63
 
64
+ # with open("knn.pkl", "rb") as f:
65
+ # knn = pickle.load(f)
66
 
67
+ # stage_encoded = ohe_stage.transform([[stage_fear]])[0] # gender encoded using one hot encoding
68
+ # drain_encoded = ohe_drain.transform([[drained]])[0]
69
 
 
 
70
 
71
+ # numeric_features = np.array([[time_spent, social_event, going_outside, friends, post_frequency]])
72
+ # scaled_features = rs.transform(numeric_features)[0]
73
 
74
+ # st.write("Scaled Features:", scaled_features)
75
 
 
 
 
 
 
 
 
76
 
77
+ # final_input = np.concatenate((
78
+ # scaled_features[:1],
79
+ # stage_encoded,
80
+ # scaled_features[1:3],
81
+ # drain_encoded,
82
+ # scaled_features[3:]
83
+ # )).reshape(1, -1)
84
 
 
85
 
86
 
87
+ # prediction_labels = {
88
+ # 0: "Extrovert",
89
+ # 1: "Introvert"
90
+ # }
91
 
92
 
 
 
 
 
93
 
94
+ # # if st.button("Predict"):
95
+ # # prediction = knn.predict(final_input)[0]
96
+ # # result_label = prediction_labels.get(prediction, "Unknown")
97
+
98
+ # # # Custom result display
99
+ # # st.markdown(
100
+ # # f"""
101
+ # # <div style='background-color: #1f77b4; padding: 15px; border-radius: 10px;'>
102
+ # # <h4 style='color: white;'>Predicted Result: {result_label}</h4>
103
+ # # </div>
104
+ # # """,
105
+ # # unsafe_allow_html=True
106
+ # # )
107
 
108
 
109
 
110
+ # if st.button("๐Ÿ” Predict"):
111
  # prediction = knn.predict(final_input)[0]
112
  # result_label = prediction_labels.get(prediction, "Unknown")
113
+
114
+ # # Styled result box
115
  # st.markdown(
116
+ # f"<div class='prediction-box'><strong>Predicted Personality:</strong> {result_label}</div>",
 
 
 
 
117
  # unsafe_allow_html=True
118
  # )
119
 
120
 
121
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
 
123
+ import streamlit as st
124
+ import pickle
125
+ import numpy as np
126
 
127
+ # --------- CSS Styling ---------
128
+ st.markdown("""
129
+ <style>
130
+ .stApp {
131
+ background: linear-gradient(to right, #c9d6ff, #e2e2e2);
132
+ font-family: 'Segoe UI', sans-serif;
133
+ }
134
+ .glass-card {
135
+ background: rgba(255, 255, 255, 0.6);
136
+ padding: 2rem;
137
+ border-radius: 20px;
138
+ box-shadow: 0 8px 32px rgba(31, 38, 135, 0.37);
139
+ backdrop-filter: blur(10px);
140
+ -webkit-backdrop-filter: blur(10px);
141
+ border: 1px solid rgba(255, 255, 255, 0.18);
142
+ margin-top: 2rem;
143
+ }
144
+ .title {
145
+ font-size: 48px;
146
+ font-weight: bold;
147
+ text-align: center;
148
+ background: linear-gradient(to right, #141E30, #243B55);
149
+ -webkit-background-clip: text;
150
+ -webkit-text-fill-color: transparent;
151
+ }
152
+ .predict-button button {
153
+ background-color: #6c63ff;
154
+ color: white;
155
+ font-size: 18px;
156
+ font-weight: 600;
157
+ border-radius: 12px;
158
+ padding: 0.6em 1.5em;
159
+ margin-top: 1rem;
160
+ }
161
+ .result-box {
162
+ animation: fadeIn 1s ease-in-out;
163
+ background: #141E30;
164
+ color: white;
165
+ padding: 1.5em;
166
+ border-radius: 15px;
167
+ text-align: center;
168
+ margin-top: 2rem;
169
+ font-size: 22px;
170
+ }
171
+ @keyframes fadeIn {
172
+ from {opacity: 0;}
173
+ to {opacity: 1;}
174
+ }
175
+ </style>
176
+ """, unsafe_allow_html=True)
177
+
178
+ # --------- Title ---------
179
+ st.markdown("<div class='title'>๐Ÿง  Introvert vs Extrovert Personality Predictor</div>", unsafe_allow_html=True)
180
+
181
+ # --------- Input Form ---------
182
+ with st.container():
183
+ st.markdown("<div class='glass-card'>", unsafe_allow_html=True)
184
+
185
+ st.markdown("#### ๐Ÿ‘ค Input Your Social Behavior Details")
186
+
187
+ col1, col2 = st.columns(2)
188
+
189
+ with col1:
190
+ time_spent = st.slider("๐Ÿ•’ Time Spent Alone", 0, 11, 5)
191
+ stage_fear = st.radio("๐ŸŽค Stage Fear", ["Yes", "No"])
192
+ going_outside = st.slider("๐Ÿšถ Going Outside Frequency", 0, 7, 3)
193
+
194
+ with col2:
195
+ social_event = st.slider("๐ŸŽ‰ Social Event Attendance", 0, 10, 5)
196
+ drained = st.radio("๐Ÿ˜“ Drained After Socializing", ["Yes", "No"])
197
+ friends = st.slider("๐Ÿ‘ฅ Friend Circle Size", 0, 15, 7)
198
+ post_frequency = st.slider("๐Ÿ“ฑ Social Media Post Frequency", 0, 10, 3)
199
+
200
+ # --------- Load Pickles ---------
201
+ with open("rs.pkl", "rb") as f:
202
+ rs = pickle.load(f)
203
+ with open("ohe_drain.pkl", "rb") as f:
204
+ ohe_drain = pickle.load(f)
205
+ with open("ohe_stage.pkl", "rb") as f:
206
+ ohe_stage = pickle.load(f)
207
+ with open("le.pkl", "rb") as f:
208
+ le = pickle.load(f)
209
+ with open("knn.pkl", "rb") as f:
210
+ knn = pickle.load(f)
211
+
212
+ # --------- Encoding & Scaling ---------
213
+ stage_encoded = ohe_stage.transform([[stage_fear]])[0]
214
+ drain_encoded = ohe_drain.transform([[drained]])[0]
215
+
216
+ numeric_features = np.array([[time_spent, social_event, going_outside, friends, post_frequency]])
217
+ scaled_features = rs.transform(numeric_features)[0]
218
+
219
+ final_input = np.concatenate((
220
+ scaled_features[:1],
221
+ stage_encoded,
222
+ scaled_features[1:3],
223
+ drain_encoded,
224
+ scaled_features[3:]
225
+ )).reshape(1, -1)
226
+
227
+ prediction_labels = {0: "๐ŸŒŸ Extrovert", 1: "๐ŸŒ™ Introvert"}
228
+
229
+ st.markdown("</div>", unsafe_allow_html=True) # Close glass card
230
+
231
+ # --------- Prediction Button ---------
232
+ with st.container():
233
+ with st.container():
234
+ predict_btn = st.container()
235
+ with predict_btn:
236
+ if st.button("๐Ÿ” Predict", key="predict", help="Click to see your personality"):
237
+ prediction = knn.predict(final_input)[0]
238
+ label = prediction_labels.get(prediction, "โ“ Unknown")
239
+ st.markdown(f"<div class='result-box'>๐Ÿ”ฎ Predicted Personality: <strong>{label}</strong></div>", unsafe_allow_html=True)