-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflask_app.py
More file actions
670 lines (579 loc) · 24.3 KB
/
Copy pathflask_app.py
File metadata and controls
670 lines (579 loc) · 24.3 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
"""
Laura-bot Advanced Learning Assistant with Arduino Integration
Flask Web Application combining hardware control with educational features
"""
from flask import Flask, render_template, jsonify, request, redirect, url_for
from flask_socketio import SocketIO, emit
import json
import sqlite3
import threading
import time
from datetime import datetime, timedelta
import os
import sys
# Add current directory to path for imports
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
# Import Laura-bot modules with graceful error handling
HARDWARE_AVAILABLE = False
LEARNING_MODULES_AVAILABLE = False
AI_AVAILABLE = False
VOICE_AVAILABLE = False
# Voice modules
try:
from voice.speaker import speak, board
from voice.listener import listen
VOICE_AVAILABLE = True
HARDWARE_AVAILABLE = board is not None
print("✅ Voice modules loaded successfully")
except Exception as e:
print(f"⚠️ Voice modules unavailable: {e}")
# Create fallback functions
def speak(text):
print(f"🤖 Laura-bot: {text}")
def listen():
return ""
# AI modules
try:
from ai.gemini_ai import get_response, get_tutor_response, generate_quiz, generate_explanation
AI_AVAILABLE = True
print("✅ AI modules loaded successfully")
except Exception as e:
print(f"⚠️ AI modules unavailable: {e}")
# Create fallback functions
def get_response(text):
return "AI response unavailable. Please check AI configuration."
def get_tutor_response(text, subject=None):
return "AI tutor unavailable. Please check AI configuration."
def generate_quiz(subject, difficulty="medium"):
return {"question": "Quiz unavailable", "options": ["A", "B", "C", "D"], "correct": 0}
def generate_explanation(topic):
return "Explanation unavailable. Please check AI configuration."
# Learning modules
try:
from learning_tracker import LearningTracker, start_session, end_session
from education_modules import get_education_module, list_available_subjects
LEARNING_MODULES_AVAILABLE = True
print("✅ Learning modules loaded successfully")
except Exception as e:
print(f"⚠️ Learning modules unavailable: {e}")
# Create fallback functions
def start_session(user_id="default"):
return {"session_id": "fallback"}
def end_session(session_id):
return {"status": "ended"}
def list_available_subjects():
return ["Mathematics", "Science", "Literature", "History"]
def get_education_module(subject):
return None
# Arduino controller
try:
from arduino.educational_controller import ArduinoEducationalController
print("✅ Arduino controller module loaded successfully")
except Exception as e:
print(f"⚠️ Arduino controller unavailable: {e}")
# Create fallback class
class ArduinoEducationalController:
def __init__(self):
self.connected = False
def move_servo(self, angle):
print(f"Servo simulation: {angle} degrees")
def control_led(self, state):
print(f"LED simulation: {'ON' if state else 'OFF'}")
def read_sensor(self):
return 50 # Simulated sensor value
# Initialize Flask app with SocketIO for real-time communication
app = Flask(__name__)
app.config['SECRET_KEY'] = 'laura_bot_secret_key_2025'
socketio = SocketIO(app, cors_allowed_origins="*")
# Initialize learning tracker
try:
learning_tracker = LearningTracker()
print("📊 Learning tracker initialized")
except Exception as e:
print(f"⚠️ Learning tracker error: {e}")
learning_tracker = None
# Initialize Arduino controller
try:
arduino_controller = ArduinoEducationalController()
print("🤖 Arduino controller initialized")
except Exception as e:
print(f"⚠️ Arduino controller error: {e}")
arduino_controller = None
# Global state
current_session = {
'user_name': 'Student',
'session_id': None,
'learning_mode': 'standby',
'current_subject': None,
'quiz_in_progress': False,
'quiz_data': None,
'question_index': 0,
'score': 0,
'listening': False,
'hardware_status': 'connected' if HARDWARE_AVAILABLE else 'simulation'
}
# Database initialization
def init_database():
"""Initialize SQLite database for learning data"""
conn = sqlite3.connect('learning_data/laura_learning.db')
cursor = conn.cursor()
# Create tables if they don't exist
cursor.execute('''
CREATE TABLE IF NOT EXISTS interactions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
user_name TEXT,
interaction_type TEXT,
subject TEXT,
topic TEXT,
content TEXT,
response TEXT,
hardware_used BOOLEAN DEFAULT 0
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS quiz_results (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
user_name TEXT,
subject TEXT,
topic TEXT,
questions_total INTEGER,
questions_correct INTEGER,
score_percentage REAL,
difficulty_level INTEGER,
time_taken INTEGER
)
''')
conn.commit()
conn.close()
print("📊 Database initialized successfully")
# Initialize database on startup
init_database()
# Route: Main Dashboard
@app.route('/')
def dashboard():
"""Main learning dashboard with mobile detection"""
# Check if user is on mobile device
user_agent = request.headers.get('User-Agent', '').lower()
is_mobile = any(device in user_agent for device in ['mobile', 'android', 'iphone', 'ipad', 'tablet'])
# Use mobile template for mobile devices
template = 'mobile_dashboard.html' if is_mobile else 'dashboard.html'
return render_template(template,
hardware_status=current_session['hardware_status'],
available_subjects=list_available_subjects() if LEARNING_MODULES_AVAILABLE else [],
session=current_session)
# Route: Learning Interface
@app.route('/learn')
def learning_interface():
"""Interactive learning interface with mobile detection"""
# Check if user is on mobile device
user_agent = request.headers.get('User-Agent', '').lower()
is_mobile = any(device in user_agent for device in ['mobile', 'android', 'iphone', 'ipad', 'tablet'])
# Use mobile template for mobile devices
template = 'mobile_learn.html' if is_mobile else 'learn.html'
return render_template(template,
session=current_session,
available_subjects=list_available_subjects() if LEARNING_MODULES_AVAILABLE else [])
# Route: Progress Analytics
@app.route('/progress')
def progress_analytics():
"""Learning progress and analytics"""
if learning_tracker:
stats = learning_tracker.get_user_stats(current_session['user_name'])
recent_sessions = learning_tracker.get_recent_sessions(current_session['user_name'], 10)
return render_template('progress.html', stats=stats, sessions=recent_sessions)
else:
return render_template('progress.html', stats={}, sessions=[])
# Route: Hardware Control
@app.route('/hardware')
def hardware_control():
"""Arduino hardware control interface"""
return render_template('hardware.html',
hardware_available=HARDWARE_AVAILABLE,
hardware_status=current_session['hardware_status'])
# API Route: Start Learning Session
@app.route('/api/start_session', methods=['POST'])
def start_learning_session():
"""Start a new learning session"""
data = request.get_json()
user_name = data.get('user_name', 'Student')
subject = data.get('subject', 'General')
current_session['user_name'] = user_name
current_session['current_subject'] = subject
current_session['learning_mode'] = 'active'
# Start tracking session
if learning_tracker:
session_id = start_session(user_name, subject)
current_session['session_id'] = session_id
# Hardware acknowledgment
if HARDWARE_AVAILABLE:
threading.Thread(target=lambda: speak(f"வணக்கம் {user_name}! {subject} கற்கத் தொடங்குவோம்!")).start()
# Log interaction
log_interaction('session_start', subject, 'Session Started', f"Learning session started for {subject}")
return jsonify({
'success': True,
'session_id': current_session['session_id'],
'message': f'Learning session started for {subject}'
})
# API Route: Generate Quiz
@app.route('/api/generate_quiz', methods=['POST'])
def generate_quiz_api():
"""Generate quiz questions for a subject"""
data = request.get_json()
subject = data.get('subject', 'Math')
topic = data.get('topic', 'General')
difficulty = data.get('difficulty', 1)
num_questions = data.get('num_questions', 5)
try:
# Try structured education modules first
if LEARNING_MODULES_AVAILABLE:
education_module = get_education_module(subject)
if education_module and hasattr(education_module, 'generate_quiz_questions'):
quiz_questions = education_module.generate_quiz_questions(topic, difficulty, num_questions)
if quiz_questions and not any('error' in q for q in quiz_questions):
quiz_data = {
'subject': subject,
'topic': topic,
'difficulty': difficulty,
'questions': quiz_questions,
'total_questions': len(quiz_questions),
'source': 'structured_module'
}
else:
raise Exception("Structured module failed")
else:
raise Exception("No structured module available")
else:
raise Exception("Learning modules not available")
except Exception as e:
print(f"⚠️ Structured quiz generation failed: {e}")
# Fallback to AI generation
try:
quiz_data = generate_quiz(subject, topic, difficulty, num_questions)
quiz_data['source'] = 'ai_generated'
except Exception as ai_error:
print(f"⚠️ AI quiz generation failed: {ai_error}")
# Create simple fallback quiz
quiz_data = {
'subject': subject,
'topic': topic,
'difficulty': difficulty,
'questions': [{
'question': f"What is an important concept in {subject}?",
'options': ['A) Basic understanding', 'B) Advanced theory', 'C) Practical application', 'D) All of the above'],
'correct_answer': 'D',
'explanation': f'{subject} involves understanding, theory, and practical application.'
}],
'total_questions': 1,
'source': 'fallback'
}
# Store quiz data
current_session['quiz_data'] = quiz_data
current_session['quiz_in_progress'] = True
current_session['question_index'] = 0
current_session['score'] = 0
# Hardware acknowledgment
if HARDWARE_AVAILABLE:
threading.Thread(target=lambda: speak(f"{subject} வினாடி வினாக்கள் தயார்! கேட்க தொடங்குகிறோம்!")).start()
# Log interaction
log_interaction('quiz_start', subject, topic, f"Quiz generated with {len(quiz_data['questions'])} questions")
return jsonify({
'success': True,
'quiz_data': quiz_data,
'current_question': quiz_data['questions'][0] if quiz_data['questions'] else None
})
# API Route: Submit Quiz Answer
@app.route('/api/submit_answer', methods=['POST'])
def submit_quiz_answer():
"""Submit answer for current quiz question"""
data = request.get_json()
answer = data.get('answer', '').upper()
if not current_session['quiz_in_progress'] or not current_session['quiz_data']:
return jsonify({'success': False, 'error': 'No active quiz'})
quiz_data = current_session['quiz_data']
current_question = quiz_data['questions'][current_session['question_index']]
correct_answer = current_question['correct_answer']
is_correct = answer == correct_answer
if is_correct:
current_session['score'] += 1
# Hardware feedback
if HARDWARE_AVAILABLE:
feedback_text = "சரியான பதில்!" if is_correct else "தவறான பதில். மீண்டும் முயற்சி செய்!"
threading.Thread(target=lambda: speak(feedback_text)).start()
# Move to next question
current_session['question_index'] += 1
# Check if quiz is complete
quiz_complete = current_session['question_index'] >= len(quiz_data['questions'])
next_question = None
if not quiz_complete:
next_question = quiz_data['questions'][current_session['question_index']]
else:
# Quiz completed - save results
if learning_tracker:
learning_tracker.record_quiz(
current_session['user_name'],
quiz_data['subject'],
quiz_data['topic'],
len(quiz_data['questions']),
current_session['score'],
quiz_data['difficulty']
)
current_session['quiz_in_progress'] = False
# Hardware completion message
if HARDWARE_AVAILABLE:
score_percentage = (current_session['score'] / len(quiz_data['questions'])) * 100
completion_text = f"வினாடி வினா முடிந்தது! உங்கள் மதிப்பெண்: {score_percentage:.1f}%"
threading.Thread(target=lambda: speak(completion_text)).start()
# Log interaction
log_interaction('quiz_answer', quiz_data['subject'], quiz_data['topic'],
f"Answer: {answer}, Correct: {is_correct}")
return jsonify({
'success': True,
'is_correct': is_correct,
'correct_answer': correct_answer,
'explanation': current_question.get('explanation', ''),
'score': current_session['score'],
'quiz_complete': quiz_complete,
'next_question': next_question,
'final_score_percentage': (current_session['score'] / len(quiz_data['questions'])) * 100 if quiz_complete else None
})
# API Route: Get Explanation
@app.route('/api/get_explanation', methods=['POST'])
def get_explanation_api():
"""Get explanation for a topic"""
data = request.get_json()
subject = data.get('subject', 'General')
topic = data.get('topic', 'General')
level = data.get('level', 'beginner')
try:
explanation = generate_explanation(topic, subject, level)
# Hardware speech
if HARDWARE_AVAILABLE:
# Speak a summary in Tamil
threading.Thread(target=lambda: speak(f"{topic} பற்றி விளக்கம் கொடுக்கிறேன்")).start()
# Log interaction
log_interaction('explanation', subject, topic, explanation[:200] + "...")
return jsonify({
'success': True,
'explanation': explanation,
'subject': subject,
'topic': topic
})
except Exception as e:
print(f"⚠️ Explanation generation error: {e}")
return jsonify({
'success': False,
'error': 'Unable to generate explanation',
'fallback': f"I'd like to explain {topic} in {subject}, but I'm having technical difficulties right now."
})
# API Route: Voice Interaction
@app.route('/api/voice_interaction', methods=['POST'])
def voice_interaction():
"""Handle voice input and provide voice response"""
data = request.get_json()
action = data.get('action', 'listen')
if action == 'start_listening':
current_session['listening'] = True
def listen_and_respond():
try:
# Listen for voice input
user_input = listen() # This should return the recognized speech
if user_input:
# Process with AI
if current_session['learning_mode'] == 'active':
response = get_tutor_response(user_input, current_session.get('current_subject', ''))
else:
response = get_response(user_input)
# Speak response
if HARDWARE_AVAILABLE:
speak(response)
# Log interaction
log_interaction('voice', current_session.get('current_subject', 'General'),
'Voice Input', f"User: {user_input} | Response: {response[:100]}...")
# Emit to frontend via SocketIO
socketio.emit('voice_response', {
'user_input': user_input,
'ai_response': response,
'timestamp': datetime.now().isoformat()
})
except Exception as e:
print(f"⚠️ Voice interaction error: {e}")
socketio.emit('voice_error', {'error': str(e)})
finally:
current_session['listening'] = False
# Start listening in background thread
threading.Thread(target=listen_and_respond).start()
return jsonify({'success': True, 'status': 'listening_started'})
elif action == 'stop_listening':
current_session['listening'] = False
return jsonify({'success': True, 'status': 'listening_stopped'})
# API Route: Hardware Control
@app.route('/api/hardware_control', methods=['POST'])
def hardware_control_api():
"""Control Arduino hardware"""
data = request.get_json()
action = data.get('action', '')
if not HARDWARE_AVAILABLE:
return jsonify({'success': False, 'error': 'Hardware not available'})
try:
if action == 'test_servos':
# Test servo movement
def test_servos():
for angle in [0, 90, 180, 90, 0]:
if board:
board.digital[9].write(angle)
board.digital[10].write(angle)
time.sleep(0.5)
threading.Thread(target=test_servos).start()
return jsonify({'success': True, 'message': 'Servo test started'})
elif action == 'speak_text':
text = data.get('text', 'வணக்கம்!')
threading.Thread(target=lambda: speak(text)).start()
return jsonify({'success': True, 'message': 'Speech started'})
elif action == 'celebration':
# Celebration animation with sound
def celebrate():
speak("வாழ்த்துக்கள்! நன்றாக படித்தீர்கள்!")
for i in range(3):
if board:
board.digital[9].write(180)
board.digital[10].write(0)
time.sleep(0.3)
board.digital[9].write(0)
board.digital[10].write(180)
time.sleep(0.3)
threading.Thread(target=celebrate).start()
return jsonify({'success': True, 'message': 'Celebration started'})
else:
return jsonify({'success': False, 'error': 'Unknown action'})
except Exception as e:
print(f"⚠️ Hardware control error: {e}")
return jsonify({'success': False, 'error': str(e)})
# API Route: Get System Status
@app.route('/api/status')
def get_system_status():
"""Get current system status"""
return jsonify({
'session': current_session,
'hardware_available': HARDWARE_AVAILABLE,
'learning_modules_available': LEARNING_MODULES_AVAILABLE,
'available_subjects': list_available_subjects() if LEARNING_MODULES_AVAILABLE else [],
'timestamp': datetime.now().isoformat()
})
@app.route('/api/subjects')
def get_subjects():
"""Get available subjects"""
return jsonify({
'subjects': list_available_subjects() if LEARNING_MODULES_AVAILABLE else ['Mathematics', 'Science', 'Literature', 'History'],
'success': True
})
@app.route('/api/progress/summary')
def get_progress_summary():
"""Get learning progress summary"""
try:
if learning_tracker:
# Get real progress data
progress_data = learning_tracker.get_progress_summary()
else:
# Return sample data
progress_data = {
'sessions': 12,
'studyTime': '5h 30m',
'achievements': 8,
'mathProgress': 75,
'scienceProgress': 60,
'overallProgress': 68
}
return jsonify({
'success': True,
**progress_data
})
except Exception as e:
return jsonify({
'success': False,
'error': str(e),
'sessions': 0,
'studyTime': '0h 0m',
'achievements': 0,
'mathProgress': 0,
'scienceProgress': 0,
'overallProgress': 0
})
@app.route('/api/hardware/status')
def get_hardware_status():
"""Get hardware connection status"""
try:
if arduino_controller:
connected = arduino_controller.connected if hasattr(arduino_controller, 'connected') else HARDWARE_AVAILABLE
else:
connected = False
return jsonify({
'connected': connected,
'available': HARDWARE_AVAILABLE,
'mode': 'real' if connected else 'simulation',
'success': True
})
except Exception as e:
return jsonify({
'connected': False,
'available': False,
'mode': 'simulation',
'success': False,
'error': str(e)
})
# Utility function: Log interactions
def log_interaction(interaction_type, subject, topic, content, response=''):
"""Log interaction to database"""
try:
conn = sqlite3.connect('learning_data/laura_learning.db')
cursor = conn.cursor()
cursor.execute('''
INSERT INTO interactions
(user_name, interaction_type, subject, topic, content, response, hardware_used)
VALUES (?, ?, ?, ?, ?, ?, ?)
''', (
current_session['user_name'],
interaction_type,
subject,
topic,
content,
response,
HARDWARE_AVAILABLE
))
conn.commit()
conn.close()
except Exception as e:
print(f"⚠️ Logging error: {e}")
# SocketIO Events
@socketio.on('connect')
def handle_connect():
"""Handle client connection"""
print(f"🔗 Client connected")
emit('status_update', get_system_status()['data'] if hasattr(get_system_status(), 'data') else {})
@socketio.on('disconnect')
def handle_disconnect():
"""Handle client disconnection"""
print(f"🔌 Client disconnected")
@socketio.on('request_status')
def handle_status_request():
"""Handle status request from client"""
emit('status_update', {
'session': current_session,
'hardware_available': HARDWARE_AVAILABLE,
'timestamp': datetime.now().isoformat()
})
# Error handlers
@app.errorhandler(404)
def not_found_error(error):
return render_template('404.html'), 404
@app.errorhandler(500)
def internal_error(error):
return render_template('500.html'), 500
if __name__ == '__main__':
print("🚀 Starting Laura-bot Advanced Learning Assistant...")
print(f"🔧 Hardware Status: {'Connected' if HARDWARE_AVAILABLE else 'Simulation Mode'}")
print(f"🎓 Learning Modules: {'Available' if LEARNING_MODULES_AVAILABLE else 'Limited'}")
print(f"🌐 Web Interface: http://localhost:5555")
# Run Flask app with SocketIO
socketio.run(app, host='0.0.0.0', port=5555, debug=True, allow_unsafe_werkzeug=True)