# import streamlit as st # import pickle # import sklearn # from sklearn.preprocessing import RobustScaler, OneHotEncoder, LabelEncoder # from sklearn.neighbors import KNeighborsClassifier # import pandas as pd # import numpy as np # import matplotlib.pyplot as plt # # st.markdown(""" # # # # """, unsafe_allow_html=True) # st.title("Introvert/Extrovert Prediction App") # #with st.sidebar: # #st.header("Patient Information") # time_spent = st.number_input("🕒 Time Spent Alone",min_value=0,max_value=11,step=1) # stage_fear = st.selectbox("🎤 Stage Fear",["Yes","No"]) # social_event = st.number_input("🎉 Social Event Attendance",min_value=0,max_value=10,step=1) # going_outside = st.number_input("🚶‍♂️ Going Outside Frequency",min_value=0,max_value=7,step=1) # drained = st.selectbox("😓 Drained After Socializing",["Yes","No"]) # friends = st.number_input("👥 Friend Circle Size",min_value=0,max_value=15,step=1) # post_frequency = st.number_input("📱 Post Frequency on Social Media",min_value=0,max_value=10,step=1) # with open("rs.pkl", "rb") as f: # rs = pickle.load(f) # with open("ohe_drain.pkl", "rb") as f: # ohe_drain = pickle.load(f) # with open("ohe_stage.pkl", "rb") as f: # ohe_stage = pickle.load(f) # with open("le.pkl", "rb") as f: # le = pickle.load(f) # with open("knn.pkl", "rb") as f: # knn = pickle.load(f) # stage_encoded = ohe_stage.transform([[stage_fear]])[0] # gender encoded using one hot encoding # drain_encoded = ohe_drain.transform([[drained]])[0] # numeric_features = np.array([[time_spent, social_event, going_outside, friends, post_frequency]]) # scaled_features = rs.transform(numeric_features)[0] # st.write("Scaled Features:", scaled_features) # final_input = np.concatenate(( # scaled_features[:1], # stage_encoded, # scaled_features[1:3], # drain_encoded, # scaled_features[3:] # )).reshape(1, -1) # prediction_labels = { # 0: "Extrovert", # 1: "Introvert" # } # if st.button("🔍 Predict"): # prediction = knn.predict(final_input)[0] # result_label = prediction_labels.get(prediction, "Unknown") # # Styled result box # st.markdown( # f"
Predicted Personality: {result_label}
", # unsafe_allow_html=True # ) import streamlit as st import pickle import numpy as np # --------- CSS Styling --------- st.markdown(""" """, unsafe_allow_html=True) # --------- Title --------- st.markdown("
🧠 Introvert vs Extrovert Personality Predictor
", unsafe_allow_html=True) # --------- Input Form --------- with st.container(): st.markdown("
", unsafe_allow_html=True) st.markdown("#### 👤 Input Your Social Behavior Details") col1, col2 = st.columns(2) with col1: time_spent = st.slider("🕒 Time Spent Alone", 0, 11, 5) stage_fear = st.radio("🎤 Stage Fear", ["Yes", "No"]) going_outside = st.slider("🚶 Going Outside Frequency", 0, 7, 3) with col2: social_event = st.slider("🎉 Social Event Attendance", 0, 10, 5) drained = st.radio("😓 Drained After Socializing", ["Yes", "No"]) friends = st.slider("👥 Friend Circle Size", 0, 15, 7) post_frequency = st.slider("📱 Social Media Post Frequency", 0, 10, 3) # --------- Load Pickles --------- with open("rs.pkl", "rb") as f: rs = pickle.load(f) with open("ohe_drain.pkl", "rb") as f: ohe_drain = pickle.load(f) with open("ohe_stage.pkl", "rb") as f: ohe_stage = pickle.load(f) with open("le.pkl", "rb") as f: le = pickle.load(f) with open("knn.pkl", "rb") as f: knn = pickle.load(f) # --------- Encoding & Scaling --------- stage_encoded = ohe_stage.transform([[stage_fear]])[0] drain_encoded = ohe_drain.transform([[drained]])[0] numeric_features = np.array([[time_spent, social_event, going_outside, friends, post_frequency]]) scaled_features = rs.transform(numeric_features)[0] final_input = np.concatenate(( scaled_features[:1], stage_encoded, scaled_features[1:3], drain_encoded, scaled_features[3:] )).reshape(1, -1) prediction_labels = {0: "🌟 Extrovert", 1: "🌙 Introvert"} st.markdown("
", unsafe_allow_html=True) # Close glass card # --- Prediction Button & Result --- result_placeholder = st.empty() # 👈 Reserve space near the button if st.button("🔍 Predict", key="predict", help="Click to see your personality"): prediction = knn.predict(final_input)[0] # --> return 1d array further taking it as scalar value proba = knn.predict_proba(final_input)[0] # --> returns 2d array containing both probablity taking as 1d array. label = prediction_labels.get(prediction, "❓ Unknown") confidence = proba[prediction] * 100 result_html = f"""
🔮 Predicted Personality: {label}
📊 Confidence: {confidence:.2f}%
""" result_placeholder.markdown(result_html, unsafe_allow_html=True) # # --- Prediction Button & Result --- # result_placeholder = st.empty() # 👈 Reserve space near the button # if st.button("🔍 Predict", key="predict", help="Click to see your personality"): # prediction = knn.predict(final_input)[0] # proba = knn.predict_proba(final_input)[0] # label = prediction_labels.get(prediction, "❓ Unknown") # result_html = f""" #
# 🔮 Predicted Personality: {label}

# 📊 Probabilities:
# 🌟 Extrovert: {proba[0]*100:.2f}%
# 🌙 Introvert: {proba[1]*100:.2f}% #
# """ # result_placeholder.markdown(result_html, unsafe_allow_html=True) # # --- Prediction Button & Result --- # result_placeholder = st.empty() # 👈 Reserve space near the button # if st.button("🔍 Predict", key="predict", help="Click to see your personality"): # prediction = knn.predict(final_input)[0] # label = prediction_labels.get(prediction, "❓ Unknown") # result_html = f""" #
# 🔮 Predicted Personality: {label} #
# """ # result_placeholder.markdown(result_html, unsafe_allow_html=True) # 👈 result shows instantly in place