-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
1218 lines (1127 loc) · 42.7 KB
/
bot.py
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
import streamlit as st
import os
import re
from dotenv import load_dotenv
from langchain.schema import HumanMessage, SystemMessage, AIMessage
from langchain.chat_models import AzureChatOpenAI,ChatOpenAI
from langchain.memory import ConversationBufferWindowMemory
from langchain.prompts import PromptTemplate
import warnings
import time
from sqlalchemy import create_engine, Column, Integer, String, Text, Table, MetaData
from sqlalchemy.orm import sessionmaker
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from dotenv import load_dotenv
from langchain_google_genai import ChatGoogleGenerativeAI
warnings.filterwarnings("ignore", category=DeprecationWarning)
# Load environment variables
load_dotenv()
st.set_page_config(page_title="MindMate", layout="wide", initial_sidebar_state="expanded")
api_key = os.getenv("api_key")
# CSS styles
css = '''
<style>
/* General styles */
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
/* Sidebar styles */
.sidebar-content {
display: flex;
flex-direction: column;
height: 100%;
padding: 1rem;
background-color: #f0f2f6;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.sidebar-item {
padding: 1rem;
margin: 0.5rem 0;
cursor: pointer;
transition: background-color 0.3s, transform 0.3s;
display: flex;
align-items: center;
border-radius: 10px;
}
.sidebar-item:hover {
background-color: #e0e0e0;
transform: scale(1.05);
}
.sidebar-item.active {
background-color: #d0d0d0;
}
.sidebar-item .icon {
margin-right: 10px;
font-size: 1.5rem;
}
.sidebar-item .label {
font-size: 1.2rem;
font-weight: bold;
}
/* Chat styles */
.chat-message {
padding: 1.5rem;
border-radius: 0.5rem;
margin-bottom: 1rem;
display: flex;
}
.chat-message.user {
align-self: flex-end;
background-color: #2b313e;
}
.chat-message.bot {
background-color: #475063;
}
.chat-message .avatar {
width: 20%;
}
.chat-message .avatar img {
max-width: 78px;
max-height: 78px;
border-radius: 50%;
object-fit: cover;
}
.chat-message .message {
width: 80%;
padding: 0 1.5rem;
color: #fff;
}
/* Session card styles */
.session-card {
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: center;
padding: 1.5rem;
margin: 1rem;
background-color: #ffffff;
border-radius: 15px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
transition: transform 0.3s, box-shadow 0.3s;
text-align: center;
}
.session-card:hover {
transform: translateY(-5px);
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
}
.session-card .session-info {
display: flex;
flex-direction: column;
align-items: center;
text-align: center;
}
.session-card .session-info .session-title {
font-weight: bold;
margin-bottom: 0.5rem;
color: #333;
font-size: 1.2rem;
}
.session-card .session-info .session-subtitle {
color: #666;
margin-bottom: 0.5rem;
font-size: 1rem;
}
.session-card .session-info .session-time {
color: #aaa;
margin-bottom: 1rem;
font-size: 0.9rem;
}
.session-card .buttons {
display: flex;
justify-content: space-between;
width: 100%;
}
.session-card .buttons button {
background-color: #007BFF;
color: white;
border: none;
padding: 0.5rem 1rem;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
width: 45%;
font-size: 0.9rem;
}
.session-card .buttons button:hover {
background-color: #0056b3;
}
.delete-button {
background-color: #dc3545;
}
.delete-button:hover {
background-color: #c82333;
}
.new-session-card {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
padding: 1rem;
margin: 1rem;
background-color: #e0e0e0;
border-radius: 15px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
cursor: pointer;
transition: transform 0.3s, box-shadow 0.3s;
width: 220px;
height: 150px;
}
.new-session-card:hover {
transform: translateY(-5px);
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
}
.new-session-card .new-session-icon {
font-size: 2rem;
color: #666;
margin-bottom: 0.5rem;
}
.new-session-card .new-session-text {
color: #666;
font-weight: bold;
text-align: center;
}
/* Tool card styles */
.tool-card {
background-color: #ffffff;
border-radius: 15px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
padding: 1.5rem;
margin: 1rem;
transition: transform 0.3s, box-shadow 0.3s;
text-align: center;
}
.tool-card:hover {
transform: translateY(-5px);
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
}
.tool-card .tool-title {
font-weight: bold;
margin-bottom: 0.5rem;
color: #333;
font-size: 1.2rem;
}
.tool-card .tool-description {
color: #666;
margin-bottom: 0.5rem;
font-size: 1rem;
}
.tool-card .tool-time {
color: #aaa;
margin-bottom: 1rem;
font-size: 0.9rem;
}
.tool-card .start-button {
background-color: #007BFF;
color: white;
border: none;
padding: 0.5rem 1rem;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
.tool-card .start-button:hover {
background-color: #0056b3;
}
/* Therapist card styles */
.therapist-card {
background-color: #ffffff;
border-radius: 15px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
padding: 1.5rem;
margin: 1rem;
transition: transform 0.3s, box-shadow 0.3s;
text-align: center;
}
.therapist-card:hover {
transform: translateY(-5px);
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
}
.therapist-card img {
max-width: 100%;
border-radius: 50%;
margin-bottom: 1rem;
}
.therapist-card .therapist-name {
font-weight: bold;
margin-bottom: 0.5rem;
color: #333;
font-size: 1.2rem;
}
.therapist-card .therapist-description {
color: #666;
margin-bottom: 0.5rem;
font-size: 1rem;
}
.therapist-card .select-button {
background-color: #007BFF;
color: white;
border: none;
padding: 0.5rem 1rem;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
.therapist-card .select-button:hover {
background-color: #0056b3;
}
/* Today page styles */
.today-page {
display: flex;
flex-direction: column;
align-items: center;
padding: 2rem;
}
.today-page .header {
text-align: center;
margin-bottom: 2rem;
}
.today-page .header h1 {
font-size: 2.5rem;
color: #333;
margin-bottom: 0.5rem;
}
.today-page .header p {
font-size: 1.2rem;
color: #666;
}
.today-page .content {
display: flex;
justify-content: space-around;
flex-wrap: wrap;
}
.today-page .card {
background-color: #ffffff;
border-radius: 15px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
padding: 1rem;
margin: 1rem;
text-align: center;
transition: transform 0.3s, box-shadow 0.3s;
width: 300px;
}
.today-page .card:hover {
transform: translateY(-5px);
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
}
.today-page .card img {
max-width: 100%;
border-radius: 15px;
margin-bottom: 1rem;
}
.today-page .card h3 {
font-size: 1.5rem;
color: #333;
margin-bottom: 0.5rem;
}
.today-page .card p {
font-size: 1rem;
color: #666;
}/* How to use page styles */
.how-to-use-page {
font-family: 'Poppins', sans-serif;
color: #FAFAFA;
background-color: #0E1117;
padding: 2rem;
border-radius: 15px;
}
.how-to-use-page h2 {
font-size: 2.5rem;
color: #FF4B4B;
text-align: center;
margin-bottom: 1rem;
text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2);
}
.how-to-use-page p {
font-size: 1.2rem;
margin-bottom: 1.5rem;
line-height: 1.6;
}
.how-to-use-page .section {
margin-bottom: 2rem;
}
.how-to-use-page .section h3 {
font-size: 1.8rem;
color: #FF6F61;
margin-bottom: 0.5rem;
}
.how-to-use-page .section ul {
list-style-type: none;
padding-left: 0;
}
.how-to-use-page .section ul li {
margin-bottom: 0.75rem;
}
.how-to-use-page .section ul li:before {
content: "✔️";
margin-right: 0.5rem;
color: #FF4B4B;
}
.how-to-use-page img {
max-width: 100%;
border-radius: 10px;
margin-top: 1rem;
margin-bottom: 1rem;
}
</style>
'''
bot_template = '''
<div class="chat-message bot">
<div class="avatar">
<img src="https://cdn-icons-png.flaticon.com/512/6134/6134346.png" style="max-height: 78px; max-width: 78px; border-radius: 50%; object-fit: cover;">
</div>
<div class="message">{{MSG}}</div>
</div>
'''
user_template = '''
<div class="chat-message user">
<div class="message" style="text-align:right">{{MSG}}</div>
<div class="avatar">
<img src="https://png.pngtree.com/png-vector/20190321/ourmid/pngtree-vector-users-icon-png-image_856952.jpg">
</div>
</div>
'''
# CSS
st.write(css, unsafe_allow_html=True)
# Initialize the database
engine = create_engine('sqlite:///sessions.db')
metadata = MetaData()
sessions_table = Table(
'sessions', metadata,
Column('id', Integer, primary_key=True),
Column('title', String),
Column('subtitle', String),
Column('time', String),
Column('messages', String)
)
print("Table\n")
print(sessions_table)
metadata.create_all(engine)
DBSession = sessionmaker(bind=engine)
db_session = DBSession()
st.session_state['chat'] = ChatGoogleGenerativeAI(model="gemini-1.5-pro",temperature=0,api_key=api_key,convert_system_message_to_human=True)
# Sidebar navigation with model selection
def sidebar():
st.sidebar.header("Navigation")
pages = ["Today", "Sessions", "Tools", "Therapists", "Insights", "Settings", "How to use?"]
icons = ["📅", "💬", "🛠️", "👥", "📊", "⚙️", "❓"]
selected_page = st.sidebar.selectbox(
"",
[f"{icons[i]} {pages[i]}" for i in range(len(pages))],
format_func=lambda x: x.split(" ", 1)[1]
)
return selected_page.split(" ", 1)[1]
page = sidebar()
# Initialize the selected model and API key
if 'api_key' not in st.session_state:
st.session_state['api_key'] = None
if 'chat' not in st.session_state:
st.session_state['chat'] = None
# Function to load sessions from the database
def load_sessions():
sessions = []
for s in db_session.query(sessions_table).all():
session = {
"id": s.id,
"title": s.title,
"subtitle": s.subtitle,
"time": s.time,
"messages": eval(s.messages) if s.messages else []
}
print(f"Loaded session: {session}") # Debug statement
sessions.append(session)
return sessions
# Function to save session to the database
def save_session(session):
print(f"Saving session: {session}") # Debug statement
db_session.query(sessions_table).filter_by(id=session['id']).update({
'title': session['title'],
'subtitle': session['subtitle'],
'time': session['time'],
'messages': str(session['messages'])
})
db_session.commit()
# Function to add new session to the database
def add_session(session):
new_session = sessions_table.insert().values(
title=session['title'],
subtitle=session['subtitle'],
time=session['time'],
messages=str(session['messages'])
)
result = db_session.execute(new_session)
db_session.commit()
return result.inserted_primary_key[0]
# Function to delete session from the database
def delete_session(session_id):
db_session.query(sessions_table).filter_by(id=session_id).delete()
db_session.commit()
# Load existing sessions from the database
if 'sessions' not in st.session_state:
st.session_state['sessions'] = load_sessions()
if 'current_session' not in st.session_state:
st.session_state['current_session'] = None
if 'selected_therapist' not in st.session_state:
st.session_state['selected_therapist'] = None
# Therapist prompt templates
therapist_templates = {
"Counsellor": """
You are a compassionate and empathetic counsellor specialized in mental health support.
Your primary goal is to provide emotional support, offer practical advice, and guide users to helpful resources.
You are non-judgmental, understanding, and always prioritize the user's well-being.
Respond in a calm and reassuring manner, ensuring that users feel heard and supported.
Guidelines:
1. Always be empathetic, supportive, and non-judgmental.
2. Provide practical advice and suggest resources where appropriate.
3. Use simple and clear language to ensure understanding.
4. Encourage users to seek professional help if needed, but never give medical diagnoses.
5. Avoid overly technical language and focus on being relatable and approachable.
6. Be friendly, remember context and conversation between the user and yourself and become more engaging.
7. Use open-ended questions to encourage users to express themselves.
8. Validate the user's feelings and experiences.
9. Offer coping strategies and self-care tips.
10. Maintain confidentiality and respect the user's privacy.
11. Do not use emojis
Translate and respond in {language}.
Current conversation:
{chat_history}
User: {user_message}
Counsellor:
""",
"Cognitive Behavioral Therapist": """
You are a cognitive-behavioral therapist specialized in helping individuals challenge and change unhelpful cognitive distortions and behaviors.
Your role is to guide users through structured exercises and provide evidence-based techniques to improve their mental health.
Guidelines:
1. Always be empathetic, supportive, and non-judgmental.
2. Provide practical advice and suggest CBT techniques where appropriate.
3. Use simple and clear language to ensure understanding.
4. Encourage users to practice the techniques regularly for better results.
5. Avoid overly technical language and focus on being relatable and approachable.
6. Provide clear explanations of cognitive-behavioral concepts.
7. Use examples and analogies to help users understand complex ideas.
8. Offer step-by-step guidance for CBT exercises.
9. Encourage users to set and work towards achievable goals.
10. Provide positive reinforcement and celebrate progress.
11. Do not use emojis
Translate and Respond in {language}.
Current conversation:
{chat_history}
User: {user_message}
Cognitive Behavioral Therapist:
""",
"Student Counsellor": """
You are a student counsellor specialized in helping students with academic, social, and emotional challenges.
Your role is to provide support, guidance, and practical advice to help students navigate their school or college life effectively.
Guidelines:
1. Always be empathetic, supportive, and non-judgmental.
2. Provide practical advice and suggest resources where appropriate.
3. Use simple and clear language to ensure understanding.
4. Encourage students to seek professional help if needed, but never give medical diagnoses.
5. Avoid overly technical language and focus on being relatable and approachable.
6. Help students develop time management and study skills.
7. Offer guidance on dealing with peer pressure and social issues.
8. Provide tips for managing stress and anxiety.
9. Encourage students to set academic and personal goals.
10. Validate students' feelings and experiences.
11. Do not use emojis
Translate and Respond in {language}.
Current conversation:
{chat_history}
User: {user_message}
Student Counsellor: Respond in {language}.
""",
"Psychologist": """
You are a clinical psychologist specialized in psychological assessment and therapy.
Your role is to provide evidence-based psychological support and guide users towards better mental health.
Guidelines:
1. Always be empathetic, supportive, and non-judgmental.
2. Provide practical advice and suggest evidence-based techniques where appropriate.
3. Use simple and clear language to ensure understanding.
4. Encourage users to seek professional help if needed, but never give medical diagnoses.
5. Avoid overly technical language and focus on being relatable and approachable.
6. Provide clear explanations of psychological concepts.
7. Use examples and analogies to help users understand complex ideas.
8. Offer step-by-step guidance for therapeutic exercises.
9. Encourage users to set and work towards achievable goals.
10. Provide positive reinforcement and celebrate progress.
11. Do not use emojis
Translate and Respond in {language}.
Current conversation:
{chat_history}
User: {user_message}
Psychologist: Respond in {language}.
""",
"Best Friend": """
You are a supportive and understanding friend who is always here to listen and chat about anything.
Your role is to provide a non-judgmental, friendly, and comforting presence.
You respond with warmth, understanding, and encouragement, just like a best friend would.
Guidelines:
1. Always be empathetic, supportive, and non-judgmental.
2. Actively listen and respond with comforting and understanding messages.
3. Encourage the user to express themselves and validate their feelings.
4. Use simple, friendly, and relatable language.
5. Maintain confidentiality and respect the user's privacy.
6. Offer encouragement and positive reinforcement.
7. Follow up on previously discussed topics.
8. Provide meaningful and contextually appropriate support.
9. Maintain a casual and approachable tone.
10. Share personal anecdotes and experiences to build rapport.
11. Do not use emojis
Translate and respond in {language}
Current conversation:
{chat_history}
User: {user_message}
Best Friend: Respond in {language}.
"""
}
therapists = [
{
"name": "Counsellor",
"description": "Compassionate and empathetic, specialized in emotional support.",
"image": "https://cdn-icons-png.flaticon.com/512/1154/1154448.png" # Female counsellor
},
{
"name": "Cognitive Behavioral Therapist",
"description": "Specializes in cognitive-behavioral techniques for mental health improvement.",
"image": "https://cdn-icons-png.flaticon.com/512/1154/1154476.png" # Male therapist
},
{
"name": "Student Counsellor",
"description": "Helps students with academic, social, and emotional challenges.",
"image": "https://cdn-icons-png.flaticon.com/512/1154/1154494.png" # New female student counsellor
},
{
"name": "Psychologist",
"description": "Specializes in psychological assessment and evidence-based therapy.",
"image": "https://cdn-icons-png.flaticon.com/512/1154/1154480.png" # Male psychologist
},
{
"name": "Best Friend",
"description": "Supportive and understanding friend for general concerns and casual conversations.",
"image": "https://cdn-icons-png.flaticon.com/512/1154/1154462.png" # New female best friend
}
]
def remove_emojis(text):
# Regular expression pattern for matching emojis
emoji_pattern = re.compile(
"[" # Start of the character class
u"\U0001f600-\U0001f64f" # emoticons
u"\U0001F300-\U0001F5FF" # symbols & pictographs
u"\U0001F680-\U0001F6FF" # transport & map symbols
u"\U0001F700-\U0001F77F" # alchemical symbols
u"\U0001F780-\U0001F7FF" # Geometric Shapes Extended
u"\U0001F800-\U0001F8FF" # Supplemental Arrows-C
u"\U0001F900-\U0001F9FF" # Supplemental Symbols and Pictographs
u"\U0001FA00-\U0001FA6F" # Chess Symbols
u"\U00002700-\U000027BF" # Dingbats
u"\U000024C2-\U0001F251" # Enclosed characters
"]+", flags=re.UNICODE)
return emoji_pattern.sub(r'', text) # Remove emojis
# Callback functions
def open_session(session_id):
st.session_state['current_session'] = session_id
def remove_session(session_id):
delete_session(session_id)
st.session_state['sessions'] = load_sessions()
if st.session_state['current_session'] == session_id:
st.session_state['current_session'] = None
def select_therapist(therapist_name):
st.session_state['selected_therapist'] = therapist_name
# Save conversation to a downloadable format
def save_conversation_to_file(conversation, filename):
with open(filename, 'w') as f:
for message in conversation:
if isinstance(message, HumanMessage):
f.write(f"User: {message.content}\n")#save message.content to file
elif isinstance(message, AIMessage):
f.write(f"Bot: {message.content}\n")
# Function to summarize the conversation using the chat model
def summarize_conversation(messages):
# conversation_text = "\n".join([f"User: {msg.content}" if isinstance(msg, HumanMessage) else f"Bot: {msg.content}" for msg in messages])
summary_prompt = f"Summarize the following conversation:\n\n{messages}"
# summary_response = get_chatmodel_response([SystemMessage(content=summary_prompt)])
return summary_prompt
# Generate insights for the selected session
def generate_insights(filename):
with open(f'{filename}.txt', 'r') as file:
messages = file.read()
# messages = sessions_table['messages']
print("\nMessages \n",messages)
if not messages:
return "No messages to summarize.", pd.DataFrame()
conversation_summary = messages
mood_labels = ['Positive', 'Neutral', 'Negative']
mood_counts = [0, 0, 0]
for message in messages:
if isinstance(message, HumanMessage):
content = message.content.lower()
if any(word in content for word in ['happy', 'good', 'great', 'awesome']):
mood_counts[0] += 1
elif any(word in content for word in ['okay', 'fine', 'alright', 'normal']):
mood_counts[1] += 1
elif any(word in content for word in ['sad', 'bad', 'terrible', 'awful']):
mood_counts[2] += 1
mood_data = [count if count != 0 else 0.1 for count in mood_counts] # Ensure no NaNs
mood_df = pd.DataFrame({
'Mood': mood_labels,
'Count': mood_data
})
return messages,conversation_summary, mood_df
if page == "Today":
# CSS Injection
st.markdown('''
<style>
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600;700&display=swap');
body {
font-family: 'Poppins', sans-serif;
color: #333;
}
.stApp {
background: linear-gradient(135deg, #f6d365 0%, #fda085 100%);
}
.today-page {
max-width: 1200px;
margin: 0 auto;
padding: 2rem;
}
.today-page .header {
text-align: center;
margin-bottom: 3rem;
animation: fadeInDown 1s ease-out;
}
@keyframes fadeInDown {
from { opacity: 0; transform: translateY(-30px); }
to { opacity: 1; transform: translateY(0); }
}
.today-page .header h1 {
font-size: 3.5rem;
color: #ff6b6b;
margin-bottom: 1rem;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
}
.today-page .header p {
font-size: 1.3rem;
color: #4a4a4a;
max-width: 800px;
margin: 0 auto;
line-height: 1.6;
}
.today-page .content {
display: flex;
justify-content: space-around;
flex-wrap: wrap;
}
.today-page .card {
background-color: #ffffff;
border-radius: 20px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
padding: 2rem;
margin: 1rem;
text-align: center;
transition: all 0.3s ease;
width: 300px;
overflow: hidden;
position: relative;
}
.today-page .card::before {
content: '';
position: absolute;
top: -50%;
left: -50%;
width: 200%;
height: 200%;
background: linear-gradient(45deg, transparent, rgba(255, 255, 255, 0.3), transparent);
transform: rotate(45deg);
transition: all 0.5s ease;
}
.today-page .card:hover::before {
left: 100%;
top: 100%;
}
.today-page .card:hover {
transform: translateY(-10px) scale(1.02);
box-shadow: 0 15px 40px rgba(0, 0, 0, 0.2);
}
.today-page .card img {
max-width: 100%;
border-radius: 15px;
margin-bottom: 1.5rem;
transition: all 0.3s ease;
}
.today-page .card:hover img {
transform: scale(1.1);
}
.today-page .card h3 {
font-size: 1.8rem;
color: #2c3e50;
margin-bottom: 1rem;
}
.today-page .card p {
font-size: 1.1rem;
color: #7f8c8d;
margin-bottom: 1.5rem;
}
.today-page .card-button {
background-color: #4facfe;
color: #ffffff;
border: none;
border-radius: 25px;
padding: 0.75rem 1.5rem;
font-size: 1rem;
font-weight: 600;
transition: all 0.3s ease;
text-decoration: none;
display: inline-block;
}
.today-page .card-button:hover {
background-color: #00f2fe;
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}
.emoji {
font-size: 2.5rem;
margin-bottom: 1rem;
}
/* Custom card colors */
.card-wellness { background: linear-gradient(135deg, #c3ec52 0%, #0ba29d 100%); }
.card-support { background: linear-gradient(135deg, #13f1fc 0%, #0470dc 100%); }
.card-selfcare { background: linear-gradient(135deg, #f6d365 0%, #fda085 100%); }
.card-wellness .card-button { background-color: #0ba29d; }
.card-wellness .card-button:hover { background-color: #c3ec52; color: #333; }
.card-support .card-button { background-color: #0470dc; }
.card-support .card-button:hover { background-color: #13f1fc; color: #333; }
.card-selfcare .card-button { background-color: #fda085; }
.card-selfcare .card-button:hover { background-color: #f6d365; color: #333; }
</style>
''', unsafe_allow_html=True)
# HTML Content
st.markdown('''
<div class="today-page">
<div class="header">
<h1>Welcome to MindMate 🧠💖</h1>
<p>Your digital wellness companion. Embark on a journey of self-discovery and growth as we guide you through the fascinating landscape of mental health and well-being.</p>
</div>
<div class="content">
<div class="card card-wellness">
<div class="emoji">🌟</div>
<img src="https://images.unsplash.com/photo-1544367567-0f2fcb009e0b?ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80" alt="Wellness">
<h3>Understanding Mental Health</h3>
<p>Unlock the secrets of your mind and learn how mental health shapes your daily life.</p>
<a href="#" class="card-button">Explore Wellness</a>
</div>
<div class="card card-support">
<div class="emoji">🤝</div>
<img src="https://images.unsplash.com/photo-1573497019940-1c28c88b4f3e?ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80" alt="Support">
<h3>Finding Support</h3>
<p>Discover a network of care, from professional guidance to heartwarming personal connections.</p>
<a href="#" class="card-button">Get Support</a>
</div>
<div class="card card-selfcare">
<div class="emoji">🌼</div>
<img src="https://images.unsplash.com/photo-1487528278747-ba99ed528ebc?ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80" alt="Self-care">
<h3>Self-Care Tips</h3>
<p>Nurture your mind and soul with practical strategies for everyday mental wellness.</p>
<a href="#" class="card-button">Practice Self-Care</a>
</div>
</div>
</div>
''', unsafe_allow_html=True)
elif page == "Sessions":
st.subheader("Sessions")
new_session_card = '''
<div class="new-session-card" onclick="document.querySelector('button#start_new_session').click()">
<div class="new-session-icon">➕</div>
<div class="new-session-text">Start a new session</div>
</div>
'''
st.markdown(new_session_card, unsafe_allow_html=True)
if st.button("Start a new session", key="start_new_session"):
new_session = {
"title": "Untitled Session",
"subtitle": "New session",
"time": time.strftime("%H:%M %p"),
"messages": []
}
new_session_id = add_session(new_session)
new_session['id'] = new_session_id
st.session_state['sessions'].append(new_session)
st.session_state['current_session'] = new_session_id
st.rerun()
session_columns = st.columns(3)
for index, session in enumerate(st.session_state['sessions']):
col = session_columns[index % 3]
with col:
st.markdown(f'''
<div class="session-card">
<div class="session-info">
<div class="session-title">{session["title"]}</div>
<div class="session-subtitle">{session["subtitle"]}</div>
<div class="session-time">🕒 {session["time"]}</div>
</div>
<div class="buttons">
''', unsafe_allow_html=True)
if st.button("Open", key=f"open_{session['id']}"):
open_session(session['id'])
st.rerun()
if st.button("Delete", key=f"delete_{session['id']}"):
remove_session(session['id'])
st.rerun()
st.markdown('</div></div>', unsafe_allow_html=True)
if st.session_state['current_session'] is not None:
session = next((s for s in st.session_state['sessions'] if s['id'] == st.session_state['current_session']), None)
if session:
st.subheader("Chat")
# Language selection
languages = {"English": "en", "Spanish": "es", "French": "fr", "German": "de", "Chinese": "zh","Hindi":"hi"}
selected_language = st.selectbox("Select Language", list(languages.keys()), index=0)
language = languages[selected_language]
# Select the appropriate prompt template based on the selected therapist
selected_therapist = st.session_state.get('selected_therapist', 'Counsellor')
therapist_template = therapist_templates.get(selected_therapist, therapist_templates['Counsellor'])
CUSTOM_PROMPT = PromptTemplate.from_template(therapist_template)
# Initialize conversation memory
if 'flowmessages' not in session:
session['flowmessages'] = [
SystemMessage(content=f"Hey there! I'm {selected_therapist}, your AI mental health assistant. How are you feeling?")
]
memory = ConversationBufferWindowMemory(k=5, return_messages=True)
def get_chatmodel_response(question):
session['flowmessages'].append(HumanMessage(content=question))
memory.save_context({"input": question}, {"output": ""}) # Save the input question to memory
# Prepare the chat history for the prompt
chat_history = "\n".join([f"User: {msg.content}" if isinstance(msg, HumanMessage) else f"Bot: {msg.content}" for msg in session['flowmessages']])
# Construct the prompt using the template
prompt = CUSTOM_PROMPT.format(chat_history=chat_history, user_message=question, language=language)
# Ensure the prompt is not empty
if not prompt.strip():
raise ValueError("The constructed prompt is empty. Check the message formatting.")
# Use HumanMessage instead of SystemMessage
messages = [HumanMessage(content=prompt)]
# Get the response from the chat model
answer = st.session_state['chat'](messages) # Pass list of messages to chat
# Ensure AI response is not empty
if not answer or not answer.content.strip():
raise ValueError("The AI response is empty. Check the prompt or input.")
# Append the AI response to the session
session['flowmessages'].append(AIMessage(content=answer.content))
save_session(session) # Save the session after appending new messages
return answer.content
input = st.text_input("Input: ", key="input")
submit = st.button("Ask the question")
if submit:
cleaned_input = remove_emojis(input)
response = get_chatmodel_response(cleaned_input)
save_session(session)
filename = f"{session['title']}.txt"
save_conversation_to_file(session['flowmessages'], filename)
with open(filename, 'rb') as file:
st.download_button(
label="Download Conversation",
data=file,
file_name=filename,
mime='text/plain'
)
st.rerun()
if "flowmessages" in session:
st.subheader("Chat")
for message in session['flowmessages']:
if isinstance(message, HumanMessage):
st.write(user_template.replace("{{MSG}}", message.content), unsafe_allow_html=True)
elif isinstance(message, AIMessage):
st.write(bot_template.replace("{{MSG}}", message.content), unsafe_allow_html=True)