-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
318 lines (271 loc) · 12.3 KB
/
Copy pathmain.py
File metadata and controls
318 lines (271 loc) · 12.3 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
import json
import language_tool_python
import textstat
from dotenv import load_dotenv
from deertick.src.py.agent import Agent
# Load environment variables from .env file
load_dotenv()
class AIPaperGrader:
"""
An AI-powered paper grader that uses a hybrid approach of objective metrics
and subjective LLM-based evaluation against a rubric.
"""
def __init__(
self,
model="OpenAI: GPT-4 Turbo",
):
"""
Initializes the grader with an OpenAI client and a grammar tool.
"""
self.agent = Agent(
model,
"You are an expert AI teaching assistant. Your task is to grade a student's paper based on a given assignment prompt and a detailed grading rubric. You must provide a score and constructive feedback for EACH criterion in the rubric. Your analysis must be objective and strictly adhere to the rubric's definitions.",
"openai"
)
# Initialize the grammar tool (it may download language data on first run)
try:
self.lang_tool = language_tool_python.LanguageTool(
'en-US'
)
except Exception as e:
print(
f"Could not initialize language tool. Grammar checks will be skipped. Error: {e}"
)
self.lang_tool = None
def _check_objective_metrics(
self,
paper_text: str
) -> dict:
"""
Calculates objective metrics for the paper.
"""
word_count = len(
paper_text
.split()
)
readability_score = textstat.flesch_reading_ease(
paper_text
)
grammar_errors = 0
if self.lang_tool:
matches = self.lang_tool.check(
paper_text
)
grammar_errors = len(
matches
)
return {
"word_count": word_count,
"readability_score_flesch": readability_score,
"grammar_and_spelling_errors": grammar_errors
}
def _evaluate_with_llm(
self,
assignment_prompt: str,
rubric: dict,
student_paper: str
) -> dict:
"""
Uses the LLM to evaluate the paper against the rubric.
"""
# We will format the rubric nicely for the prompt
rubric_str = json.dumps(
rubric,
indent=2
)
user_prompt = f"""
Please grade the following student paper.
[ASSIGNMENT PROMPT]:
{assignment_prompt}
[GRADING RUBRIC]:
{rubric_str}
[STUDENT SUBMISSION]:
{student_paper}
[YOUR TASK]:
Evaluate the student's submission based STRICTLY on the provided rubric. For each criterion,
provide a numerical score and detailed, constructive feedback explaining why you gave that score.
Your feedback should be helpful and guide the student on how to improve.
Return your entire response as a single, valid JSON object. The JSON object should have two main keys:
1. "summary_feedback": A brief overall summary of the paper's strengths and weaknesses.
2. "criteria_breakdown": A list of objects, where each object represents a criterion from the rubric and contains:
- "criterion": The name of the criterion (e.g., "Clarity and Argument").
- "score": The score you awarded for this criterion.
- "max_score": The maximum possible score for this criterion.
- "feedback": Your detailed feedback for this specific criterion.
Do not include any text outside of the JSON object.
"""
try:
response = self.agent.generate_response(
self.agent.system_prompt,
f"""
Please grade the following student paper.
[ASSIGNMENT PROMPT]:
{assignment_prompt}
[GRADING RUBRIC]:
{rubric_str}
[STUDENT SUBMISSION]:
{student_paper}
[YOUR TASK]:
Evaluate the student's submission based STRICTLY on the provided rubric. For each criterion,
provide a numerical score and detailed, constructive feedback explaining why you gave that score.
Your feedback should be helpful and guide the student on how to improve.
Return your entire response as a single, valid JSON object. The JSON object should have two main keys:
1. "summary_feedback": A brief overall summary of the paper's strengths and weaknesses.
2. "criteria_breakdown": A list of objects, where each object represents a criterion from the rubric and contains:
- "criterion": The name of the criterion (e.g., "Clarity and Argument").
- "score": The score you awarded for this criterion.
- "max_score": The maximum possible score for this criterion.
- "feedback": Your detailed feedback for this specific criterion.
Do not include any text outside of the JSON object.
"""
)
response = self.client.chat.completions.create(
model=self.model,
messages=[
{
"role": "system",
"content": system_prompt
},
{
"role": "user",
"content": user_prompt
}
],
# This ensures the model outputs valid JSON
response_format={
"type": "json_object"
},
temperature=0.2 # Lower temperature for more consistent, objective grading
)
llm_evaluation = json.loads(
response.choices[0]
.message.content
)
return llm_evaluation
except Exception as e:
print(
f"An error occurred during LLM evaluation: {e}"
)
return {
"summary_feedback": "Error: Could not get evaluation from the AI model.",
"criteria_breakdown": []
}
def grade_paper(
self,
assignment_prompt: str,
rubric: dict,
student_paper: str
) -> dict:
"""
Orchestrates the entire grading process.
"""
print(
"1. Analyzing objective metrics..."
)
objective_results = self._check_objective_metrics(
student_paper
)
print(
"2. Performing subjective evaluation with AI..."
)
llm_results = self._evaluate_with_llm(
assignment_prompt,
rubric,
student_paper
)
# Calculate total score from the LLM's breakdown
total_score = sum(item.get('score', 0) for item in llm_results.get('criteria_breakdown', []))
max_total_score = sum(item.get('max_score', 0) for item in rubric.get('criteria', []))
print(
"3. Compiling final report..."
)
final_grade = {
"overall_score": total_score,
"max_possible_score": max_total_score,
"summary_feedback": llm_results.get(
'summary_feedback'
),
"criteria_breakdown": llm_results.get(
'criteria_breakdown'
),
"objective_metrics": objective_results
}
return final_grade
@staticmethod
def print_report(
grade_report: dict
):
"""
Prints the final grade report in a readable format.
"""
print("\n" + "=" * 50)
print(" " * 15 + "AI GRADING REPORT")
print("=" * 50 + "\n")
print(f"FINAL SCORE: {grade_report['overall_score']} / {grade_report['max_possible_score']}\n")
print("--- Overall Summary ---")
print(grade_report.get('summary_feedback', 'N/A'))
print("\n" + "-" * 25 + "\n")
print("--- Detailed Breakdown by Criterion ---")
for item in grade_report.get('criteria_breakdown', []):
print(f"\n[{item['criterion']}]")
print(f" Score: {item['score']} / {item['max_score']}")
print(f" Feedback: {item['feedback']}")
print("\n" + "-" * 25 + "\n")
print("--- Objective Metrics ---")
metrics = grade_report.get('objective_metrics', {})
print(f" Word Count: {metrics.get('word_count')}")
print(f" Flesch Reading Ease: {metrics.get('readability_score_flesch'):.2f} (Higher is easier to read)")
print(f" Grammar & Spelling Issues Found: {metrics.get('grammar_and_spelling_errors')}")
print("\n" + "=" * 50)
if __name__ == '__main__':
# 1. DEFINE THE ASSIGNMENT PROMPT
ASSIGNMENT_PROMPT = """
Write a 300-500 word essay on the role of technology in modern education.
Discuss both the advantages and disadvantages, and provide at least one specific example
for each. Conclude with your own perspective on the future of educational technology.
"""
# 2. DEFINE THE GRADING RUBRIC
# This is the most important part. A detailed rubric gets better results.
GRADING_RUBRIC = {
"criteria": [
{
"criterion": "Argument and Analysis",
"max_score": 40,
"description": "Evaluates the clarity, depth, and coherence of the argument. Assesses discussion of both advantages and disadvantages."
},
{
"criterion": "Use of Evidence",
"max_score": 30,
"description": "Checks if the student provided specific, relevant examples for both advantages and disadvantages as required."
},
{
"criterion": "Structure and Organization",
"max_score": 20,
"description": "Assesses the logical flow of the essay, including introduction, body paragraphs, and conclusion."
},
{
"criterion": "Clarity and Mechanics",
"max_score": 10,
"description": "Evaluates grammar, spelling, punctuation, and overall readability. Adherence to word count."
}
]
}
# 3. A SAMPLE STUDENT PAPER
# This paper is intentionally written to be good but not perfect.
STUDENT_PAPER = """
Technology in Education Today
The role of technology in education has become super important. It has changed how students learn and teachers teach. There are many good things about it, but also some bad things.
One major advantage is access to information. With the internet, students can find anything they want instantly. For example, a student studying history can watch documentaries or read original documents online, which is much better than just a textbook. This makes learning more engaging. Digital tools like interactive whiteboards and educational apps also make lessons more fun.
However, there are also disadvantages. The biggest one is the digital divide, where not all students have equal access to computers or reliable internet at home. This creates inequality. Another problem is the potential for distraction. When students use laptops in class, they might be tempted to go on social media instead of paying attention to the lesson. It's a real issue for classroom management.
In conclusion, I believe that technology is a powerful tool for education. The benefits, like access to resources, are huge. We need to work on solving the problems like the digital divide to make sure everyone can benefit. The future of education will definitely involve more technology, and it will probably be even more integrated into learning.
"""
# 4. RUN THE GRADER
try:
grader = AIPaperGrader()
final_grade_report = grader.grade_paper(ASSIGNMENT_PROMPT, GRADING_RUBRIC, STUDENT_PAPER)
# 5. PRINT THE FINAL REPORT
AIPaperGrader.print_report(final_grade_report)
except ValueError as e:
print(e)
except Exception as e:
print(f"An unexpected error occurred: {e}")