Spaces:
Runtime error
Runtime error
Update agents.py
Browse files
agents.py
CHANGED
|
@@ -2,39 +2,77 @@ import os
|
|
| 2 |
import random
|
| 3 |
from zhipuai import ZhipuAI
|
| 4 |
|
| 5 |
-
|
| 6 |
-
client = ZhipuAI(api_key=
|
| 7 |
|
| 8 |
class AnalyzerAgent:
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
feedback = []
|
| 12 |
-
for
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
else:
|
| 16 |
-
feedback.append(f"Q{qid}:
|
| 17 |
-
return
|
|
|
|
| 18 |
|
| 19 |
class CoachAgent:
|
| 20 |
-
|
| 21 |
-
if not feedback:
|
| 22 |
-
return "Excellent! You answered all correctly."
|
| 23 |
-
return "Review these: " + "; ".join(feedback)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
resp = client.chat.completions.create(
|
| 32 |
-
model="glm-4
|
| 33 |
messages=[{"role": "user", "content": prompt}]
|
| 34 |
)
|
| 35 |
return resp.choices[0].message["content"]
|
| 36 |
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
|
| 40 |
|
|
|
|
| 2 |
import random
|
| 3 |
from zhipuai import ZhipuAI
|
| 4 |
|
| 5 |
+
# Initialize ZhipuAI
|
| 6 |
+
client = ZhipuAI(api_key=os.getenv("ZHIPUAI_API_KEY", "demo-key"))
|
| 7 |
|
| 8 |
class AnalyzerAgent:
|
| 9 |
+
"""Grades answers and provides analysis."""
|
| 10 |
+
|
| 11 |
+
def analyze(self, answers, questions):
|
| 12 |
+
correct = 0
|
| 13 |
+
total = len(questions)
|
| 14 |
+
for q in questions:
|
| 15 |
+
qid = str(q.get("id"))
|
| 16 |
+
if qid in answers and answers[qid] == q.get("answer"):
|
| 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 |
+
feedback.append(f"Q{qid}: Incorrect ❌ (Correct: {q.get('answer')})")
|
| 30 |
+
return "\n".join(feedback)
|
| 31 |
+
|
| 32 |
|
| 33 |
class CoachAgent:
|
| 34 |
+
"""Provides motivational coaching based on analysis."""
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
def provide_guidance(self, analysis_text):
|
| 37 |
+
prompt = (
|
| 38 |
+
"You are a supportive SPM exam coach. "
|
| 39 |
+
"Given the analysis of a student's answers, "
|
| 40 |
+
"provide encouragement, study tips, and focus areas.\n\n"
|
| 41 |
+
f"Analysis:\n{analysis_text}"
|
| 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 |
+
"""Generates AI-predicted practice questions."""
|
| 52 |
+
|
| 53 |
+
def generate_predicted_questions(self, subject, count=5):
|
| 54 |
+
prompt = (
|
| 55 |
+
f"Generate {count} multiple-choice practice questions for SPM {subject}. "
|
| 56 |
+
"Each question should include: text, four choices (A-D), and correct answer."
|
| 57 |
+
)
|
| 58 |
+
resp = client.chat.completions.create(
|
| 59 |
+
model="glm-4",
|
| 60 |
+
messages=[{"role": "user", "content": prompt}]
|
| 61 |
+
)
|
| 62 |
+
# Parse output into simple mock questions (demo version)
|
| 63 |
+
questions = []
|
| 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 |
+
|
| 76 |
|
| 77 |
|
| 78 |
|