-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquiz_app.py
More file actions
347 lines (306 loc) · 14.7 KB
/
quiz_app.py
File metadata and controls
347 lines (306 loc) · 14.7 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import streamlit as st
import pandas as pd
import altair as alt
import os
from datetime import datetime
from multiple_options import get_questions, load_questions
from ml_part import get_adaptive_quiz
# -------- Config ----------
st.set_page_config(page_title="IAS Quiz Platform", layout="wide")
RESULTS_FILE = "results.csv"
USERS_FILE = "users.csv"
RESOURCES_FILE = "resources.csv"
# -------- Init helper files/headers (safe) ----------
def ensure_file(path, header_cols):
if not os.path.exists(path):
pd.DataFrame(columns=header_cols).to_csv(path, index=False)
ensure_file(USERS_FILE, ["username", "fullname", "password"])
ensure_file(RESULTS_FILE, ["timestamp", "user", "subject", "subtopic", "score", "total"])
# -------- Session-state defaults ----------
for key, val in {
"logged_in": False, "username": None, "fullname": None,
"quiz_started": False, "questions": [], "answers": {}, "submitted": False
}.items():
if key not in st.session_state:
st.session_state[key] = val
# -------- Utility: safe read & normalize results.csv ----------
def read_results_safe():
expected_cols = ["timestamp", "user", "subject", "subtopic", "score", "total"]
if not os.path.exists(RESULTS_FILE):
return pd.DataFrame(columns=expected_cols)
try:
df = pd.read_csv(RESULTS_FILE)
if "username" in df.columns and "user" not in df.columns:
df = df.rename(columns={"username": "user"})
if set(expected_cols).issubset(df.columns):
df["score"] = pd.to_numeric(df["score"], errors="coerce").fillna(0).astype(int)
df["total"] = pd.to_numeric(df["total"], errors="coerce").fillna(0).astype(int)
df["timestamp"] = pd.to_datetime(df["timestamp"], errors="coerce")
return df[expected_cols]
except Exception:
lines = []
with open(RESULTS_FILE, "r", encoding="utf-8", errors="ignore") as f:
lines = [ln.rstrip("\n\r") for ln in f if ln.strip()]
if not lines:
return pd.DataFrame(columns=expected_cols)
header = lines[0].split(",")
data_lines = lines[1:]
rows = []
for ln in data_lines:
parts = ln.split(",")
if len(parts) == 6:
rows.append(parts)
elif len(parts) == 5:
rows.append([parts[0], "", parts[1], parts[2], parts[3], parts[4]])
elif len(parts) > 6:
user = ",".join(parts[1:-4])
subject, subtopic, score, total = parts[-4], parts[-3], parts[-2], parts[-1]
rows.append([parts[0], user, subject, subtopic, score, total])
else:
continue
df = pd.DataFrame(rows, columns=expected_cols)
df["score"] = pd.to_numeric(df["score"], errors="coerce").fillna(0).astype(int)
df["total"] = pd.to_numeric(df["total"], errors="coerce").fillna(0).astype(int)
df["timestamp"] = pd.to_datetime(df["timestamp"], errors="coerce")
return df
# -------- Auth: signup & login ----------
def signup_user(username, fullname, password):
users = pd.read_csv(USERS_FILE)
if username in users["username"].values:
return False, "Username already exists."
new = pd.DataFrame([[username, fullname, password]], columns=["username", "fullname", "password"])
users = pd.concat([users, new], ignore_index=True)
users.to_csv(USERS_FILE, index=False)
return True, "Signup successful."
def login_user(username, password):
users = pd.read_csv(USERS_FILE)
mask = (users["username"] == username) & (users["password"] == password)
if mask.any():
fullname = users.loc[mask, "fullname"].values[0]
return True, fullname
return False, None
# -------- Navigation (dynamic) ----------
if st.session_state.logged_in:
# Show welcome message above sidebar
st.markdown(f"### 👋 Welcome, {st.session_state.fullname}!")
menu = ["Quiz", "Resources", "Analytics", "Logout"]
else:
menu = ["Login", "Sign Up", "Resources"]
choice = st.sidebar.selectbox("Navigate to:", menu)
# -------- PAGE: Sign Up ----------
if choice == "Sign Up":
st.title("🆕 Create an account")
col1, col2 = st.columns(2)
with col1:
new_fullname = st.text_input("Full name")
new_username = st.text_input("Username")
with col2:
new_password = st.text_input("Password", type="password")
if st.button("Create account"):
if not (new_fullname and new_username and new_password):
st.warning("Fill all fields.")
else:
ok, msg = signup_user(new_username.strip(), new_fullname.strip(), new_password)
if not ok:
st.error(msg)
else:
st.success(msg)
st.info("Now go to Login and sign in.")
st.rerun()
# -------- PAGE: Login ----------
elif choice == "Login":
st.title("🔐 Login")
user_in = st.text_input("Username")
pwd_in = st.text_input("Password", type="password")
if st.button("Login"):
ok, fullname = login_user(user_in.strip(), pwd_in)
if ok:
st.session_state.logged_in = True
st.session_state.username = user_in.strip()
st.session_state.fullname = fullname
st.success(f"Welcome, {fullname}!")
st.rerun()
else:
st.error("Invalid username or password.")
# -------- PAGE: Logout ----------
elif choice == "Logout":
st.session_state.logged_in = False
st.session_state.username = None
st.session_state.fullname = None
st.success("Logged out.")
st.rerun()
# -------- PAGE: Resources ----------
elif choice == "Resources":
st.title("📚 IAS Preparation Resources")
if not os.path.exists(RESOURCES_FILE):
st.error("⚠️ 'resources.csv' file not found in current directory.")
else:
try:
res = pd.read_csv(RESOURCES_FILE)
if "subject" not in res.columns or "subtopic" not in res.columns:
st.error("resources.csv must contain columns: subject, subtopic, youtube_link/link")
else:
link_col = "youtube_link" if "youtube_link" in res.columns else ("link" if "link" in res.columns else None)
expected_cols = {"subject", "subtopic"}
if link_col:
expected_cols.add(link_col)
if not expected_cols.issubset(res.columns):
st.error(f"⚠️ The CSV must have these columns: {expected_cols}")
else:
for subj in sorted(res["subject"].dropna().unique()):
with st.expander(f"📘 {subj}"):
subset = res[res["subject"] == subj]
for _, row in subset.iterrows():
yt = row.get(link_col, "")
yt_link = f"[▶️ Watch Playlist]({yt})" if pd.notna(yt) and str(yt).strip() else ""
st.markdown(f"**{row['subtopic']}** — {yt_link}")
except Exception as e:
st.error(f"Error reading resources.csv: {e}")
# -------- PAGE: Quiz ----------
elif choice == "Quiz":
if not st.session_state.logged_in:
st.warning("Please log in first to take quizzes.")
st.stop()
st.title("🧠 IAS Adaptive Quiz Platform")
try:
master_df = load_questions()
except Exception as e:
st.error(f"Could not load questions dataset: {e}")
st.stop()
subjects = master_df["subject"].dropna().unique().tolist()
if not subjects:
st.error("No subjects found in question bank.")
st.stop()
subject = st.selectbox("Select Subject", subjects)
subtopics = master_df[master_df["subject"] == subject]["subtopic"].dropna().unique().tolist()
if not subtopics:
st.error("No subtopics found for this subject.")
st.stop()
subtopic = st.selectbox("Select Topic", subtopics)
num_qs = st.slider("Number of Questions", 5, 20, 10)
if st.button("Start Quiz"):
try:
qdf = get_questions(subject=subject, subtopic=subtopic, n=num_qs)
q_list = qdf.reset_index(drop=True).to_dict(orient="records") if isinstance(qdf, pd.DataFrame) else qdf
except Exception as e:
st.error(f"Error obtaining questions: {e}")
q_list = []
if not q_list:
st.warning("No questions available for the selected subject/topic.")
else:
st.session_state.quiz_started = True
st.session_state.questions = q_list
st.session_state.answers = {}
st.session_state.submitted = False
st.rerun()
if st.session_state.quiz_started and st.session_state.questions:
st.subheader(f"Quiz: {subject} — {subtopic}")
for idx, row in enumerate(st.session_state.questions):
qid = f"q{idx}"
st.write(f"**Q{idx+1}: {row.get('question','(no question text)')}**")
options = row.get("options") if isinstance(row.get("options"), (list, tuple)) else [
row.get(k) for k in ("optionA","optionB","optionC","optionD") if pd.notna(row.get(k))
]
if not options:
st.warning("No options found for this question.")
continue
# NO preselection
choice = st.radio("Choose:", options, key=qid, index=None)
if choice: # only save if user chooses
st.session_state.answers[qid] = choice
if st.button("Submit Quiz"):
st.session_state.submitted = True
if st.session_state.submitted:
score = 0
correct_easy = total_easy = 0
correct_med = total_med = 0
correct_hard = total_hard = 0
st.subheader("Review & Results")
for idx, row in enumerate(st.session_state.questions):
qid = f"q{idx}"
chosen = st.session_state.answers.get(qid, None)
ans_letter = str(row.get("answer","")).strip().upper()
correct_text = row.get(f"option{ans_letter}") if ans_letter in ("A","B","C","D") else row.get("correct") or row.get("answer_text") or row.get("answer")
d = int(row.get("difficulty",2))
if d==1: total_easy+=1
elif d==2: total_med+=1
elif d==3: total_hard+=1
if not chosen:
st.warning(f"Q{idx+1}: No answer selected. Correct: **{correct_text}**. {row.get('explanation','')}")
continue
if chosen == correct_text:
score += 1
if d==1: correct_easy+=1
elif d==2: correct_med+=1
elif d==3: correct_hard+=1
st.success(f"Q{idx+1}: ✅ Correct — {row.get('explanation','')}")
else:
st.error(f"Q{idx+1}: ❌ Wrong. You chose **{chosen}**. Correct: **{correct_text}**. {row.get('explanation','')}")
total_qs = len(st.session_state.questions)
st.markdown(f"### 🎯 Final Score: **{score}/{total_qs}** ({round(score/total_qs*100,2)}%)")
# Save result
result_row = {
"user": st.session_state.username,
"subject": subject,
"subtopic": subtopic,
"score": int(score),
"total": int(total_qs)
}
row_df = pd.DataFrame([result_row])
if not os.path.exists(RESULTS_FILE):
row_df.to_csv(RESULTS_FILE, index=False)
else:
row_df.to_csv(RESULTS_FILE, mode="a", index=False, header=False)
st.subheader("Performance by Difficulty")
st.write(f"Easy: {correct_easy}/{total_easy}")
st.write(f"Medium: {correct_med}/{total_med}")
st.write(f"Hard: {correct_hard}/{total_hard}")
try:
suggested = get_adaptive_quiz(master_df, score=score*100/total_qs)
if isinstance(suggested, pd.DataFrame) and not suggested.empty:
st.subheader("Suggested Next Quiz (sample)")
cols = [c for c in ["question","subject","subtopic","difficulty"] if c in suggested.columns]
st.dataframe(suggested[cols].reset_index(drop=True).head())
else:
st.info("No adaptive suggestion available right now. Great work!")
except Exception:
pass
# -------- PAGE: Analytics ----------
elif choice == "Analytics":
if not st.session_state.logged_in:
st.warning("Please log in to view analytics.")
st.stop()
st.title("📊 Progress Dashboard")
results = read_results_safe()
if results.empty:
st.info("No quiz results yet. Take a quiz to populate analytics.")
st.stop()
username = st.session_state.username
fullname = st.session_state.fullname
user_mask = (results["user"] == username) | (results["user"] == fullname)
user_results = results[user_mask].copy()
if user_results.empty:
st.info("No results found for your account.")
st.dataframe(results.sort_values(by="timestamp", ascending=False).head(10))
st.stop()
user_results["timestamp"] = pd.to_datetime(user_results["timestamp"], errors="coerce")
user_results["accuracy"] = (user_results["score"]/user_results["total"])*100
user_results = user_results.sort_values("timestamp")
st.subheader("Recent Results")
st.dataframe(user_results.assign(timestamp=user_results["timestamp"].dt.strftime("%d-%b-%Y %H:%M")).reset_index(drop=True).head(10))
score_chart = alt.Chart(user_results).mark_line(point=True).encode(
x=alt.X("timestamp:T", title="Time"),
y=alt.Y("score:Q", title="Score"),
color=alt.Color("subject:N", title="Subject")
).properties(title="Score Trend Over Time")
st.altair_chart(score_chart, use_container_width=True)
weak = user_results.groupby("subtopic")["accuracy"].mean().sort_values().head(8)
if not weak.empty:
weak_chart = alt.Chart(weak.reset_index()).mark_bar().encode(
x=alt.X("subtopic:N", sort="-y", title="Subtopic"),
y=alt.Y("accuracy:Q", title="Average Accuracy")
).properties(title="Weakest Topics (by avg accuracy)")
st.altair_chart(weak_chart, use_container_width=True)
st.subheader("Summary")
st.write(f"Quizzes taken: {len(user_results)}")
st.write(f"Average accuracy: {round(user_results['accuracy'].mean(),2)}%")