Benny-Tang commited on
Commit
dc8d23a
·
verified ·
1 Parent(s): 4aeb6f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -51
app.py CHANGED
@@ -5,7 +5,6 @@ import gradio as gr
5
  from agents import AnalyzerAgent, CoachAgent, PredictiveAgent
6
 
7
  QUESTIONS_FILE = "questions.json"
8
- PREDICTIONS_CACHE = "predictions_cache.json"
9
 
10
  if not os.path.exists(QUESTIONS_FILE):
11
  with open(QUESTIONS_FILE, "w", encoding="utf-8") as f:
@@ -14,58 +13,26 @@ if not os.path.exists(QUESTIONS_FILE):
14
  with open(QUESTIONS_FILE, "r", encoding="utf-8") as f:
15
  QUESTION_BANK = json.load(f)
16
 
17
- if not os.path.exists(PREDICTIONS_CACHE):
18
- with open(PREDICTIONS_CACHE, "w", encoding="utf-8") as f:
19
- json.dump({}, f)
20
-
21
  analyzer = AnalyzerAgent()
22
  coach_agent = CoachAgent()
23
- predictor = PredictiveAgent(cache_path=PREDICTIONS_CACHE)
24
-
25
- def ensure_predictions_injected(level, subject, n=8):
26
- key = f"{level}_{subject}"
27
- with open(predictor.cache_path, "r", encoding="utf-8") as f:
28
- cache = json.load(f)
29
-
30
- if key in cache and cache[key].get("injected"):
31
- return
32
-
33
- preds = predictor.get_or_generate_predictions(level, subject, QUESTION_BANK, n=n)
34
-
35
- next_id = 900000
36
- existing_ids = {q["id"] for q in QUESTION_BANK}
37
- while next_id in existing_ids:
38
- next_id += 1
39
-
40
- for p in preds:
41
- QUESTION_BANK.append({
42
- "id": next_id,
43
- "subject": f"{level}_{subject}",
44
- "question_type": p.get("question_type", "mcq"),
45
- "text": p.get("text"),
46
- "choices": p.get("choices", []),
47
- "correct_answer": p.get("predicted_answer", ""),
48
- "topics": [p.get("topic", "general")],
49
- "difficulty": p.get("difficulty", 3),
50
- "source": "predicted"
51
- })
52
- next_id += 1
53
-
54
- cache[key] = {"predictions": preds, "injected": True}
55
- with open(predictor.cache_path, "w", encoding="utf-8") as f:
56
- json.dump(cache, f, indent=2, ensure_ascii=False)
57
 
58
  def start_exam(level, subject, num_questions=10, include_predicted=True):
59
- if include_predicted:
60
- ensure_predictions_injected(level, subject, n=8)
61
-
62
  pool = [q for q in QUESTION_BANK if q.get("subject") == f"{level}_{subject}"]
63
 
64
- if not pool:
 
 
 
 
 
 
 
65
  return [], gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), []
66
 
67
- random.shuffle(pool)
68
- selected = pool[:min(num_questions, len(pool))]
69
 
70
  exam_data = [
71
  {"id": q["id"], "text": q["text"], "choices": q.get("choices", []), "topics": q.get("topics", [])}
@@ -82,8 +49,14 @@ def submit_exam(answers, exam_data, level, subject):
82
  for q in exam_data:
83
  qid = str(q["id"])
84
  user_ans = answers.get(qid)
85
- orig = next((item for item in QUESTION_BANK if item["id"] == q["id"]), None)
86
- correct_ans = orig.get("correct_answer") if orig else None
 
 
 
 
 
 
87
  per_question[qid] = {"user": user_ans, "correct": correct_ans, "topics": q.get("topics", [])}
88
  if user_ans and correct_ans and str(user_ans).strip() == str(correct_ans).strip():
89
  correct += 1
@@ -93,10 +66,7 @@ def submit_exam(answers, exam_data, level, subject):
93
  analysis = analyzer.analyze(per_question)
94
  coach = coach_agent.coach(analysis, level, subject)
95
 
96
- with open(predictor.cache_path, "r", encoding="utf-8") as f:
97
- cache = json.load(f)
98
- pred_key = f"{level}_{subject}"
99
- predictions_summary = cache.get(pred_key, {})
100
 
101
  return (
102
  f"Your Score: {score}%",
@@ -161,3 +131,4 @@ if __name__ == "__main__":
161
 
162
 
163
 
 
 
5
  from agents import AnalyzerAgent, CoachAgent, PredictiveAgent
6
 
7
  QUESTIONS_FILE = "questions.json"
 
8
 
9
  if not os.path.exists(QUESTIONS_FILE):
10
  with open(QUESTIONS_FILE, "w", encoding="utf-8") as f:
 
13
  with open(QUESTIONS_FILE, "r", encoding="utf-8") as f:
14
  QUESTION_BANK = json.load(f)
15
 
 
 
 
 
16
  analyzer = AnalyzerAgent()
17
  coach_agent = CoachAgent()
18
+ predictor = PredictiveAgent()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  def start_exam(level, subject, num_questions=10, include_predicted=True):
21
+ # Filter only real past paper questions
 
 
22
  pool = [q for q in QUESTION_BANK if q.get("subject") == f"{level}_{subject}"]
23
 
24
+ # Optionally add predictions in memory (not saved)
25
+ predicted_questions = []
26
+ if include_predicted:
27
+ predicted_questions = predictor.generate_predictions(level, subject, n=8)
28
+
29
+ # Combine both pools
30
+ combined_pool = pool + predicted_questions
31
+ if not combined_pool:
32
  return [], gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), []
33
 
34
+ random.shuffle(combined_pool)
35
+ selected = combined_pool[:min(num_questions, len(combined_pool))]
36
 
37
  exam_data = [
38
  {"id": q["id"], "text": q["text"], "choices": q.get("choices", []), "topics": q.get("topics", [])}
 
49
  for q in exam_data:
50
  qid = str(q["id"])
51
  user_ans = answers.get(qid)
52
+ correct_ans = None
53
+
54
+ if q["id"] < 900000: # real past paper
55
+ orig = next((item for item in QUESTION_BANK if item["id"] == q["id"]), None)
56
+ correct_ans = orig.get("correct_answer") if orig else None
57
+ else: # predicted question
58
+ correct_ans = q.get("correct_answer")
59
+
60
  per_question[qid] = {"user": user_ans, "correct": correct_ans, "topics": q.get("topics", [])}
61
  if user_ans and correct_ans and str(user_ans).strip() == str(correct_ans).strip():
62
  correct += 1
 
66
  analysis = analyzer.analyze(per_question)
67
  coach = coach_agent.coach(analysis, level, subject)
68
 
69
+ predictions_summary = predictor.summary(level, subject)
 
 
 
70
 
71
  return (
72
  f"Your Score: {score}%",
 
131
 
132
 
133
 
134
+