Spaces:
Running
Running
github-actions[bot] commited on
Commit ·
40e4107
1
Parent(s): 900dd15
🚀 Auto-deploy backend from GitHub (cf4bb39)
Browse files
services/intervention_engine.py
CHANGED
|
@@ -96,9 +96,11 @@ def _classify_risk(avg_score: float, quiz_count: int, days_since_active: Optiona
|
|
| 96 |
return "Low Risk"
|
| 97 |
|
| 98 |
|
| 99 |
-
def _classify_engagement(days_since_active: Optional[int], recent_quiz_count: int) -> str:
|
| 100 |
-
if
|
| 101 |
return "High"
|
|
|
|
|
|
|
| 102 |
if days_since_active is not None and days_since_active <= 7:
|
| 103 |
return "Medium"
|
| 104 |
return "Low"
|
|
@@ -181,8 +183,21 @@ class InterventionEngine:
|
|
| 181 |
# Recent quiz count (last 14 days)
|
| 182 |
recent_count = sum(1 for q in quiz_attempts if self._is_recent(q, now, 14))
|
| 183 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
risk_level = _classify_risk(avg_score, quiz_count, days_since_active)
|
| 185 |
-
engagement = _classify_engagement(days_since_active, recent_count)
|
| 186 |
|
| 187 |
# Generate AI insights
|
| 188 |
insights = await self._generate_insights(
|
|
@@ -194,6 +209,7 @@ class InterventionEngine:
|
|
| 194 |
strong_topics=strong_topics,
|
| 195 |
weak_topics=weak_topics,
|
| 196 |
quiz_count=quiz_count,
|
|
|
|
| 197 |
)
|
| 198 |
|
| 199 |
# Generate learning path
|
|
@@ -344,6 +360,7 @@ Student: Grade {kwargs['grade_level']}, Section {kwargs['section']}
|
|
| 344 |
Risk Level: {kwargs['risk_level']}
|
| 345 |
Average Score: {kwargs['avg_score']:.1f}%
|
| 346 |
Engagement: {kwargs['engagement']}
|
|
|
|
| 347 |
Strong Topics (accuracy > 70%): {', '.join(kwargs['strong_topics'][:3]) or 'None identified yet'}
|
| 348 |
Weak Topics (accuracy < 60%): {', '.join(kwargs['weak_topics'][:3]) or 'None identified yet'}
|
| 349 |
Quiz Attempt Count (last 30 days): {kwargs['quiz_count']}
|
|
@@ -373,7 +390,7 @@ Return as JSON:
|
|
| 373 |
except Exception as e:
|
| 374 |
logger.warning(f"DeepSeek insights failed: {e}")
|
| 375 |
return {
|
| 376 |
-
"learning_strengths": "Shows willingness to engage with the platform." if kwargs['quiz_count'] > 0 else "No assessment data yet — potential to be discovered.",
|
| 377 |
"next_steps_summary": f"Begin with foundational practice in {kwargs['weak_topics'][0] if kwargs['weak_topics'] else 'core topics'}.",
|
| 378 |
}
|
| 379 |
|
|
|
|
| 96 |
return "Low Risk"
|
| 97 |
|
| 98 |
|
| 99 |
+
def _classify_engagement(days_since_active: Optional[int], recent_quiz_count: int, lessons_completed: int = 0) -> str:
|
| 100 |
+
if lessons_completed >= 5:
|
| 101 |
return "High"
|
| 102 |
+
if lessons_completed >= 2 or (days_since_active is not None and days_since_active <= 2 and recent_quiz_count >= 5):
|
| 103 |
+
return "High" if recent_quiz_count >= 3 else "Medium"
|
| 104 |
if days_since_active is not None and days_since_active <= 7:
|
| 105 |
return "Medium"
|
| 106 |
return "Low"
|
|
|
|
| 183 |
# Recent quiz count (last 14 days)
|
| 184 |
recent_count = sum(1 for q in quiz_attempts if self._is_recent(q, now, 14))
|
| 185 |
|
| 186 |
+
# Fetch lessons completed from progress doc
|
| 187 |
+
lessons_completed = 0
|
| 188 |
+
for lookup_id in [student_id, student_data.get("accountUid")]:
|
| 189 |
+
if not lookup_id:
|
| 190 |
+
continue
|
| 191 |
+
try:
|
| 192 |
+
pdoc = db.collection("progress").document(lookup_id).get()
|
| 193 |
+
if pdoc.exists:
|
| 194 |
+
lessons_completed = pdoc.to_dict().get("totalLessonsCompleted", 0)
|
| 195 |
+
break
|
| 196 |
+
except Exception:
|
| 197 |
+
pass
|
| 198 |
+
|
| 199 |
risk_level = _classify_risk(avg_score, quiz_count, days_since_active)
|
| 200 |
+
engagement = _classify_engagement(days_since_active, recent_count, lessons_completed)
|
| 201 |
|
| 202 |
# Generate AI insights
|
| 203 |
insights = await self._generate_insights(
|
|
|
|
| 209 |
strong_topics=strong_topics,
|
| 210 |
weak_topics=weak_topics,
|
| 211 |
quiz_count=quiz_count,
|
| 212 |
+
lessons_completed=lessons_completed,
|
| 213 |
)
|
| 214 |
|
| 215 |
# Generate learning path
|
|
|
|
| 360 |
Risk Level: {kwargs['risk_level']}
|
| 361 |
Average Score: {kwargs['avg_score']:.1f}%
|
| 362 |
Engagement: {kwargs['engagement']}
|
| 363 |
+
Lessons Completed: {kwargs.get('lessons_completed', 0)}
|
| 364 |
Strong Topics (accuracy > 70%): {', '.join(kwargs['strong_topics'][:3]) or 'None identified yet'}
|
| 365 |
Weak Topics (accuracy < 60%): {', '.join(kwargs['weak_topics'][:3]) or 'None identified yet'}
|
| 366 |
Quiz Attempt Count (last 30 days): {kwargs['quiz_count']}
|
|
|
|
| 390 |
except Exception as e:
|
| 391 |
logger.warning(f"DeepSeek insights failed: {e}")
|
| 392 |
return {
|
| 393 |
+
"learning_strengths": "Shows willingness to engage with the platform." if kwargs['quiz_count'] > 0 else (f"Completed {kwargs.get('lessons_completed', 0)} lessons; consistent study habits developing." if kwargs.get('lessons_completed', 0) >= 2 else "No assessment data yet — potential to be discovered."),
|
| 394 |
"next_steps_summary": f"Begin with foundational practice in {kwargs['weak_topics'][0] if kwargs['weak_topics'] else 'core topics'}.",
|
| 395 |
}
|
| 396 |
|