Benny-Tang commited on
Commit
88df600
·
verified ·
1 Parent(s): da308b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -4
app.py CHANGED
@@ -1,7 +1,103 @@
 
 
1
  import gradio as gr
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
1
+ import json
2
+ import random
3
  import gradio as gr
4
+ from agents import AnalyzerAgent, ForecastAgent, CoachAgent
5
 
6
+ # Load questions
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:
33
+ qid = str(q["id"])
34
+ user_ans = answers.get(qid, None)
35
+ correct_ans = next(item for item in QUESTION_BANK if item["id"] == q["id"])["correct_answer"]
36
+ per_question[qid] = {"user": user_ans, "correct": correct_ans}
37
+ if user_ans == correct_ans:
38
+ correct += 1
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 (
52
+ f"Your Score: {score}%",
53
+ analysis,
54
+ forecast,
55
+ coach,
56
+ gr.update(visible=False),
57
+ gr.update(visible=True)
58
+ )
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