Benny-Tang commited on
Commit
079930c
·
verified ·
1 Parent(s): d603172

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -26
app.py CHANGED
@@ -7,26 +7,26 @@ from agents import AnalyzerAgent, ForecastAgent, CoachAgent
7
  with open("questions.json", "r", encoding="utf-8") as f:
8
  QUESTION_BANK = json.load(f)
9
 
10
- # Agents
11
  analyzer = AnalyzerAgent()
12
  forecast_agent = ForecastAgent()
13
  coach_agent = CoachAgent()
14
 
15
- # State
16
  def start_exam(level, subject, num_questions=5):
17
- # Filter questions
18
  questions = [q for q in QUESTION_BANK if q["subject"] == f"{level}_{subject}"]
19
  selected = random.sample(questions, min(num_questions, len(questions)))
20
 
21
- # Keep only necessary fields for UI
22
  exam_data = [
23
  {"id": q["id"], "text": q["text"], "choices": q.get("choices", [])}
24
  for q in selected
25
  ]
26
  return exam_data, gr.update(visible=True), gr.update(visible=False)
27
 
 
28
  def submit_exam(answers, exam_data, level, subject):
29
- # Score
30
  correct = 0
31
  per_question = {}
32
  for q in exam_data:
@@ -39,13 +39,9 @@ def submit_exam(answers, exam_data, level, subject):
39
 
40
  score = round(100 * correct / len(exam_data), 2)
41
 
42
- # Run Analyzer
43
- analysis = analyzer.analyze(per_question, QUESTION_BANK)
44
-
45
- # Call Forecast Agent (GLM-4.5)
46
  forecast = forecast_agent.forecast(level, subject)
47
-
48
- # Call Coach Agent (GLM-4.5)
49
  coach = coach_agent.coach(analysis, level, subject)
50
 
51
  return (
@@ -59,45 +55,52 @@ def submit_exam(answers, exam_data, level, subject):
59
 
60
  # Gradio UI
61
  with gr.Blocks() as demo:
62
- gr.Markdown("## 📘 Pay Fong Exam Simulator (AI-Powered)")
63
-
64
  with gr.Row():
65
- level = gr.Dropdown(["Form2", "Form3", "Higher1", "Higher2", "Higher3"], label="Select Level")
66
- subject = gr.Dropdown(["Math", "Science", "Physics", "Chemistry", "Biology"], label="Select Subject")
 
 
 
 
67
  start_btn = gr.Button("Start Exam")
68
-
69
  exam_output = gr.State()
70
- answers_state = gr.State()
71
 
72
  exam_area = gr.Column(visible=False)
73
  with exam_area:
74
  gr.Markdown("### Exam Questions")
75
  exam_display = gr.JSON(label="Questions")
76
- answers_box = gr.JSON(label="Your Answers (edit as JSON: {id: 'answer'})")
 
 
77
  submit_btn = gr.Button("Submit Exam")
78
 
79
  results_area = gr.Column(visible=False)
80
  with results_area:
81
  score_text = gr.Textbox(label="Score")
82
- analysis_json = gr.JSON(label="Weakness Analysis")
83
- forecast_json = gr.JSON(label="Forecast (AI)")
84
- coach_json = gr.JSON(label="Study Coach")
 
 
 
85
 
86
  # Button actions
87
  start_btn.click(
88
  start_exam,
89
  inputs=[level, subject],
90
- outputs=[exam_display, exam_area, results_area]
91
- ).then(
92
- lambda exam: exam, outputs=exam_output
93
- )
94
 
95
  submit_btn.click(
96
  submit_exam,
97
  inputs=[answers_box, exam_output, level, subject],
98
- outputs=[score_text, analysis_json, forecast_json, coach_json, exam_area, results_area]
99
  )
100
 
101
  if __name__ == "__main__":
102
  demo.launch()
103
 
 
 
7
  with open("questions.json", "r", encoding="utf-8") as f:
8
  QUESTION_BANK = json.load(f)
9
 
10
+ # Agents (all powered by GLM-4.5 API)
11
  analyzer = AnalyzerAgent()
12
  forecast_agent = ForecastAgent()
13
  coach_agent = CoachAgent()
14
 
15
+ # Start exam
16
  def start_exam(level, subject, num_questions=5):
17
+ # Filter question bank
18
  questions = [q for q in QUESTION_BANK if q["subject"] == f"{level}_{subject}"]
19
  selected = random.sample(questions, min(num_questions, len(questions)))
20
 
21
+ # Format for UI
22
  exam_data = [
23
  {"id": q["id"], "text": q["text"], "choices": q.get("choices", [])}
24
  for q in selected
25
  ]
26
  return exam_data, gr.update(visible=True), gr.update(visible=False)
27
 
28
+ # Submit exam
29
  def submit_exam(answers, exam_data, level, subject):
 
30
  correct = 0
31
  per_question = {}
32
  for q in exam_data:
 
39
 
40
  score = round(100 * correct / len(exam_data), 2)
41
 
42
+ # GLM-4.5 Agents
43
+ analysis = analyzer.analyze(per_question, exam_data)
 
 
44
  forecast = forecast_agent.forecast(level, subject)
 
 
45
  coach = coach_agent.coach(analysis, level, subject)
46
 
47
  return (
 
55
 
56
  # Gradio UI
57
  with gr.Blocks() as demo:
58
+ gr.Markdown("## 📘 Pay Fong Exam Simulator (AI-Powered with GLM-4.5)")
59
+
60
  with gr.Row():
61
+ level = gr.Dropdown(
62
+ ["Form2", "Form3", "Higher1", "Higher2", "Higher3"], label="Select Level"
63
+ )
64
+ subject = gr.Dropdown(
65
+ ["Math", "Science", "Physics", "Chemistry", "Biology"], label="Select Subject"
66
+ )
67
  start_btn = gr.Button("Start Exam")
68
+
69
  exam_output = gr.State()
 
70
 
71
  exam_area = gr.Column(visible=False)
72
  with exam_area:
73
  gr.Markdown("### Exam Questions")
74
  exam_display = gr.JSON(label="Questions")
75
+ answers_box = gr.JSON(
76
+ label="Your Answers (edit as JSON: {id: 'answer'})"
77
+ )
78
  submit_btn = gr.Button("Submit Exam")
79
 
80
  results_area = gr.Column(visible=False)
81
  with results_area:
82
  score_text = gr.Textbox(label="Score")
83
+ with gr.Tab("Weakness Analysis"):
84
+ analysis_json = gr.JSON()
85
+ with gr.Tab("Forecast (AI)"):
86
+ forecast_json = gr.JSON()
87
+ with gr.Tab("Study Coach"):
88
+ coach_json = gr.JSON()
89
 
90
  # Button actions
91
  start_btn.click(
92
  start_exam,
93
  inputs=[level, subject],
94
+ outputs=[exam_display, exam_area, results_area],
95
+ ).then(lambda exam: exam, outputs=exam_output)
 
 
96
 
97
  submit_btn.click(
98
  submit_exam,
99
  inputs=[answers_box, exam_output, level, subject],
100
+ outputs=[score_text, analysis_json, forecast_json, coach_json, exam_area, results_area],
101
  )
102
 
103
  if __name__ == "__main__":
104
  demo.launch()
105
 
106
+