-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
173 lines (142 loc) · 5.65 KB
/
app.py
File metadata and controls
173 lines (142 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import streamlit as st
import pandas as pd
import joblib
# Load Data and Model components
@st.cache_data
def load_data():
# Ensure this filename matches your GitHub repo file
return pd.read_csv('Student_performance_data.csv')
# Try to load model and scaler
try:
model = joblib.load('model.joblib')
scaler = joblib.load('scaler.joblib')
except Exception as e:
st.error(f"Error loading model artifacts: {e}")
st.warning("Please ensure model.joblib and scaler.joblib are in the same folder as app.py.")
st.stop() # Stop execution if model fails to load
# --- Page Configuration ---
st.set_page_config(
page_title="Student GPA Predictor",
page_icon="🎓",
layout="centered"
)
# --- Main Title and Description ---
st.title("🎓 Student GPA Predictor")
st.markdown("""
This application uses a machine learning model to estimate a student's Grade Point Average (GPA) based on various academic, demographic, and lifestyle factors.
Fill in the details below to generate a prediction.
""")
st.markdown("---")
# --- Sidebar Statistics ---
with st.sidebar:
st.header("📊 GPA Statistics")
st.markdown("Contextual data on average high school GPAs (4.0 scale):")
st.subheader("By Demographics")
st.markdown("""
* **Overall Average:** ~3.00
* **Female Students:** 3.10
* **Male Students:** 2.90
* **Asian/Pacific Islander:** 3.26
* **White:** 3.09
* **Hispanic:** 2.84
* **Black:** 2.69
""")
st.subheader("By Parental Education")
st.markdown("""
* **High School Diploma:** ~2.60 - 2.80
* **Bachelor's Degree:** ~3.00 - 3.20
* **Graduate Degree:** ~3.20 - 3.40
""")
st.caption("Source: National Center for Education Statistics (NCES) & Recent Academic Studies")
# --- Input Form ---
st.header("📝 Student Profile")
with st.form("prediction_form"):
# Group 1: Demographics & Background
st.subheader("1. Demographics & Background")
col1, col2 = st.columns(2)
with col1:
age = st.slider("Age", 15, 18, 17)
gender = st.selectbox("Gender", [0, 1], format_func=lambda x: "Male" if x==0 else "Female")
with col2:
ethnicity = st.selectbox(
"Ethnicity",
[0, 1, 2, 3],
format_func=lambda x: ["Caucasian", "African American", "Asian", "Other"][x]
)
parent_edu = st.selectbox(
"Parental Education Level",
[0, 1, 2, 3, 4],
format_func=lambda x: ["None", "High School", "Some College", "Bachelor's", "Higher"][x]
)
st.markdown("---")
# Group 2: Academics & Habits
st.subheader("2. Academic Habits")
col3, col4 = st.columns(2)
with col3:
study_time = st.number_input("Weekly Study Time (Hours)", 0.0, 20.0, 10.0, step=0.5)
absences = st.number_input("Total Absences (School Year)", 0, 30, 5)
tutoring = st.radio("Receives Tutoring?", [0, 1], format_func=lambda x: "No" if x==0 else "Yes", horizontal=True)
with col4:
parent_support = st.selectbox(
"Parental Support",
[0, 1, 2, 3, 4],
format_func=lambda x: ["None", "Low", "Moderate", "High", "Very High"][x]
)
st.markdown("---")
# Group 3: Activities
st.subheader("3. Extracurricular Activities")
st.markdown("Check all that apply:")
col5, col6, col7, col8 = st.columns(4)
with col5:
extracurricular = st.checkbox("General Extracurriculars")
with col6:
sports = st.checkbox("Sports Team")
with col7:
music = st.checkbox("Music/Arts")
with col8:
volunteering = st.checkbox("Volunteering")
# Convert checkboxes to 0/1 for model
extracurricular = 1 if extracurricular else 0
sports = 1 if sports else 0
music = 1 if music else 0
volunteering = 1 if volunteering else 0
st.markdown("###")
# Submit Button
submitted = st.form_submit_button("🔮 Predict GPA", type="primary", use_container_width=True)
# --- Prediction Logic ---
if submitted:
# 1. Arrange inputs EXACTLY as model expects
input_features = [
age, gender, ethnicity, parent_edu, study_time, absences,
tutoring, parent_support, extracurricular, sports, music, volunteering
]
# 2. Create DataFrame
input_df = pd.DataFrame([input_features], columns=[
'Age', 'Gender', 'Ethnicity', 'ParentalEducation', 'StudyTimeWeekly', 'Absences',
'Tutoring', 'ParentalSupport', 'Extracurricular', 'Sports', 'Music', 'Volunteering'
])
# 3. Predict
try:
input_scaled = scaler.transform(input_df)
prediction = model.predict(input_scaled)[0]
# Clamp prediction between 0.0 and 4.0
prediction = max(0.0, min(4.0, prediction))
st.markdown("---")
st.subheader("Prediction Result")
# Create columns for result and visual
res_col1, res_col2 = st.columns([1, 2])
with res_col1:
st.metric(label="Estimated GPA", value=f"{prediction:.2f}")
with res_col2:
st.write("GPA Scale (0.0 - 4.0)")
st.progress(prediction / 4.0)
if prediction >= 3.5:
st.success("🌟 Excellent! Keep up the great work!")
elif prediction >= 3.0:
st.info("✅ Good standing. Consistent effort pays off.")
elif prediction >= 2.0:
st.warning("⚠️ Average. Consider focusing on study habits or tutoring.")
else:
st.error("🚨 At Risk. Intervention or extra support may be needed.")
except Exception as e:
st.error(f"An error occurred during prediction: {e}")