-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_prototype.py
More file actions
355 lines (290 loc) Β· 15.8 KB
/
demo_prototype.py
File metadata and controls
355 lines (290 loc) Β· 15.8 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/env python3
"""
CogniFlow Prototype Demo Script
==============================
This script demonstrates the key features of the refactored CogniFlow prototype
by simulating a realistic user learning journey.
Usage: python demo_prototype.py
"""
import asyncio
import httpx
import json
import time
from typing import Dict, Any
# Service endpoints
SERVICES = {
"courses": "http://localhost:8003",
"ai_tutor": "http://localhost:8000",
"analytics": "http://localhost:8004"
}
class CogniFlowDemo:
def __init__(self):
self.user_id = "demo_user"
async def demo_step(self, title: str, description: str):
"""Display a demo step with formatting"""
print(f"\n{'='*60}")
print(f"π― {title}")
print(f"{'='*60}")
print(f"π {description}")
print()
# Small delay for readability
await asyncio.sleep(1)
async def run_demo(self):
"""Run the complete CogniFlow demo"""
print("π CogniFlow Prototype Demo")
print("==========================")
print("This demo shows how a user interacts with the learning platform.")
print()
async with httpx.AsyncClient() as client:
# Step 1: Browse and enroll in courses
await self.demo_step(
"Step 1: Course Discovery & Enrollment",
"User browses available courses and enrolls in 'Introduction to AI'"
)
try:
# Get available courses
response = await client.get(f"{SERVICES['courses']}/courses/")
courses = response.json()
print(f"π Available Courses ({len(courses)} found):")
for course in courses[:3]: # Show first 3
print(f" β’ {course['title']} - {course['difficulty']} ({course['enrollment_count']} students)")
# Enroll in first course
course_id = courses[0]["id"]
enrollment_data = {"user_id": self.user_id, "course_id": course_id}
response = await client.post(
f"{SERVICES['courses']}/courses/{course_id}/enroll",
json=enrollment_data
)
if response.status_code in [200, 201]:
print(f"\nβ
Successfully enrolled in: {courses[0]['title']}")
else:
print(f"\nβ Enrollment failed: {response.text}")
except Exception as e:
print(f"β Error in course enrollment: {e}")
# Step 2: AI Tutor Interaction
await self.demo_step(
"Step 2: AI Tutor Conversation",
"User asks the AI tutor for help with learning concepts"
)
try:
# Chat with AI tutor
chat_messages = [
"Hello! I just enrolled in the AI course. Can you help me get started?",
"What are the main types of machine learning?",
"Can you explain what supervised learning is?"
]
for message in chat_messages:
print(f"π€ User: {message}")
chat_data = {
"user_id": self.user_id,
"message": message,
"context": "ai_course"
}
response = await client.post(f"{SERVICES['ai_tutor']}/chat", json=chat_data)
if response.status_code == 200:
chat_response = response.json()
ai_response = chat_response['response']
print(f"π€ AI Tutor: {ai_response}")
if chat_response.get('suggestions'):
print(f"π‘ Suggestions: {', '.join(chat_response['suggestions'][:2])}")
else:
print("β Chat failed")
print()
await asyncio.sleep(1)
except Exception as e:
print(f"β Error in AI chat: {e}")
# Step 3: Learning Progress
await self.demo_step(
"Step 3: Learning Progress Tracking",
"User completes lessons and tracks progress through the course"
)
try:
# Complete several lessons
for lesson_id in [1, 2, 3]:
response = await client.post(
f"{SERVICES['courses']}/users/{self.user_id}/progress/{course_id}/lessons/{lesson_id}"
)
if response.status_code in [200, 201]:
progress = response.json()
print(f"β
Completed Lesson {lesson_id}")
print(f" Progress: {progress.get('progress_percentage', 0):.1f}% complete")
print(f" Time spent: {progress.get('time_spent_minutes', 0)} minutes")
else:
print(f"β Failed to complete lesson {lesson_id}")
await asyncio.sleep(0.5)
# Get overall progress
response = await client.get(f"{SERVICES['courses']}/users/{self.user_id}/progress/{course_id}")
if response.status_code == 200:
progress = response.json()
print(f"\nπ Overall Course Progress:")
print(f" Completion: {progress.get('progress_percentage', 0):.1f}%")
print(f" Lessons completed: {progress.get('completed_lesson_count', 0)}/{progress.get('total_lessons', 0)}")
print(f" Estimated time remaining: {progress.get('estimated_completion_time_minutes', 0)} minutes")
except Exception as e:
print(f"β Error in progress tracking: {e}")
# Step 4: Spaced Repetition System
await self.demo_step(
"Step 4: Spaced Repetition Learning",
"User reviews scheduled content using the spaced repetition algorithm"
)
try:
# Get spaced repetition schedule
response = await client.get(f"{SERVICES['ai_tutor']}/spaced-repetition/{self.user_id}")
if response.status_code == 200:
schedule = response.json()
print(f"π
Spaced Repetition Schedule:")
print(f" Total items: {schedule.get('total_items', 0)}")
print(f" Items due today: {schedule.get('items_due', 0)}")
if schedule.get('due_items'):
print(f"\nπ Items due for review:")
for item in schedule['due_items'][:2]: # Show first 2
print(f" β’ {item.get('topic', 'Unknown topic')}")
# Simulate review session
if schedule.get('due_items'):
item = schedule['due_items'][0]
update_data = {
"user_id": self.user_id,
"item_id": item['item_id'],
"performance": "good"
}
response = await client.post(
f"{SERVICES['ai_tutor']}/spaced-repetition/update",
json=update_data
)
if response.status_code == 200:
result = response.json()
updated_item = result['updated_item']
print(f"\nβ
Reviewed: {item.get('topic')}")
print(f" Performance: Good")
print(f" Next review: {updated_item['next_review'][:10]}")
except Exception as e:
print(f"β Error in spaced repetition: {e}")
# Step 5: Gamification & Achievements
await self.demo_step(
"Step 5: Gamification & Achievement System",
"User earns points and badges for learning activities"
)
try:
# Get gamification stats
response = await client.get(f"{SERVICES['ai_tutor']}/gamification/{self.user_id}")
if response.status_code == 200:
stats = response.json()
user_stats = stats.get('stats', {})
badges = stats.get('badges', [])
print(f"π Achievement Progress:")
print(f" Total Points: {user_stats.get('total_points', 0)}")
print(f" Current Streak: {user_stats.get('current_streak', 0)} days")
print(f" Topics Mastered: {user_stats.get('topics_mastered', 0)}")
print(f" Sessions Completed: {user_stats.get('total_sessions', 0)}")
if badges:
print(f"\nποΈ Earned Badges ({len(badges)}):")
for badge in badges[:3]: # Show first 3
badge_info = stats.get('available_badges', {}).get(badge, {})
print(f" β’ {badge_info.get('name', badge)}")
# Show next milestone
next_milestone = stats.get('next_milestone', {})
if next_milestone:
print(f"\nπ― Next Milestone:")
print(f" Target: {next_milestone.get('points', 0)} points")
print(f" Progress: {next_milestone.get('progress', 0)}/100")
except Exception as e:
print(f"β Error in gamification: {e}")
# Step 6: Quiz Generation & Assessment
await self.demo_step(
"Step 6: Adaptive Quiz System",
"User takes an AI-generated quiz to test understanding"
)
try:
# Generate a quiz
quiz_data = {
"user_id": self.user_id,
"topic": "ai",
"difficulty": "beginner",
"num_questions": 3
}
response = await client.post(f"{SERVICES['ai_tutor']}/quiz/generate", json=quiz_data)
if response.status_code == 200:
quiz = response.json()
print(f"π Generated Quiz: {quiz['quiz_id']}")
print(f" Topic: AI Fundamentals")
print(f" Questions: {len(quiz['questions'])}")
print(f" Time limit: {quiz['time_limit_minutes']} minutes")
# Show first question
if quiz['questions']:
q = quiz['questions'][0]
print(f"\nβ Sample Question:")
print(f" {q['question']}")
for i, option in enumerate(q['options'], 1):
print(f" {i}. {option}")
# Simulate quiz submission
print(f"\nβ³ Simulating quiz completion...")
await asyncio.sleep(2)
print(f"β
Quiz completed with score: 85%")
print(f"π Earned 25 points for quiz completion!")
except Exception as e:
print(f"β Error in quiz generation: {e}")
# Step 7: Analytics Dashboard
await self.demo_step(
"Step 7: Learning Analytics Dashboard",
"System provides insights into learning patterns and progress"
)
try:
# Get user analytics
response = await client.get(f"{SERVICES['analytics']}/analytics/user/{self.user_id}")
if response.status_code == 200:
analytics = response.json()['analytics']
print(f"π Learning Analytics Summary:")
print(f" Total Learning Events: {analytics.get('total_events', 0)}")
print(f" Active Learning Days: {analytics.get('learning_patterns', {}).get('session_frequency', 0)}")
print(f" Average Session Length: {analytics.get('engagement', {}).get('session_length_avg', 0):.1f} minutes")
print(f" Learning Consistency: {analytics.get('learning_patterns', {}).get('learning_consistency', 0):.2f}")
# Show activity breakdown
event_breakdown = analytics.get('event_breakdown', {})
if event_breakdown:
print(f"\nπ Activity Breakdown:")
for event_type, count in list(event_breakdown.items())[:3]:
print(f" {event_type.replace('_', ' ').title()}: {count}")
# Show performance metrics
performance = analytics.get('performance', {})
if performance:
print(f"\nπ― Performance Metrics:")
print(f" Quizzes Completed: {performance.get('total_quizzes', 0)}")
print(f" Average Quiz Score: {performance.get('average_quiz_score', 0):.1f}%")
print(f" Improvement Trend: {performance.get('quiz_improvement_trend', 'stable').title()}")
except Exception as e:
print(f"β Error in analytics: {e}")
# Final Summary
await self.demo_step(
"π Demo Complete!",
"The CogniFlow prototype demonstrates a comprehensive learning platform"
)
print("π Key Features Demonstrated:")
print(" β
Course enrollment and progress tracking")
print(" β
AI-powered tutoring and conversation")
print(" β
Spaced repetition learning algorithm")
print(" β
Gamification with points and badges")
print(" β
Adaptive quiz generation and assessment")
print(" β
Comprehensive learning analytics")
print()
print("ποΈ Architecture Highlights:")
print(" β
Microservices with clean APIs")
print(" β
Store abstraction for easy database migration")
print(" β
Event-driven analytics and insights")
print(" β
Scalable and maintainable code structure")
print()
print("π This prototype is ready for:")
print(" β’ Frontend integration (React dashboard)")
print(" β’ Database migration (PostgreSQL)")
print(" β’ Real AI integration (OpenAI, etc.)")
print(" β’ Production deployment")
async def main():
demo = CogniFlowDemo()
try:
await demo.run_demo()
except KeyboardInterrupt:
print("\n\nβΉοΈ Demo interrupted by user")
except Exception as e:
print(f"\n\nβ Demo failed: {e}")
print("Make sure all services are running: ./start_prototype.sh")
if __name__ == "__main__":
asyncio.run(main())