Spaces:
Runtime error
Runtime error
Create agents.py
Browse files
agents.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import requests
|
| 4 |
+
|
| 5 |
+
GLM_API_URL = "https://api.your-glm-provider.com/v1/chat/completions"
|
| 6 |
+
GLM_API_KEY = os.getenv("GLM_API_KEY")
|
| 7 |
+
|
| 8 |
+
def call_glm(prompt, temperature=0.3):
|
| 9 |
+
headers = {"Authorization": f"Bearer {GLM_API_KEY}"}
|
| 10 |
+
payload = {
|
| 11 |
+
"model": "glm-4.5",
|
| 12 |
+
"messages": [{"role": "system", "content": "You are an educational assistant."},
|
| 13 |
+
{"role": "user", "content": prompt}],
|
| 14 |
+
"temperature": temperature
|
| 15 |
+
}
|
| 16 |
+
response = requests.post(GLM_API_URL, headers=headers, json=payload)
|
| 17 |
+
result = response.json()
|
| 18 |
+
return result["choices"][0]["message"]["content"]
|
| 19 |
+
|
| 20 |
+
class AnalyzerAgent:
|
| 21 |
+
def analyze(self, answers, question_bank):
|
| 22 |
+
# Simple deterministic analysis
|
| 23 |
+
topic_stats = {}
|
| 24 |
+
for qid, result in answers.items():
|
| 25 |
+
q = next(item for item in question_bank if str(item["id"]) == qid)
|
| 26 |
+
for topic in q.get("topics", []):
|
| 27 |
+
if topic not in topic_stats:
|
| 28 |
+
topic_stats[topic] = {"correct": 0, "total": 0}
|
| 29 |
+
topic_stats[topic]["total"] += 1
|
| 30 |
+
if result["user"] == result["correct"]:
|
| 31 |
+
topic_stats[topic]["correct"] += 1
|
| 32 |
+
|
| 33 |
+
analysis = {t: round(v["correct"]/v["total"], 2) for t,v in topic_stats.items()}
|
| 34 |
+
return {"topic_accuracy": analysis}
|
| 35 |
+
|
| 36 |
+
class ForecastAgent:
|
| 37 |
+
def forecast(self, level, subject):
|
| 38 |
+
prompt = f"""
|
| 39 |
+
You are an exam forecast assistant.
|
| 40 |
+
Predict 3 high-probability topics for {level} {subject} exam.
|
| 41 |
+
Return JSON: {{"predicted_topics": [{{"topic": "...", "confidence": 0.0}}]}}
|
| 42 |
+
"""
|
| 43 |
+
try:
|
| 44 |
+
response = call_glm(prompt)
|
| 45 |
+
return json.loads(response)
|
| 46 |
+
except Exception:
|
| 47 |
+
return {"error": "Forecast unavailable"}
|
| 48 |
+
|
| 49 |
+
class CoachAgent:
|
| 50 |
+
def coach(self, analysis, level, subject):
|
| 51 |
+
prompt = f"""
|
| 52 |
+
You are a study coach.
|
| 53 |
+
The student's weaknesses are: {analysis}.
|
| 54 |
+
Suggest a study plan and 3 practice questions for {level} {subject}.
|
| 55 |
+
Return JSON: {{"tips": ["..."], "study_plan": "...", "practice_questions": [{{"text": "...", "answer": "..."}}]}}
|
| 56 |
+
"""
|
| 57 |
+
try:
|
| 58 |
+
response = call_glm(prompt)
|
| 59 |
+
return json.loads(response)
|
| 60 |
+
except Exception:
|
| 61 |
+
return {"error": "Coach unavailable"}
|