Spaces:
Runtime error
Runtime error
Update agents.py
Browse files
agents.py
CHANGED
|
@@ -2,74 +2,53 @@ import os
|
|
| 2 |
import random
|
| 3 |
from zhipuai import ZhipuAI
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
|
| 8 |
class AnalyzerAgent:
|
| 9 |
-
"""
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
correct += 1
|
| 18 |
-
return f"Score: {correct}/{total}"
|
| 19 |
-
|
| 20 |
-
def get_detailed_feedback(self, answers, questions):
|
| 21 |
-
feedback = []
|
| 22 |
-
for q in questions:
|
| 23 |
-
qid = str(q.get("id"))
|
| 24 |
-
if qid not in answers:
|
| 25 |
-
feedback.append(f"Q{qid}: Not attempted")
|
| 26 |
-
elif answers[qid] == q.get("answer"):
|
| 27 |
-
feedback.append(f"Q{qid}: Correct ✅")
|
| 28 |
else:
|
| 29 |
-
|
| 30 |
-
return
|
| 31 |
-
|
| 32 |
|
| 33 |
class CoachAgent:
|
| 34 |
-
"""Provides
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
"
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
resp = client.chat.completions.create(
|
| 44 |
model="glm-4",
|
| 45 |
messages=[{"role": "user", "content": prompt}]
|
| 46 |
)
|
| 47 |
return resp.choices[0].message["content"]
|
| 48 |
|
| 49 |
-
|
| 50 |
class PredictiveAgent:
|
| 51 |
-
"""
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
def
|
| 54 |
prompt = (
|
| 55 |
-
f"
|
| 56 |
-
"
|
| 57 |
)
|
| 58 |
-
resp = client.chat.completions.create(
|
| 59 |
model="glm-4",
|
| 60 |
messages=[{"role": "user", "content": prompt}]
|
| 61 |
)
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
for i in range(count):
|
| 65 |
-
questions.append({
|
| 66 |
-
"id": 900000 + i,
|
| 67 |
-
"text": f"Practice predicted question {i+1} on {subject}",
|
| 68 |
-
"choices": ["A", "B", "C", "D"],
|
| 69 |
-
"answer": random.choice(["A", "B", "C", "D"]),
|
| 70 |
-
"source": "predicted"
|
| 71 |
-
})
|
| 72 |
-
return questions
|
| 73 |
|
| 74 |
|
| 75 |
|
|
|
|
| 2 |
import random
|
| 3 |
from zhipuai import ZhipuAI
|
| 4 |
|
| 5 |
+
# Load API key
|
| 6 |
+
ZHIPU_API_KEY = os.getenv("zhipuai_api_key")
|
| 7 |
|
| 8 |
class AnalyzerAgent:
|
| 9 |
+
"""Analyzes student answers against scheme."""
|
| 10 |
+
def analyze(self, student_answers, correct_answers):
|
| 11 |
+
score = 0
|
| 12 |
+
analysis = []
|
| 13 |
+
for i, (ans, correct) in enumerate(zip(student_answers, correct_answers)):
|
| 14 |
+
if ans == correct:
|
| 15 |
+
score += 1
|
| 16 |
+
analysis.append(f"Q{i+1}: ✅ Correct")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
else:
|
| 18 |
+
analysis.append(f"Q{i+1}: ❌ Wrong (Correct: {correct})")
|
| 19 |
+
return score, analysis
|
|
|
|
| 20 |
|
| 21 |
class CoachAgent:
|
| 22 |
+
"""Provides AI feedback for essays and weak areas."""
|
| 23 |
+
def __init__(self):
|
| 24 |
+
self.client = ZhipuAI(api_key=ZHIPU_API_KEY)
|
| 25 |
+
|
| 26 |
+
def coach(self, essay_text):
|
| 27 |
+
if not essay_text.strip():
|
| 28 |
+
return "⚠️ No essay text provided."
|
| 29 |
+
prompt = f"Provide constructive feedback and improvement tips for this essay:\n\n{essay_text}"
|
| 30 |
+
resp = self.client.chat.completions.create(
|
|
|
|
| 31 |
model="glm-4",
|
| 32 |
messages=[{"role": "user", "content": prompt}]
|
| 33 |
)
|
| 34 |
return resp.choices[0].message["content"]
|
| 35 |
|
|
|
|
| 36 |
class PredictiveAgent:
|
| 37 |
+
"""Forecasts likely SPM 2025–2026 questions based on past papers."""
|
| 38 |
+
def __init__(self):
|
| 39 |
+
self.client = ZhipuAI(api_key=ZHIPU_API_KEY)
|
| 40 |
|
| 41 |
+
def predict(self, subject, year_range="2018-2024"):
|
| 42 |
prompt = (
|
| 43 |
+
f"Analyze Malaysian SPM {subject} past papers from {year_range}.\n"
|
| 44 |
+
f"Identify recurring patterns and predict 5 high-probability questions for upcoming exams."
|
| 45 |
)
|
| 46 |
+
resp = self.client.chat.completions.create(
|
| 47 |
model="glm-4",
|
| 48 |
messages=[{"role": "user", "content": prompt}]
|
| 49 |
)
|
| 50 |
+
return resp.choices[0].message["content"]
|
| 51 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
|
| 54 |
|