-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
1456 lines (1244 loc) · 55.5 KB
/
Copy pathbot.py
File metadata and controls
1456 lines (1244 loc) · 55.5 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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from flask import Flask, render_template, request, redirect, url_for, flash, session, jsonify, send_file
import aiml
import glob
from neo4j import GraphDatabase
from werkzeug.security import generate_password_hash, check_password_hash
import atexit
from nltk import word_tokenize, pos_tag
from nltk.corpus import stopwords
import nltk
from nltk.sentiment import SentimentIntensityAnalyzer
from collections import deque
import subprocess
import os
import re
from datetime import datetime
from gtts import gTTS
from flask import jsonify
import speech_recognition as sr
import cv2
import numpy as np
from cmake import cmake
#import face_recognition
import base64
import json
import yt_dlp
app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'a_very_long_and_secure_random_string_for_flask_security')
# YouTube playback process tracker
youtube_process = None
# Database configuration
URI = os.environ.get('NEO4J_URI', "bolt://127.0.0.1:7687")
USER = os.environ.get('NEO4J_USER', "neo4j")
PASSWORD = os.environ.get('NEO4J_PASSWORD', "custom123")
driver = None
# Conversation context storage (in-memory per session)
conversation_contexts = {}
# Audio folder configuration
AUDIO_FOLDER = r'C:\Users\user\Downloads\PythonProject\Audio'
if not os.path.exists(AUDIO_FOLDER):
os.makedirs(AUDIO_FOLDER)
# Track latest audio file for ESP32
latest_audio_file = None
# ============================================================================
# EMOTIONAL CONTEXT CLASS
# ============================================================================
class EmotionalContext:
def __init__(self, username):
self.username = username
self.emotion_history = deque(maxlen=10)
self.topics_mentioned = set()
self.crisis_indicators = 0
self.overall_mood_score = 0.0
self.conversation_start = datetime.now()
self.last_topic = None
def add_emotion(self, mood, intensity, compound, message):
self.emotion_history.append({
'mood': mood,
'intensity': intensity,
'compound': compound,
'message': message.lower(),
'timestamp': datetime.now()
})
words = message.lower().split()
topics = ['work', 'family', 'friend', 'relationship', 'school', 'job', 'health', 'money', 'life', 'love']
for topic in topics:
if topic in words or f"{topic}s" in words:
self.topics_mentioned.add(topic)
self.last_topic = topic
crisis_words = ['suicide', 'kill myself', 'end it', 'die', 'hurt myself', 'no point', 'give up']
if any(phrase in message.lower() for phrase in crisis_words):
self.crisis_indicators += 1
if len(self.emotion_history) > 0:
self.overall_mood_score = sum(e['compound'] for e in self.emotion_history) / len(self.emotion_history)
def get_mood_trend(self):
if len(self.emotion_history) < 3:
return 'unknown'
recent_scores = [e['compound'] for e in list(self.emotion_history)[-3:]]
if recent_scores[-1] > recent_scores[0] + 0.2:
return 'improving'
elif recent_scores[-1] < recent_scores[0] - 0.2:
return 'worsening'
else:
return 'stable'
def count_recent_sad_messages(self):
if len(self.emotion_history) == 0:
return 0
return sum(1 for e in self.emotion_history if e['mood'] == 'sad')
def is_in_crisis(self):
return self.crisis_indicators > 0 or (self.overall_mood_score < -0.6 and self.count_recent_sad_messages() >= 3)
# ============================================================================
# SENTIMENT ANALYSIS FUNCTIONS
# ============================================================================
def init_sentiment_analyzer():
try:
nltk.download('vader_lexicon', quiet=True)
nltk.download('punkt', quiet=True)
sia = SentimentIntensityAnalyzer()
print("✅ Sentiment analyzer initialized successfully.")
return sia
except Exception as e:
print(f"⚠️ Warning: Could not initialize sentiment analyzer: {e}")
return None
sentiment_analyzer = init_sentiment_analyzer()
def analyze_sentiment(text):
if not sentiment_analyzer:
return 'neutral', 'moderate', 0.0
try:
scores = sentiment_analyzer.polarity_scores(text)
compound = scores['compound']
text_lower = text.lower()
if 'cry' in text_lower and any(
word in text_lower for word in ['want', 'wana', 'wanna', 'wan', 'going', 'gonna', 'need', 'feel']):
compound = min(compound, -0.5)
if any(word in text_lower for word in ['die', 'suicide', 'kill myself', 'end it', 'give up']):
compound = -0.8
if 'very sad' in text_lower or 'so sad' in text_lower or 'extremely sad' in text_lower:
compound = min(compound, -0.6)
if 'very happy' in text_lower or 'so happy' in text_lower:
compound = max(compound, 0.6)
if compound >= 0.05:
mood = 'happy'
if compound >= 0.6:
intensity = 'very'
elif compound >= 0.3:
intensity = 'quite'
else:
intensity = 'somewhat'
elif compound <= -0.05:
mood = 'sad'
if compound <= -0.6:
intensity = 'very'
elif compound <= -0.3:
intensity = 'quite'
else:
intensity = 'somewhat'
else:
mood = 'neutral'
intensity = 'moderate'
print(f"📊 Sentiment: '{text[:40]}...' → {compound:.3f} → {mood} ({intensity})")
return mood, intensity, compound
except Exception as e:
print(f"Error analyzing sentiment: {e}")
return 'neutral', 'moderate', 0.0
def extract_topics(text):
stop_words = set(stopwords.words('english'))
words = word_tokenize(text.lower())
tagged = pos_tag(words)
topics = set()
for word, tag in tagged:
if tag.startswith('NN') and word.isalpha():
if word not in stop_words and len(word) > 2:
topics.add(word)
return topics
def extract_emotion_keywords(text):
text_lower = text.lower()
emotions_found = {
'sad': ['sad', 'depressed', 'down', 'miserable', 'unhappy', 'upset', 'hurt'],
'anxious': ['anxious', 'worried', 'nervous', 'scared', 'afraid', 'panic', 'stress'],
'angry': ['angry', 'mad', 'furious', 'annoyed', 'frustrated', 'irritated'],
'happy': ['happy', 'joyful', 'excited', 'glad', 'wonderful', 'great', 'amazing'],
'lonely': ['lonely', 'alone', 'isolated', 'abandoned'],
'hopeless': ['hopeless', 'pointless', 'worthless', 'meaningless']
}
detected = []
for emotion, keywords in emotions_found.items():
if any(keyword in text_lower for keyword in keywords):
detected.append(emotion)
return detected
from nltk.corpus import wordnet
def get_definition(term):
synsets = wordnet.synsets(term)
if synsets:
return synsets[0].definition()
return f"I couldn't find a clear definition for {term}."
def set_context_predicates(query, context, mood, intensity, compound, username, k):
query_lower = query.lower().strip()
words = query_lower.split()
term = words[-1] if len(words) > 1 else query_lower
definition = get_definition(term)
trend = context.get_mood_trend()
sad_count = context.count_recent_sad_messages()
k.setPredicate('mood', mood, username)
k.setPredicate('intensity', intensity, username)
k.setPredicate('compound', str(round(compound, 3)), username)
k.setPredicate('username', username, username)
k.setPredicate('email', session.get('email', ''), username)
k.setPredicate('definition', definition, username)
k.setPredicate('mood_trend', trend, username)
k.setPredicate('sad_message_count', str(sad_count), username)
k.setPredicate('overall_mood_score', str(round(context.overall_mood_score, 3)), username)
k.setPredicate('last_topic', context.last_topic or 'none', username)
k.setPredicate('is_crisis', 'yes' if context.is_in_crisis() else 'no', username)
emotions = extract_emotion_keywords(query)
k.setPredicate('detected_emotions', ','.join(emotions) if emotions else 'none', username)
k.setPredicate('is_single_word', 'yes' if len(query_lower.split()) == 1 else 'no', username)
k.setPredicate('contains_cry', 'yes' if 'cry' in query_lower else 'no', username)
k.setPredicate('contains_die', 'yes' if any(w in query_lower for w in ['die', 'suicide', 'kill']) else 'no',
username)
k.setPredicate('contains_why', 'yes' if query_lower.startswith('why') else 'no', username)
k.setPredicate('is_question', 'yes' if '?' in query else 'no', username)
k.setPredicate('sentiment', mood, username)
print(
f"🎭 Predicates: mood={mood}/{intensity}, trend={trend}, sad_count={sad_count}, crisis={context.is_in_crisis()}")
# ============================================================================
# DATABASE FUNCTIONS
# ============================================================================
def init_database():
global driver
try:
driver = GraphDatabase.driver(URI, auth=(USER, PASSWORD))
driver.verify_connectivity()
print("✅ Neo4j database connected successfully.")
with driver.session() as session_db:
session_db.execute_write(ensure_agent_and_creator)
return True
except Exception as e:
print(f"❌ Error connecting to Neo4j: {e}")
driver = None
return False
@atexit.register
def shutdown():
if driver:
driver.close()
print("Neo4j driver closed")
# ============================================================================
# AIML FUNCTIONS
# ============================================================================
def init_aiml():
k = aiml.Kernel()
all_files = glob.glob("./data/*.aiml")
alice_files = [f for f in all_files if 'emotion' not in f.lower()]
emotion_files = [f for f in all_files if 'emotion' in f.lower()]
loaded_count = 0
print("📚 Loading ALICE brain...")
for f in sorted(alice_files):
try:
k.learn(f)
loaded_count += 1
except Exception as e:
pass
print("🎭 Loading emotion patterns (PRIORITY OVERRIDE)...")
for f in sorted(emotion_files):
try:
k.learn(f)
loaded_count += 1
print(f" ✅ {os.path.basename(f)}")
except Exception as e:
print(f" ⚠️ Error loading {f}: {str(e)[:50]}")
BRAIN_FILE = "./pretrained_model/aiml_pretrained_model.dump"
if os.path.exists(BRAIN_FILE):
try:
k.loadBrain(BRAIN_FILE)
print(f"🧠 Loaded pretrained brain from {BRAIN_FILE}")
except Exception as e:
print(f"⚠️ Could not load brain dump: {e}")
print(f"📚 Total: {loaded_count} AIML files loaded")
return k
k = init_aiml()
# ============================================================================
# NEO4J QUERIES
# ============================================================================
def create_user_node(tx, username, email, password_hash, first_name, last_name, gender, date_of_birth,
face_encoding=None):
query = """
CREATE (u:Person {
username: $username,
email: $email,
password_hash: $password_hash,
first_name: $first_name,
last_name: $last_name,
gender: $gender,
date_of_birth: $date_of_birth,
face_encoding: $face_encoding,
created_at: $created_at,
last_login: $created_at
})
RETURN u
"""
tx.run(query,
username=username,
email=email,
password_hash=password_hash,
first_name=first_name,
last_name=last_name,
gender=gender,
date_of_birth=date_of_birth,
face_encoding=face_encoding,
created_at=datetime.now().isoformat())
def find_user_by_username_or_email(tx, identifier):
query = """
MATCH (u:Person)
WHERE u.username = $identifier OR u.email = $identifier
RETURN u.username AS username, u.email AS email, u.password_hash AS password_hash,
u.first_name AS first_name, u.last_name AS last_name, u.gender AS gender,
u.date_of_birth AS date_of_birth, u.face_encoding AS face_encoding
"""
result = tx.run(query, identifier=identifier).single()
return result
def get_user_for_login(tx, username):
query = """
MATCH (u:Person)
WHERE u.username = $username
RETURN u.username AS username, u.email AS email, u.password_hash AS password_hash,
u.first_name AS first_name, u.last_name AS last_name, u.gender AS gender,
u.date_of_birth AS date_of_birth, u.face_encoding AS face_encoding
"""
result = tx.run(query, username=username).single()
return result
def ensure_agent_and_creator(tx):
query = """
MERGE (c:Person {name: 'Ali'})
MERGE (a:Agent {name: 'Yone'})
MERGE (c)-[:CREATED]->(a)
"""
tx.run(query)
def save_chat_with_agent(tx, username, message, sender, timestamp, session_id, sentiment=None, intensity=None,
compound=None):
if sender == "user":
query = """
MATCH (u:Person {username: $username})
MATCH (s:Session {session_id: $session_id})
CREATE (m:Message {
text: $message,
sender: 'user',
timestamp: $timestamp,
sentiment: $sentiment,
intensity: $intensity,
compound: $compound
})
CREATE (u)-[:SENT]->(m)
CREATE (s)-[:HAS_MESSAGE]->(m)
RETURN id(m) AS message_id
"""
result = tx.run(query,
username=username,
session_id=session_id,
message=message,
timestamp=timestamp,
sentiment=sentiment,
intensity=intensity,
compound=compound)
return result.single()["message_id"]
else:
query = """
MATCH (a:Agent {name: 'Yone'})
MATCH (s:Session {session_id: $session_id})
CREATE (m:Message {
text: $message,
sender: 'bot',
timestamp: $timestamp
})
CREATE (a)-[:REPLIED]->(m)
CREATE (s)-[:HAS_MESSAGE]->(m)
RETURN id(m) AS message_id
"""
result = tx.run(query,
session_id=session_id,
message=message,
timestamp=timestamp)
return result.single()["message_id"]
def get_chat_history(tx, username, limit=50):
query = """
MATCH (u:Person {username: $username})-[:STARTED]->(s:Session)
WHERE s.active = true
MATCH (s)-[:HAS_MESSAGE]->(m:Message)
RETURN m.text AS text,
m.sender AS sender,
m.timestamp AS timestamp,
m.sentiment AS sentiment
ORDER BY m.timestamp DESC
LIMIT $limit
"""
result = tx.run(query, username=username, limit=limit)
return [dict(record) for record in result]
# ============================================================================
# FACE RECOGNITION FUNCTIONS
# ============================================================================
@app.route("/process_face", methods=["POST"])
def process_face():
try:
data = request.json
image_data = data['image'].split(',')[1] # Remove base64 header
image_bytes = base64.b64decode(image_data)
# Convert to numpy array
nparr = np.frombuffer(image_bytes, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
# Convert to RGB (face_recognition uses RGB)
rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Get face encodings
face_locations = face_recognition.face_locations(rgb_img)
if not face_locations:
return jsonify({"success": False, "error": "No face detected"})
face_encodings = face_recognition.face_encodings(rgb_img, face_locations)
if not face_encodings:
return jsonify({"success": False, "error": "Could not encode face"})
# Return the first face encoding as list
return jsonify({
"success": True,
"encoding": face_encodings[0].tolist()
})
except Exception as e:
print(f"Face processing error: {e}")
return jsonify({"success": False, "error": str(e)})
@app.route("/face_login", methods=["POST"])
def face_login():
try:
data = request.json
login_encoding = np.array(data['encoding'])
# Get all users with face encodings
with driver.session() as session_db:
result = session_db.run("""
MATCH (u:Person)
WHERE u.face_encoding IS NOT NULL
RETURN u
""")
for record in result:
user = record['u']
stored_encoding = np.array(json.loads(user['face_encoding']))
# Compare faces
match = face_recognition.compare_faces([stored_encoding], login_encoding, tolerance=0.5)
if match[0]:
# Login successful
session['logged_in'] = True
session['username'] = user['username']
session['email'] = user['email']
session['first_name'] = user['first_name']
session['last_name'] = user['last_name']
session['gender'] = user['gender']
session['date_of_birth'] = user['date_of_birth']
# Create chat session
chat_session_id = str(datetime.now().timestamp())
session_db.run("""
MATCH (u:Person {username: $username})
MATCH (a:Agent {name: 'Yone'})
CREATE (s:Session {
session_id: $session_id,
start_time: $start_time,
active: true
})
CREATE (u)-[:STARTED]->(s)
CREATE (a)-[:PARTICIPATED_IN]->(s)
""", username=user['username'], session_id=chat_session_id,
start_time=datetime.now().isoformat())
session['chat_session_id'] = chat_session_id
return jsonify({"success": True})
return jsonify({"success": False, "error": "Face not recognized"})
except Exception as e:
print(f"Face login error: {e}")
return jsonify({"success": False, "error": str(e)})
# ============================================================================
# ROUTES
# ============================================================================
@app.route("/", methods=['GET', 'POST'])
def home():
if session.get('logged_in'):
return render_template("home.html")
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
if not username or not password:
flash('⚠️ Please enter both username and password.', 'error')
return redirect(url_for('home'))
if not driver:
flash('❌ Database connection is not available. Please try again later.', 'error')
return redirect(url_for('home'))
try:
with driver.session() as session_db:
user_record = session_db.execute_read(get_user_for_login, username=username)
if not user_record:
flash(f'❌ User "{username}" not found. Please sign up first!', 'error')
return redirect(url_for('home'))
if check_password_hash(user_record['password_hash'], password):
session['logged_in'] = True
session['username'] = username
session['email'] = user_record['email']
session['first_name'] = user_record['first_name']
session['last_name'] = user_record['last_name']
session['gender'] = user_record['gender']
session['date_of_birth'] = user_record['date_of_birth']
update_last_login_query = """
MATCH (u:Person {username: $username})
SET u.last_login = $last_login
RETURN u
"""
session_db.run(update_last_login_query,
username=username,
last_login=datetime.now().isoformat())
chat_session_id = str(datetime.now().timestamp())
create_session_query = """
MATCH (u:Person {username: $username})
MATCH (a:Agent {name: 'Yone'})
CREATE (s:Session {
session_id: $session_id,
start_time: $start_time,
active: true
})
CREATE (u)-[:STARTED]->(s)
CREATE (a)-[:PARTICIPATED_IN]->(s)
RETURN s.session_id AS session_id
"""
session_db.run(create_session_query,
username=username,
session_id=chat_session_id,
start_time=datetime.now().isoformat())
session['chat_session_id'] = chat_session_id
flash(f'🎉 Welcome back, {username}!', 'success')
return redirect(url_for('home'))
else:
flash(f'❌ Incorrect password for user "{username}". Please try again.', 'error')
return redirect(url_for('home'))
except Exception as e:
flash(f'❌ Database error: {str(e)}', 'error')
return redirect(url_for('home'))
return render_template("home.html")
@app.route('/register', methods=['GET', 'POST'])
def register_page():
if request.method == 'POST':
username = request.form.get('username')
email = request.form.get('email')
password = request.form.get('password')
first_name = request.form.get('first_name')
last_name = request.form.get('last_name')
gender = request.form.get('gender')
date_of_birth = request.form.get('date_of_birth')
face_encoding = request.form.get('face_encoding') # Optional face encoding
required_fields = {
'username': username,
'email': email,
'password': password,
'first_name': first_name,
'last_name': last_name,
'gender': gender,
'date_of_birth': date_of_birth
}
missing_fields = [field for field, value in required_fields.items() if not value]
if missing_fields:
flash(f'⚠️ Please fill in all fields: {", ".join(missing_fields)}', 'error')
return redirect(url_for('register_page'))
if len(password) < 6:
flash('⚠️ Password must be at least 6 characters long!', 'error')
return redirect(url_for('register_page'))
try:
birth_date = datetime.strptime(date_of_birth, '%Y-%m-%d')
today = datetime.now()
age = today.year - birth_date.year - ((today.month, today.day) < (birth_date.month, birth_date.day))
if age < 13:
flash('⚠️ You must be at least 13 years old to register!', 'error')
return redirect(url_for('register_page'))
except ValueError:
flash('⚠️ Invalid date of birth format!', 'error')
return redirect(url_for('register_page'))
if not driver:
flash('❌ Database connection is not available. Cannot register user.', 'error')
return redirect(url_for('register_page'))
try:
with driver.session() as session_db:
existing_user = session_db.execute_read(find_user_by_username_or_email, identifier=username)
if existing_user:
flash(f'❌ Username "{username}" already exists. Please choose a different one.', 'error')
return redirect(url_for('register_page'))
existing_email = session_db.execute_read(find_user_by_username_or_email, identifier=email)
if existing_email:
flash(f'❌ Email "{email}" is already registered. Please use a different email.', 'error')
return redirect(url_for('register_page'))
password_hash = generate_password_hash(password)
session_db.execute_write(
create_user_node,
username=username,
email=email,
password_hash=password_hash,
first_name=first_name,
last_name=last_name,
gender=gender,
date_of_birth=date_of_birth,
face_encoding=face_encoding
)
flash(f'✅ Account created successfully for "{username}"! Please log in.', 'success')
return redirect(url_for('home'))
except Exception as e:
print(f"Registration error: {e}")
flash(f'❌ Registration failed: {str(e)}', 'error')
return redirect(url_for('register_page'))
today = datetime.now()
max_date = datetime(today.year - 13, today.month, today.day).strftime('%Y-%m-%d')
return render_template('signup.html', max_date=max_date)
@app.route('/logout')
def logout():
username = session.get('username', 'User')
chat_session_id = session.get('chat_session_id')
if chat_session_id and driver:
with driver.session() as session_db:
close_query = """
MATCH (s:Session {session_id: $session_id})
SET s.active = false,
s.end_time = $end_time
"""
session_db.run(close_query,
session_id=chat_session_id,
end_time=datetime.now().isoformat())
if username in conversation_contexts:
del conversation_contexts[username]
session.clear()
flash(f'👋 Goodbye {username}! You have been logged out.', 'success')
return redirect(url_for('home'))
@app.route("/history")
def get_history():
if not session.get('logged_in'):
return jsonify([])
username = session.get('username')
try:
if driver:
with driver.session() as session_db:
history = session_db.execute_read(get_chat_history, username)
return jsonify(history)
except Exception as e:
print(f"Error fetching history: {e}")
return jsonify([])
@app.route("/sentiment-stats")
def get_sentiment_stats():
if not session.get('logged_in'):
return jsonify({'error': 'Not logged in'})
username = session.get('username')
try:
if driver:
with driver.session() as session_db:
query = """
MATCH (u:Person {username: $username})-[:SENT]->(m:Message {sender: 'user'})
WHERE m.sentiment IS NOT NULL
RETURN m.sentiment AS sentiment, count(*) AS count
ORDER BY count DESC
"""
result = session_db.run(query, username=username)
stats = []
for record in result:
stats.append({
'sentiment': record['sentiment'],
'count': record['count']
})
return jsonify(stats)
except Exception as e:
print(f"Error fetching sentiment stats: {e}")
return jsonify({})
@app.route("/emotion-context")
def get_emotion_context():
if not session.get('logged_in'):
return jsonify({'error': 'Not logged in'})
username = session.get('username')
if username not in conversation_contexts:
return jsonify({'error': 'No context found'})
context = conversation_contexts[username]
return jsonify({
'mood_trend': context.get_mood_trend(),
'sad_count': context.count_recent_sad_messages(),
'overall_mood': context.overall_mood_score,
'topics': list(context.topics_mentioned),
'last_topic': context.last_topic,
'crisis_level': context.is_in_crisis()
})
@app.route("/debug-predicates")
def debug_predicates():
if not session.get('logged_in'):
return jsonify({'error': 'Not logged in'})
username = session.get('username')
predicates = {
'mood': k.getPredicate('mood', username),
'intensity': k.getPredicate('intensity', username),
'compound': k.getPredicate('compound', username),
'mood_trend': k.getPredicate('mood_trend', username),
'sad_message_count': k.getPredicate('sad_message_count', username),
'last_topic': k.getPredicate('last_topic', username),
'is_crisis': k.getPredicate('is_crisis', username),
'detected_emotions': k.getPredicate('detected_emotions', username),
'contains_cry': k.getPredicate('contains_cry', username),
'contains_die': k.getPredicate('contains_die', username),
'definition': k.getPredicate('definition', username),
'sentiment': k.getPredicate('sentiment', username),
}
return jsonify(predicates)
@app.route("/profile")
def get_profile():
if not session.get('logged_in'):
return jsonify({'error': 'Not logged in'})
profile = {
'username': session.get('username'),
'email': session.get('email'),
'first_name': session.get('first_name'),
'last_name': session.get('last_name'),
'gender': session.get('gender'),
'date_of_birth': session.get('date_of_birth')
}
return jsonify(profile)
# ============================================================================
# ESP32 AUDIO ENDPOINTS
# ============================================================================
@app.route("/check_audio")
def check_audio():
"""Check if new audio is available for ESP32"""
global latest_audio_file
if latest_audio_file and os.path.exists(os.path.join(AUDIO_FOLDER, latest_audio_file)):
return "READY"
return "EMPTY"
@app.route("/esp32_audio")
def esp32_audio():
"""Serve the latest audio file to ESP32"""
global latest_audio_file
if latest_audio_file:
file_path = os.path.join(AUDIO_FOLDER, latest_audio_file)
if os.path.exists(file_path):
return send_file(file_path, mimetype='audio/wav')
return "No audio", 404
@app.route("/clear_audio")
def clear_audio():
"""Clear the audio file after ESP32 plays it"""
global latest_audio_file
if latest_audio_file:
file_path = os.path.join(AUDIO_FOLDER, latest_audio_file)
if os.path.exists(file_path):
os.remove(file_path)
print(f"🗑️ Deleted: {file_path}")
latest_audio_file = None
return "CLEARED"
return "EMPTY"
@app.route("/whisper", methods=["POST"])
def transcribe_voice():
return jsonify({"error": "Whisper integration not configured"}), 501
# ============================================================================
# MAIN BOT RESPONSE ENDPOINT
# ============================================================================
@app.route("/get")
def get_bot_response():
if not session.get('logged_in'):
return jsonify({"text": "Please log in to chat with the bot.", "audio_url": ""})
global youtube_process
query = request.args.get('msg')
if not query:
return jsonify({"text": "Please enter a message.", "audio_url": ""})
username = session.get('username', 'User')
email = session.get('email', '')
first_name = session.get('first_name', '')
last_name = session.get('last_name', '')
gender = session.get('gender', '')
# Initialize or get conversation context
if username not in conversation_contexts:
conversation_contexts[username] = EmotionalContext(username)
context = conversation_contexts[username]
# ---------- SENTIMENT ANALYSIS ----------
user_mood, intensity, compound_score = analyze_sentiment(query)
# Update emotional + topic context
context.add_emotion(user_mood, intensity, compound_score, query)
# Set predicates for AIML
set_context_predicates(
query,
context,
user_mood,
intensity,
compound_score,
username,
k
)
query_lower = query.lower()
response = None
handled = False
# ---------- YOUTUBE PLAY / STOP ----------
# ---------- YOUTUBE PLAY / STOP ----------
def play_youtube_song(song_name):
"""Search YouTube for the song and play it using yt-dlp and VLC."""
global youtube_process
# Stop any currently playing song first
if youtube_process and youtube_process.poll() is None:
youtube_process.terminate()
youtube_process = None
time.sleep(0.5) # Brief pause to ensure it stops
audio_url = None
video_title = "Unknown"
# Method 1: Try yt_dlp Python module (most reliable)
try:
import yt_dlp
print(f"📦 Using yt-dlp to find: '{song_name}'")
# Configure yt-dlp options
ydl_opts = {
'format': 'bestaudio/best',
'quiet': True,
'no_warnings': True,
'noplaylist': True,
'default_search': 'ytsearch1', # Get first result only
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
# Search for the song
info = ydl.extract_info(f'ytsearch1:{song_name}', download=False)
if info and 'entries' in info and info['entries']:
entry = info['entries'][0]
video_title = entry.get('title', song_name)
print(f"🎵 Found: {video_title}")
# Get the best audio URL
if 'url' in entry:
audio_url = entry['url']
elif 'formats' in entry:
# Find the best audio-only format
for fmt in entry['formats']:
if fmt.get('acodec') != 'none' and fmt.get('vcodec') == 'none':
audio_url = fmt['url']
break
# If no audio-only format, get any format with audio
if not audio_url:
for fmt in entry['formats']:
if fmt.get('acodec') != 'none':
audio_url = fmt['url']
break
if audio_url:
print(f"✅ Got audio URL (first 50 chars): {audio_url[:50]}...")
else:
print("❌ Could not extract audio URL from video")
else:
print("❌ No search results found")
except ImportError:
print("⚠️ yt-dlp module not installed. Please install: pip install yt-dlp")
return None
except Exception as e:
print(f"⚠️ yt-dlp error: {e}")
return None
if not audio_url:
print("❌ Failed to get audio URL")
return None
# ===== VLC PATHS - UPDATE THIS WITH YOUR ACTUAL PATH =====
# Use the path from your test
vlc_paths = [
r'C:\Program Files\VideoLAN\VLC\vlc.exe', # Most common
r'C:\Program Files (x86)\VideoLAN\VLC\vlc.exe', # 32-bit on 64-bit
r'C:\VLC\vlc.exe', # Alternative install
os.path.expanduser('~/AppData/Local/Programs/VLC/vlc.exe'), # User install
'vlc' # Fallback to PATH
]
# Try each VLC path
for vlc_path in vlc_paths:
try:
# Check if file exists (for absolute paths)
if vlc_path != 'vlc' and not os.path.exists(vlc_path):
continue
print(f"🔄 Trying VLC: {vlc_path}")
# Launch VLC in background mode
youtube_process = subprocess.Popen(
[vlc_path, '--intf', 'dummy', '--play-and-exit', audio_url],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
# Verify it started
time.sleep(1)
if youtube_process.poll() is None:
print(f"✅ Successfully playing with VLC!")
return audio_url
else:
print(f"⚠️ VLC process exited immediately")
except FileNotFoundError:
continue
except Exception as e:
print(f"⚠️ Error with {vlc_path}: {e}")
continue
# If all VLC paths fail, try alternative players