-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
260 lines (175 loc) · 6.31 KB
/
app.py
File metadata and controls
260 lines (175 loc) · 6.31 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import streamlit as st
import pandas as pd
import pickle
import sqlite3
import plotly.express as px
import matplotlib.pyplot as plt
import seaborn as sns
from auth import login_signup
from database import create_db, save_prediction
# ---------------- PAGE CONFIG ----------------
st.set_page_config(
page_title="Student AI Analytics",
page_icon="🎓",
layout="wide"
)
# ---------------- LOAD MODEL ----------------
model = pickle.load(open("model.pkl", "rb"))
features = pickle.load(open("features.pkl", "rb"))
# ---------------- CREATE DATABASE ----------------
create_db()
# ---------------- SESSION STATE ----------------
if "logged_in" not in st.session_state:
st.session_state.logged_in = False
# ---------------- TITLE ----------------
st.title("🎓 Student Performance AI Platform")
# ---------------- LOGIN PAGE ----------------
if not st.session_state.logged_in:
login_signup()
else:
# ---------------- SIDEBAR ----------------
st.sidebar.success(f"Logged in as {st.session_state.user}")
if st.sidebar.button("Logout"):
st.session_state.logged_in = False
st.rerun()
page = st.sidebar.radio(
"Navigation",
["🏠 Dashboard", "📊 Predict Score", "📜 Prediction History", "📈 Analytics", "ℹ About"]
)
# ---------------- DASHBOARD ----------------
if page == "🏠 Dashboard":
st.header("Platform Overview")
try:
df = pd.read_csv("exams.csv")
except:
st.error("Dataset not found.")
st.stop()
col1, col2, col3, col4 = st.columns(4)
col1.metric("Total Students", df.shape[0])
col2.metric("Features", df.shape[1])
col3.metric("Avg Math Score", round(df["math score"].mean(), 2))
col4.metric("Max Math Score", df["math score"].max())
st.subheader("Dataset Preview")
st.dataframe(df.head(10), use_container_width=True)
# ---------------- MODEL COMPARISON ----------------
st.subheader("Machine Learning Model Comparison")
results = pd.DataFrame({
"Model": ["Linear Regression", "Decision Tree", "Random Forest"],
"MSE": [30.28, 71.54, 35.82],
"R2 Score": [0.8706, 0.6943, 0.8469]
})
st.dataframe(results, use_container_width=True)
fig = px.bar(
results,
x="Model",
y="R2 Score",
title="Machine Learning Model Comparison (R² Score)"
)
st.plotly_chart(fig, use_container_width=True)
best_model = results.loc[results["R2 Score"].idxmax()]
st.success(
f"🏆 Best Model: **{best_model['Model']}** with R² Score **{best_model['R2 Score']:.2f}**"
)
# ---------------- PREDICTION ----------------
elif page == "📊 Predict Score":
st.header("Predict Student Math Score")
col1, col2 = st.columns(2)
with col1:
reading = st.slider("Reading Score", 0, 100, 60)
writing = st.slider("Writing Score", 0, 100, 60)
with col2:
gender = st.selectbox("Gender", ["male", "female"])
lunch = st.selectbox("Lunch Type", ["standard", "free/reduced"])
input_df = pd.DataFrame(columns=features)
input_df.loc[0] = 0
input_df["reading score"] = reading
input_df["writing score"] = writing
input_df[f"gender_{gender}"] = 1
input_df[f"lunch_{lunch}"] = 1
if st.button("Predict Score"):
prediction = model.predict(input_df)[0]
st.success(f"🎯 Predicted Math Score: {prediction:.2f}")
save_prediction(
st.session_state.user,
reading,
writing,
prediction
)
fig = px.bar(
x=["Predicted Score"],
y=[prediction],
labels={"x": "Result", "y": "Score"},
title="Prediction Result"
)
st.plotly_chart(fig)
# ---------------- HISTORY ----------------
elif page == "📜 Prediction History":
st.header("Your Prediction History")
conn = sqlite3.connect("student.db")
query = "SELECT * FROM predictions WHERE username=?"
df = pd.read_sql_query(
query,
conn,
params=(st.session_state.user,)
)
conn.close()
if df.empty:
st.info("No predictions yet.")
else:
st.dataframe(df, use_container_width=True)
csv = df.to_csv(index=False)
st.download_button(
"Download Predictions",
csv,
"predictions.csv",
"text/csv"
)
# ---------------- ANALYTICS ----------------
elif page == "📈 Analytics":
st.header("Student Performance Analytics")
df = pd.read_csv("exams.csv")
col1, col2 = st.columns(2)
with col1:
fig = px.scatter(
df,
x="reading score",
y="math score",
title="Reading vs Math Score"
)
st.plotly_chart(fig)
with col2:
fig = px.scatter(
df,
x="writing score",
y="math score",
title="Writing vs Math Score"
)
st.plotly_chart(fig)
st.subheader("Subject Correlation")
corr = df[['math score', 'reading score', 'writing score']].corr()
fig2, ax = plt.subplots()
sns.heatmap(corr, annot=True, cmap="coolwarm", ax=ax)
st.pyplot(fig2)
# ---------------- ABOUT ----------------
elif page == "ℹ About":
st.header("About the Project")
st.write("""
**Student Performance AI Platform**
This platform predicts student math performance using machine learning.
**Technologies Used**
- Python
- Scikit-learn
- Streamlit
- SQLite
- Plotly
**Machine Learning Models Compared**
- Linear Regression
- Decision Tree
- Random Forest
**Features**
- AI score prediction
- Interactive analytics dashboard
- User authentication
- Prediction history
- Download reports
""")