jaelin215 commited on
Commit
bd9870c
1 Parent(s): e196233

Upload 14 files

Browse files
Chromadb_storage_JyotiNigam.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #---
2
+ #- Author: Jaelin Lee, Jyoti Nigam
3
+ #- Date: Mar 16, 2024
4
+ #- Description: storing data into vector database called Chroma DB. Based on user input, retrieve most relevant info from knowledge base.
5
+ #- How it works: Tokenize the user input text using NLTK. Then, get TF-IDF based score against knowledge base using BM25. Get the index of the most similar item within knowledgebase using `argmax()`. Then, using the index, retrieve that item from the knowledge base.
6
+ #---
7
+
8
+ import nltk
9
+ from nltk.tokenize import word_tokenize
10
+ from langchain_community.document_loaders import TextLoader
11
+ from langchain_community.embeddings.sentence_transformer import (
12
+ SentenceTransformerEmbeddings,
13
+ )
14
+ from langchain_community.vectorstores import Chroma
15
+ from langchain_text_splitters import CharacterTextSplitter
16
+
17
+ # Download NLTK data for tokenization
18
+ nltk.download('punkt')
19
+ import os
20
+ global db
21
+ class QuestionRetriever:
22
+
23
+ def load_documents(self,file_name):
24
+ current_directory = os.getcwd()
25
+ data_directory = os.path.join(current_directory, "data")
26
+ file_path = os.path.join(data_directory, file_name)
27
+ loader = TextLoader(file_path)
28
+ documents = loader.load()
29
+ return documents
30
+
31
+ def store_data_in_vector_db(self,documents):
32
+ # global db
33
+ text_splitter = CharacterTextSplitter(chunk_size=100, chunk_overlap=0,separator="\n")
34
+ docs = text_splitter.split_documents(documents)
35
+ # create the open-source embedding function
36
+ embedding_function = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
37
+ # print(docs)
38
+ # load it into Chroma
39
+ db = Chroma.from_documents(docs, embedding_function)
40
+ return db
41
+
42
+ def get_response(self, user_query, predicted_mental_category):
43
+ if predicted_mental_category == "depression":
44
+ documents=self.load_documents("depression_questions.txt")
45
+
46
+ elif predicted_mental_category == "adhd":
47
+ documents=self.load_documents("adhd_questions.txt")
48
+
49
+ elif predicted_mental_category == "anxiety":
50
+ documents=self.load_documents("anxiety_questions.txt")
51
+
52
+ else:
53
+ print("Sorry, allowed predicted_mental_category is ['depresison', 'adhd', 'anxiety'].")
54
+ return
55
+ db=self.store_data_in_vector_db(documents)
56
+
57
+ docs = db.similarity_search(user_query)
58
+ most_similar_question = docs[0].page_content.split("\n")[0] # Extract the first question
59
+ if user_query==most_similar_question:
60
+ most_similar_question=docs[1].page_content.split("\n")[0]
61
+
62
+ print(most_similar_question)
63
+ return most_similar_question
64
+
65
+ if __name__ == "__main__":
66
+ model = QuestionRetriever()
67
+ user_input = input("User: ")
68
+
69
+ predicted_mental_condition = "depression"
70
+ response = model.get_response(user_input, predicted_mental_condition)
71
+ print("Chatbot:", response)
bm25_retreive_question.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #---
2
+ #- Author: Jaelin Lee
3
+ #- Date: Mar 23, 2024
4
+ #- Description: Similarity search using BM25. Based on user input, retrieve most relevant info from knowledge base.
5
+ #- How it works: Tokenize the user input text using NLTK. Then, get TF-IDF based score against knowledge base using BM25. Get the index of the most similar item within knowledgebase using `argmax()`. Then, using the index, retrieve that item from the knowledge base.
6
+ #---
7
+
8
+ from rank_bm25 import BM25Okapi
9
+ import nltk
10
+ from nltk.tokenize import word_tokenize
11
+
12
+ # Download NLTK data for tokenization
13
+ nltk.download('punkt')
14
+
15
+ class QuestionRetriever:
16
+ def __init__(self):
17
+ self.depression_questions = self.load_questions_from_file("src/model_building/RL/data/depression_questions.txt")
18
+ self.adhd_questions = self.load_questions_from_file("src/model_building/RL/data/adhd_questions.txt")
19
+ self.anxiety_questions = self.load_questions_from_file("src/model_building/RL/data/anxiety_questions.txt")
20
+ self.social_isolation_questions = self.load_questions_from_file("src/model_building/RL/data/social_isolation.txt")
21
+ self.cyberbullying_questions = self.load_questions_from_file("src/model_building/RL/data/cyberbullying.txt")
22
+ self.social_media_addiction_questions = self.load_questions_from_file("src/model_building/RL/data/socialmediaaddiction.txt")
23
+
24
+
25
+ def load_questions_from_file(self, filename):
26
+ with open(filename, "r") as file:
27
+ questions = file.readlines()
28
+ # Remove any leading or trailing whitespace and newline characters
29
+ questions = [question.strip() for question in questions]
30
+ return questions
31
+
32
+ def get_response(self, user_query, predicted_mental_category):
33
+ if predicted_mental_category == "depression":
34
+ knowledge_base = self.depression_questions
35
+ elif predicted_mental_category == "adhd":
36
+ knowledge_base = self.adhd_questions
37
+ elif predicted_mental_category == "anxiety":
38
+ knowledge_base = self.anxiety_questions
39
+ elif predicted_mental_category == "social isolation":
40
+ knowledge_base = self.social_isolation_questions
41
+ elif predicted_mental_category == "cyberbullying":
42
+ knowledge_base = self.cyberbullying_questions
43
+ elif predicted_mental_category == "social media addiction":
44
+ knowledge_base = self.social_media_addiction_questions
45
+
46
+ else:
47
+ knowledge_base = None
48
+ print("Sorry, I didn't understand that.")
49
+
50
+ if knowledge_base:
51
+ tokenized_docs = [word_tokenize(doc.lower()) for doc in knowledge_base] # Ensure lowercase for consistency
52
+ bm25 = BM25Okapi(tokenized_docs)
53
+ tokenized_query = word_tokenize(user_query.lower()) # Ensure lowercase for consistency
54
+ doc_scores = bm25.get_scores(tokenized_query)
55
+
56
+ # Get the index of the most relevant document
57
+ most_relevant_doc_index = doc_scores.argmax()
58
+
59
+ # Fetch the corresponding response from the knowledge base
60
+ response = knowledge_base[most_relevant_doc_index]
61
+ return response
62
+ else:
63
+ return None
64
+
65
+ if __name__ == "__main__":
66
+ # knowledge_base = "depression_questions"
67
+ predicted_mental_category = "cyberbullying"
68
+ model = QuestionRetriever()
69
+ user_input = input("User: ")
70
+
71
+ response = model.get_response(user_input, predicted_mental_category)
72
+ print("Chatbot:", response)
73
+
clean_text_model.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import warnings
3
+ from nltk.corpus import stopwords
4
+ from nltk.stem import WordNetLemmatizer
5
+ from bs4 import BeautifulSoup
6
+
7
+ class TextCleaner:
8
+ def __init__(self):
9
+ warnings.filterwarnings("ignore")
10
+ self.stop_words = set(stopwords.words('english'))
11
+ self.lemmatizer = WordNetLemmatizer()
12
+
13
+ def cleaning_text(self, text):
14
+ if text and isinstance(text, str):
15
+ text = BeautifulSoup(text, "html.parser").get_text()
16
+ text = re.sub(r'https?://\S+|www\.\S+|@\w+|#\w+|[^a-zA-Z]', ' ', text.lower())
17
+ text = ' '.join([self.lemmatizer.lemmatize(word) for word in text.split() if len(word) > 1 and word not in self.stop_words])
18
+ text = ' '.join(list(dict.fromkeys(text.split())))
19
+ else:
20
+ text = ''
21
+ return text
22
+
23
+ if __name__ == "__main__":
24
+ # Example usage:
25
+ cleaner = TextCleaner()
26
+ print(cleaner.cleaning_text("I feel bullied online."))
data/adhd_questions.txt ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ How often have you been feeling excessive worry or nervousness, even about everyday things?
2
+ How often do you experience physical symptoms like racing heart, sweating, or shortness of breath when feeling anxious?
3
+ How often do you encounter certain situations or triggers that cause you significant anxiety?
4
+ How often do you find yourself avoiding places or activities due to anxiety?
5
+ How often have you had trouble sleeping or concentrating because of anxious thoughts?
6
+ How often do you feel a constant need to be in control or have things perfect to avoid anxiety?
7
+ How often do you have intrusive thoughts that are difficult to stop?
8
+ How often do you experience sudden feelings of intense fear or panic (panic attacks)?
9
+ How often has your anxiety significantly impacted your daily life or relationships?
10
+ How much does anxiety interfere with your daily life?
11
+ How often have you felt nervous, anxious, or on edge?
12
+ How often have you found it difficult to stop or control worrying thoughts in the last two weeks?
13
+ Have you experienced restlessness or felt keyed up or on edge due to anxiety in the past two weeks?
14
+ How often have you felt easily fatigued or had difficulty concentrating because of anxiety recently?
15
+ Have you noticed irritability or muscle tension as a result of feeling anxious over the past two weeks?
16
+ How often have you had trouble falling asleep, staying asleep, or restless sleep due to anxiety in the last two weeks?
17
+ Do you find yourself easily startled or feeling on edge most days because of anxiety?
18
+ How often have you experienced physical symptoms like sweating, trembling, or a racing heart due to anxiety recently?
19
+ Have you found it challenging to relax or felt restless most days because of anxiety in the past two weeks?
20
+ How often have you felt a sense of impending doom or danger because of anxiety over the past two weeks?
21
+ How often do you feel nervous or on edge?
22
+ Do you have trouble relaxing or controlling your worries?
23
+ Have you experienced a racing or irregular heartbeat?
24
+ Do you feel like you're constantly on guard or on high alert?
25
+ Have you noticed any physical symptoms like trembling or sweating?
26
+ Do you feel like you're having trouble concentrating or making decisions?
27
+ Have you experienced any feelings of detachment or disconnection from others?
28
+ Do you feel like you're having trouble sleeping or experiencing vivid dreams?
29
+ Have you noticed any changes in your appetite or eating habits?
30
+ Have you considered seeking professional help for your anxiety?
data/anxiety_questions.txt ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ How often have you been feeling excessive worry or nervousness, even about everyday things?
2
+ How often do you experience physical symptoms like racing heart, sweating, or shortness of breath when feeling anxious?
3
+ How often do you encounter certain situations or triggers that cause you significant anxiety?
4
+ How often do you find yourself avoiding places or activities due to anxiety?
5
+ How often have you had trouble sleeping or concentrating because of anxious thoughts?
6
+ How often do you feel a constant need to be in control or have things perfect to avoid anxiety?
7
+ How often do you have intrusive thoughts that are difficult to stop?
8
+ How often do you experience sudden feelings of intense fear or panic (panic attacks)?
9
+ How often has your anxiety significantly impacted your daily life or relationships?
10
+ How much does anxiety interfere with your daily life?
11
+ How often have you felt nervous, anxious, or on edge?
12
+ How often have you found it difficult to stop or control worrying thoughts in the last two weeks?
13
+ Have you experienced restlessness or felt keyed up or on edge due to anxiety in the past two weeks?
14
+ How often have you felt easily fatigued or had difficulty concentrating because of anxiety recently?
15
+ Have you noticed irritability or muscle tension as a result of feeling anxious over the past two weeks?
16
+ How often have you had trouble falling asleep, staying asleep, or restless sleep due to anxiety in the last two weeks?
17
+ Do you find yourself easily startled or feeling on edge most days because of anxiety?
18
+ How often have you experienced physical symptoms like sweating, trembling, or a racing heart due to anxiety recently?
19
+ Have you found it challenging to relax or felt restless most days because of anxiety in the past two weeks?
20
+ How often have you felt a sense of impending doom or danger because of anxiety over the past two weeks?
21
+ How often do you feel nervous or on edge?
22
+ Do you have trouble relaxing or controlling your worries?
23
+ Have you experienced a racing or irregular heartbeat?
24
+ Do you feel like you're constantly on guard or on high alert?
25
+ Have you noticed any physical symptoms like trembling or sweating?
26
+ Do you feel like you're having trouble concentrating or making decisions?
27
+ Have you experienced any feelings of detachment or disconnection from others?
28
+ Do you feel like you're having trouble sleeping or experiencing vivid dreams?
29
+ Have you noticed any changes in your appetite or eating habits?
30
+ Have you considered seeking professional help for your anxiety?
data/cyberbullying.txt ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Have you noticed instances of verbal attacks or insults directed at you online?
2
+ How often do you find yourself targeted by rumors or gossip spread through social media?
3
+ Can you recall receiving threatening or intimidating messages or comments online?
4
+ Are you aware of any instances where someone impersonated you or stole your identity online?
5
+ Have you ever felt excluded or ostracized from online groups or communities?
6
+ Have you witnessed cyberbullying directed at others in your online interactions?
7
+ When was the last time you felt anxious or afraid about using social media due to cyberbullying concerns?
8
+ Do you find yourself altering your online behavior due to fears of cyberbullying?
9
+ Have you experienced negative consequences in your personal or professional life because of cyberbullying?
10
+ How often do you compare yourself to others online, and have you experienced cyberbullying as a result?
11
+ Can you recall instances where you felt your self-worth was tied to social media feedback, leading to cyberbullying?
12
+ Have you experienced withdrawal symptoms when you're unable to access social media due to cyberbullying?
13
+ Do you use social media as a means to escape from negative emotions or stress caused by cyberbullying?
14
+ Have you found it challenging to focus on tasks or activities due to cyberbullying-related stress?
15
+ Can you recall instances where cyberbullying led to neglecting responsibilities or obligations?
16
+ How often do you experience guilt or shame about the time spent dealing with cyberbullying?
17
+ Have your relationships been negatively impacted by cyberbullying directed at you or others?
18
+ When was the last time you felt like you were missing out on social opportunities due to cyberbullying?
19
+ Have you felt isolated or alone due to cyberbullying experiences?
20
+ How frequently do you feel like your social media usage is beyond your control because of cyberbullying?
21
+ Can you recall instances where you felt compelled to constantly refresh your social media feed due to cyberbullying-related anxiety?
22
+ Have you experienced physical symptoms like headaches or eye strain due to cyberbullying stress?
23
+ Do you feel like your productivity suffers due to the mental toll of cyberbullying?
24
+ How often do you feel like your mental health is negatively affected by cyberbullying?
25
+ Have you ever felt physically unsafe due to cyberbullying threats?
26
+ Can you recall instances where you've lied about your online activities to avoid cyberbullying?
27
+ Have you experienced a decline in self-esteem due to cyberbullying?
28
+ How often do you feel like your sleep patterns are disrupted because of cyberbullying-related stress?
29
+ Do you feel like you've missed out on opportunities or experiences in real life because of cyberbullying?
30
+ How frequently do you feel like your social interactions are negatively impacted by cyberbullying?nships?
data/depression_questions.txt ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ How often have you felt persistently low in mood or sad for most of the day?
2
+ How often have you lost interest or pleasure in activities you used to enjoy?
3
+ How often have you experienced significant changes in appetite or weight (up or down)?
4
+ How often have you had trouble sleeping or sleeping too much?
5
+ How often have you felt restless or slowed down most of the time?
6
+ How often have you felt worthless or excessively guilty, even for minor things?
7
+ How often have you had difficulty thinking, concentrating, or making decisions?
8
+ How often have you had recurrent thoughts of death or suicide?
9
+ How often have you felt hopeless or like there's no point in life?
10
+ How often have you felt isolated or withdrawn from others due to your mood?
11
+ How often have you felt down, depressed, or hopeless?
12
+ How often have you had little interest or pleasure in doing things over the past two weeks?
13
+ Have you experienced changes in your appetite or weight due to feeling depressed in the last two weeks?
14
+ How often have you had trouble falling asleep, staying asleep, or sleeping too much because of feeling depressed?
15
+ Have you felt tired or had little energy most days over the past two weeks due to depression?
16
+ How often have you felt bad about yourself or that you are a failure or have let yourself or your family down in the last two weeks?
17
+ Have you had trouble concentrating on things like reading, watching TV, or engaging in conversations due to feeling depressed?
18
+ How often have you thought that you would be better off dead or hurting yourself in some way over the past two weeks?
19
+ Do you feel restless or slowed down physically most days because of feeling depressed in the last two weeks?
20
+ How often have you found it difficult to make decisions or felt indecisive due to feeling depressed recently?
21
+ How often do you feel sad or hopeless?
22
+ Do you have trouble getting out of bed or motivating yourself to do things?
23
+ Have you experienced a loss of interest in activities or hobbies?
24
+ Do you feel like you're not good enough or that you've failed at things?
25
+ Have you noticed any changes in your appetite or sleep patterns?
26
+ Do you feel like you're isolated from others or that you don't have any support?
27
+ Have you experienced any thoughts of self-harm or suicide?
28
+ Do you feel like you're stuck in a rut or that you can't see a way out of your current situation?
29
+ Have you noticed any physical symptoms like fatigue or loss of energy?
30
+ Have you considered seeking professional help for your depression?
data/social_isolation.txt ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ How often do you find yourself spending more time on social media than engaging face-to-face with friends or family?
2
+ Have you noticed feeling disconnected or isolated from others due to your excessive use of social media?
3
+ When was the last time you felt like your real-life social interactions were being replaced by social media?
4
+ Are you experiencing difficulty initiating or maintaining conversations in real life because of your reliance on social media?
5
+ Can you recall a recent instance where you felt left out of social activities because you weren't informed through social media?
6
+ Have you recently experienced feelings of loneliness or emptiness due to your social media habits?
7
+ Do you often prioritize interacting online over spending time with loved ones in person?
8
+ How frequently do you feel like you're missing out on meaningful experiences because of your focus on social media?
9
+ Are you finding it harder to form new friendships or relationships offline due to your social media usage?
10
+ Have you noticed your social circle shrinking or becoming more limited because of your reliance on social media?
11
+ How do you feel your online interactions impact your ability to connect meaningfully with others in real life?
12
+ Can you recall an instance where your social media usage led to decreased participation in real-life activities?
13
+ Have you experienced increased feelings of social anxiety or discomfort in social settings due to social media?
14
+ Are you finding it more challenging to engage in deep or meaningful conversations with others outside of social media?
15
+ Have you felt reluctant to seek support from friends or family during difficult times because of your social media habits?
16
+ Can you think of a time when your social media usage made you feel disconnected from your surroundings?
17
+ Do you feel your social media usage has impacted your feelings of belonging or acceptance within your social circles?
18
+ Have you noticed your satisfaction with your social life decreasing due to your social media usage?
19
+ When was the last time your social media usage left you feeling more isolated or lonely?
20
+ Are you finding it harder to participate in offline activities or hobbies because of your social media habits?
21
+ Have you noticed a decrease in feelings of social support or connection from your online friends or followers?
22
+ Can you recall an instance where you felt inadequate or compared yourself negatively to others on social media?
23
+ How do you feel your social media usage has impacted your ability to form close relationships with others?
24
+ When was the last time you felt a sense of loneliness or isolation when you weren't online?
25
+ Do you feel less connected to your community or social groups because of your social media usage?
26
+ Have you noticed a decrease in feelings of empathy or understanding towards others due to social media?
27
+ Can you recall a time when your social media usage left you feeling distant or disconnected from others?
28
+ Have you noticed your feelings of loneliness or isolation increasing as a result of your social media habits?
29
+ When was the last time you felt a lack of belonging or connection to your social circles?
30
+ How do you feel your social media usage has impacted your ability to empathize with others?
data/socialmediaaddiction.txt ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ How often do you find yourself unable to resist the urge to check your social media accounts?
2
+ Have you noticed an increasing need for more time spent on social media to achieve the same level of satisfaction?
3
+ Can you recall feeling irritable or anxious when you haven't been able to access social media?
4
+ What lengths have you gone to in order to hide or minimize your social media usage from others?
5
+ Have you ever experienced cravings or strong desires to use social media?
6
+ Can you describe any unsuccessful attempts to cut down or control your social media usage?
7
+ How often do you find yourself spending more time on social media than you originally intended?
8
+ Have you neglected important responsibilities or obligations in favor of spending time on social media?
9
+ Can you identify any negative consequences in your personal or professional life as a result of your social media usage?
10
+ What impact has your social media usage had on your sleep patterns or quality of sleep?
11
+ Have you experienced physical symptoms such as headaches or eye strain due to excessive social media use?
12
+ How do you feel when you're unable to access social media for an extended period of time?
13
+ Can you recall instances where you've experienced withdrawal symptoms when you haven't been able to use social media?
14
+ What emotions do you experience when you're unable to engage with social media?
15
+ Have you noticed any changes in your mood or behavior after spending time on social media?
16
+ Can you identify any signs of tolerance, where you need to spend more time on social media to achieve the same level of satisfaction?
17
+ How often do you find yourself thinking about social media, even when you're not using it?
18
+ Have you ever experienced feelings of guilt or shame about the amount of time you spend on social media?
19
+ What impact do you think your social media usage has had on your mental health and well-being?
20
+ Can you recall a time when social media usage interfered with your ability to focus on work, study, or other activities?
21
+ How do you feel about your social media usage in relation to your relationships with friends and family?
22
+ Have you ever tried to cut back on your social media usage, but found it difficult to do so?
23
+ Can you identify any situations or triggers that lead to increased social media usage for you?
24
+ How often do you feel like you're missing out on things happening on social media when you're not online?
25
+ Have you ever experienced anxiety or stress related to your social media usage?
26
+ Can you recall instances where you've experienced negative emotions, such as jealousy or envy, while using social media?
27
+ What strategies do you use to manage or cope with your social media usage?
28
+ How do you feel about the idea of taking a temporary break from social media?
29
+ Have you ever experienced difficulty in reducing your social media usage, despite wanting to do so?
30
+ Can you envision a healthier balance between social media and other aspects of your life?
31
+
32
+
33
+
34
+
llama_guard.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import load_dotenv, find_dotenv
2
+ import os
3
+ import requests
4
+
5
+ # Please ensure you have a .env file available with 'HUGGINGFACEHUB_API_TOKEN'
6
+ load_dotenv(find_dotenv())
7
+ HUGGINGFACEHUB_API_TOKEN = os.environ["HUGGINGFACEHUB_API_TOKEN"]
8
+
9
+ with open("data/processed/llama_guard-unsafe_files.txt", "r") as file:
10
+ unsafe_categories = file.read()
11
+
12
+
13
+ def moderation_prompt_for_chat(chat):
14
+ # For simplicity, we assume the chat dictionary correctly alternates "user" and "assistant" roles
15
+ # Feel free to include error checking if needed
16
+ conversation = [turn["content"] for turn in chat]
17
+ is_agent = len(conversation) % 2 == 0
18
+ role = "Agent" if len(conversation) % 2 == 0 else "User"
19
+ prompt = f"""[INST] Task: Check if there is unsafe content in '{role}' messages in conversations according our safety policy with the below categories.
20
+
21
+ <BEGIN UNSAFE CONTENT CATEGORIES>
22
+ {unsafe_categories}
23
+ <END UNSAFE CONTENT CATEGORIES>
24
+
25
+ <BEGIN CONVERSATION>
26
+
27
+ """
28
+
29
+ # Alternate User/Agent turns, inserting 2 newlines between each
30
+ for i, m in enumerate(conversation):
31
+ role = "User" if i % 2 == 0 else "Agent"
32
+ prompt += f"{role}: {m}\n\n"
33
+ prompt += f"""<END CONVERSATION>
34
+
35
+ Provide your safety assessment for {role} in the above conversation:
36
+ - First line must read 'safe' or 'unsafe'.
37
+ - If unsafe, a second line must include a comma-separated list of violated categories. [/INST]"""
38
+ return prompt
39
+
40
+
41
+ def query(payload):
42
+ API_URL = "https://okoknht2arqo574k.us-east-1.aws.endpoints.huggingface.cloud"
43
+ bearer_txt = f"Bearer {HUGGINGFACEHUB_API_TOKEN}"
44
+ headers = {
45
+ "Accept": "application/json",
46
+ "Authorization": bearer_txt,
47
+ "Content-Type": "application/json",
48
+ }
49
+ response = requests.post(API_URL, headers=headers, json=payload)
50
+
51
+ return response.json()
52
+
53
+
54
+ def moderate_chat(chat):
55
+ prompt = moderation_prompt_for_chat(chat)
56
+
57
+ output = query(
58
+ {
59
+ "inputs": prompt,
60
+ "parameters": {
61
+ "top_k": 1,
62
+ "top_p": 0.2,
63
+ "temperature": 0.1,
64
+ "max_new_tokens": 512,
65
+ },
66
+ }
67
+ )
68
+
69
+ return output
llm_response_generator.py ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #---
2
+ #- Author: Jaelin Lee
3
+ #- Date: Mar 16, 2024
4
+ #- Description: Calls HuggingFace API to generate natural response.
5
+ #- Credit: The initial code is from Abhishek Dutta.
6
+ # Most of the code is kept as he created.
7
+ # I only added a modification to convert it to class.
8
+ # And, I tweaked the prompt to feed into the `streamlit_app.py` file.
9
+ #---
10
+
11
+ import os
12
+ from dotenv import load_dotenv, find_dotenv
13
+ from langchain_community.llms import HuggingFaceHub
14
+ from langchain_community.llms import OpenAI
15
+ # from langchain.llms import HuggingFaceHub, OpenAI
16
+ from langchain.chains import LLMChain
17
+ from langchain.prompts import PromptTemplate
18
+ import warnings
19
+
20
+ warnings.filterwarnings("ignore")
21
+
22
+ class LLLResponseGenerator():
23
+
24
+ def __init__(self):
25
+ print("initialized")
26
+
27
+
28
+ def llm_inference(
29
+ self,
30
+ model_type: str,
31
+ question: str,
32
+ prompt_template: str,
33
+ context: str,
34
+ ai_tone: str,
35
+ questionnaire: str,
36
+ user_text: str,
37
+ openai_model_name: str = "",
38
+ hf_repo_id: str = "tiiuae/falcon-7b-instruct",
39
+ temperature: float = 0.1,
40
+ max_length: int = 128,
41
+ ) -> str:
42
+ """Call HuggingFace/OpenAI model for inference
43
+
44
+ Given a question, prompt_template, and other parameters, this function calls the relevant
45
+ API to fetch LLM inference results.
46
+
47
+ Args:
48
+ model_str: Denotes the LLM vendor's name. Can be either 'huggingface' or 'openai'
49
+ question: The question to be asked to the LLM.
50
+ prompt_template: The prompt template itself.
51
+ context: Instructions for the LLM.
52
+ ai_tone: Can be either empathy, encouragement or suggest medical help.
53
+ questionnaire: Can be either depression, anxiety or adhd.
54
+ user_text: Response given by the user.
55
+ hf_repo_id: The Huggingface model's repo_id
56
+ temperature: (Default: 1.0). Range: Float (0.0-100.0). The temperature of the sampling operation. 1 means regular sampling, 0 means always take the highest score, 100.0 is getting closer to uniform probability.
57
+ max_length: Integer to define the maximum length in tokens of the output summary.
58
+
59
+ Returns:
60
+ A Python string which contains the inference result.
61
+
62
+ HuggingFace repo_id examples:
63
+ - google/flan-t5-xxl
64
+ - tiiuae/falcon-7b-instruct
65
+
66
+ """
67
+ prompt = PromptTemplate(
68
+ template=prompt_template,
69
+ input_variables=[
70
+ "context",
71
+ "ai_tone",
72
+ "questionnaire",
73
+ "question",
74
+ "user_text",
75
+ ],
76
+ )
77
+
78
+ if model_type == "openai":
79
+ # https://api.python.langchain.com/en/stable/llms/langchain.llms.openai.OpenAI.html#langchain.llms.openai.OpenAI
80
+ llm = OpenAI(
81
+ model_name=openai_model_name, temperature=temperature, max_tokens=max_length
82
+ )
83
+ llm_chain = LLMChain(prompt=prompt, llm=llm)
84
+ return llm_chain.run(
85
+ context=context,
86
+ ai_tone=ai_tone,
87
+ questionnaire=questionnaire,
88
+ question=question,
89
+ user_text=user_text,
90
+ )
91
+
92
+ elif model_type == "huggingface":
93
+ # https://python.langchain.com/docs/integrations/llms/huggingface_hub
94
+ llm = HuggingFaceHub(
95
+ repo_id=hf_repo_id,
96
+ model_kwargs={"temperature": temperature, "max_length": max_length},
97
+ )
98
+
99
+ llm_chain = LLMChain(prompt=prompt, llm=llm)
100
+ response = llm_chain.run(
101
+ context=context,
102
+ ai_tone=ai_tone,
103
+ questionnaire=questionnaire,
104
+ question=question,
105
+ user_text=user_text,
106
+ )
107
+ print(response)
108
+ # Extracting only the response part from the output
109
+ response_start_index = response.find("Response;")
110
+ return response[response_start_index + len("Response;"):].strip()
111
+
112
+ else:
113
+ print(
114
+ "Please use the correct value of model_type parameter: It can have a value of either openai or huggingface"
115
+ )
116
+
117
+
118
+ if __name__ == "__main__":
119
+ # Please ensure you have a .env file available with 'HUGGINGFACEHUB_API_TOKEN' and 'OPENAI_API_KEY' values.
120
+ load_dotenv(find_dotenv())
121
+
122
+ context = "You are a mental health supporting non-medical assistant. DO NOT PROVIDE any medical advice with conviction."
123
+
124
+ ai_tone = "EMPATHY"
125
+ questionnaire = "ADHD"
126
+ question = (
127
+ "How often do you find yourself having trouble focusing on tasks or activities?"
128
+ )
129
+ user_text = "I feel distracted all the time, and I am never able to finish"
130
+
131
+ # The user may have signs of {questionnaire}.
132
+ template = """INSTRUCTIONS: {context}
133
+
134
+ Respond to the user with a tone of {ai_tone}.
135
+
136
+ Question asked to the user: {question}
137
+
138
+ Response by the user: {user_text}
139
+
140
+ Provide some advice and ask a relevant question back to the user.
141
+
142
+ Response;
143
+ """
144
+
145
+ temperature = 0.1
146
+ max_length = 128
147
+
148
+ model = LLLResponseGenerator()
149
+
150
+
151
+ llm_response = model.llm_inference(
152
+ model_type="huggingface",
153
+ question=question,
154
+ prompt_template=template,
155
+ context=context,
156
+ ai_tone=ai_tone,
157
+ questionnaire=questionnaire,
158
+ user_text=user_text,
159
+ temperature=temperature,
160
+ max_length=max_length,
161
+ )
162
+
163
+ print(llm_response)
q_learning_chatbot.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ###
2
+ # - Author: Jaelin Lee
3
+ # - Date: Mar 15, 2024
4
+ # - Description: q-learning based RL mental health support chatbot with StreamlitUI. Incorporated the updated code from Aleksandra Śledziewska that fixed token size issue. The model is now loaded from pickle if the model is already saved to pickle. This saves time for each prediction.
5
+ ###
6
+
7
+ import os
8
+ import numpy as np
9
+ import pandas as pd
10
+ from xgb_mental_health import MentalHealthClassifier
11
+ import pickle
12
+ import streamlit as st
13
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
14
+ from torch.nn.functional import softmax
15
+ import torch
16
+
17
+
18
+
19
+ class QLearningChatbot:
20
+ def __init__(self, states, actions, learning_rate=0.9, discount_factor=0.1):
21
+ self.states = states
22
+ self.actions = actions
23
+ self.learning_rate = learning_rate
24
+ self.discount_factor = discount_factor
25
+ self.q_values = np.random.rand(len(states), len(actions))
26
+ self.mood = "Neutral"
27
+ self.mood_history = []
28
+ self.mood_history_int = []
29
+ self.tokenizer = AutoTokenizer.from_pretrained("nlptown/bert-base-multilingual-uncased-sentiment")
30
+ self.bert_sentiment_model_path = "bert_sentiment.pkl"
31
+ self.bert_sentiment_model = self.load_model() if os.path.exists(self.bert_sentiment_model_path) else AutoModelForSequenceClassification.from_pretrained("nlptown/bert-base-multilingual-uncased-sentiment")
32
+
33
+
34
+ def detect_sentiment(self, input_text):
35
+ # Encode the text
36
+ encoded_input = self.tokenizer(input_text, return_tensors='pt', truncation=True, max_length=512)
37
+
38
+ # Perform inference
39
+ with torch.no_grad():
40
+ output = self.bert_sentiment_model(**encoded_input)
41
+
42
+ # Process the output (softmax to get probabilities)
43
+ scores = softmax(output.logits, dim=1)
44
+
45
+ # Map scores to sentiment labels
46
+ labels = ['Negative', 'Moderately Negative', 'Neutral', 'Moderately Positive', 'Positive']
47
+ scores = scores.numpy().flatten()
48
+ scores_dict = {label: score for label, score in zip(labels, scores)}
49
+ highest_sentiment = max(scores_dict, key=scores_dict.get)
50
+ self.mood = highest_sentiment
51
+ return highest_sentiment
52
+
53
+ def get_action(self, current_state):
54
+ current_state_index = self.states.index(current_state)
55
+ # print(np.argmax(self.q_values[current_state_index, :]))
56
+ return self.actions[np.argmax(self.q_values[current_state_index, :])]
57
+
58
+ def update_q_values(self, current_state, action, reward, next_state):
59
+ # print(f"state-reward: {current_state} - {reward} -- (b)")
60
+ current_state_index = self.states.index(current_state)
61
+ action_index = self.actions.index(action)
62
+ next_state_index = self.states.index(next_state)
63
+
64
+ current_q_value = self.q_values[current_state_index, action_index]
65
+ max_next_q_value = np.max(self.q_values[next_state_index, :])
66
+
67
+ new_q_value = current_q_value + self.learning_rate * (reward + self.discount_factor * max_next_q_value - current_q_value)
68
+ self.q_values[current_state_index, action_index] = new_q_value
69
+
70
+ def update_mood_history(self):
71
+ st.session_state.entered_mood.append(self.mood)
72
+ self.mood_history = st.session_state.entered_mood
73
+ return self.mood_history
74
+
75
+ def check_mood_trend(self):
76
+ mood_dict = {'Negative': 1, 'Moderately Negative': 2, 'Neutral': 3, 'Moderately Positive': 4, 'Positive': 5}
77
+
78
+ if len(self.mood_history) >= 2:
79
+ self.mood_history_int = [mood_dict.get(x) for x in self.mood_history]
80
+ recent_moods = self.mood_history_int[-2:]
81
+ if recent_moods[-1] > recent_moods[-2]:
82
+ return 'increased'
83
+ elif recent_moods[-1] < recent_moods[-2]:
84
+ return 'decreased'
85
+ else:
86
+ return 'unchanged'
87
+ else:
88
+ return 'unchanged'
89
+
streamlit_app.py ADDED
@@ -0,0 +1,284 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ###
2
+ # - Author: Jaelin Lee, Abhishek Dutta
3
+ # - Date: Mar 23, 2024
4
+ # - Description: Streamlit UI for mental health support chatbot using sentiment analsys, RL, BM25/ChromaDB, and LLM.
5
+ # - Note:
6
+ # - Updated to UI to show predicted mental health condition in behind the scence regardless of the ositive/negative sentiment
7
+ ###
8
+
9
+ from dotenv import load_dotenv, find_dotenv
10
+ import pandas as pd
11
+ import streamlit as st
12
+ from q_learning_chatbot import QLearningChatbot
13
+ from xgb_mental_health import MentalHealthClassifier
14
+ from bm25_retreive_question import QuestionRetriever as QuestionRetriever_bm25
15
+ from Chromadb_storage_JyotiNigam import QuestionRetriever as QuestionRetriever_chromaDB
16
+ from llm_response_generator import LLLResponseGenerator
17
+ import os
18
+ from llama_guard import moderate_chat
19
+
20
+ # Streamlit UI
21
+ st.title("MindfulMedia Mentor")
22
+
23
+ # Define states and actions
24
+ states = [
25
+ "Negative",
26
+ "Moderately Negative",
27
+ "Neutral",
28
+ "Moderately Positive",
29
+ "Positive",
30
+ ]
31
+ actions = ["encouragement", "empathy", "spiritual"]
32
+
33
+ # Initialize Q-learning chatbot and mental health classifier
34
+ chatbot = QLearningChatbot(states, actions)
35
+
36
+ # Initialize MentalHealthClassifier
37
+ # data_path = "/Users/jaelinlee/Documents/projects/fomo/input/data.csv"
38
+ data_path = os.path.join("data", "processed", "data.csv")
39
+ print(data_path)
40
+
41
+ tokenizer_model_name = "nlptown/bert-base-multilingual-uncased-sentiment"
42
+ mental_classifier_model_path = "app/mental_health_model.pkl"
43
+ mental_classifier = MentalHealthClassifier(data_path, mental_classifier_model_path)
44
+
45
+
46
+ # Function to display Q-table
47
+ def display_q_table(q_values, states, actions):
48
+ q_table_dict = {"State": states}
49
+ for i, action in enumerate(actions):
50
+ q_table_dict[action] = q_values[:, i]
51
+
52
+ q_table_df = pd.DataFrame(q_table_dict)
53
+ return q_table_df
54
+
55
+
56
+ # Initialize memory
57
+ if "entered_text" not in st.session_state:
58
+ st.session_state.entered_text = []
59
+ if "entered_mood" not in st.session_state:
60
+ st.session_state.entered_mood = []
61
+ if "messages" not in st.session_state:
62
+ st.session_state.messages = []
63
+ if "user_sentiment" not in st.session_state:
64
+ st.session_state.user_sentiment = "Neutral"
65
+ if "mood_trend" not in st.session_state:
66
+ st.session_state.mood_trend = "Unchanged"
67
+ if "predicted_mental_category" not in st.session_state:
68
+ st.session_state.predicted_mental_category = ""
69
+ if "ai_tone" not in st.session_state:
70
+ st.session_state.ai_tone = "Empathy"
71
+ if "mood_trend_symbol" not in st.session_state:
72
+ st.session_state.mood_trend_symbol = ""
73
+ if "show_question" not in st.session_state:
74
+ st.session_state.show_question = False
75
+ if "asked_questions" not in st.session_state:
76
+ st.session_state.asked_questions = []
77
+ # Check if 'llama_guard_enabled' is already in session state, otherwise initialize it
78
+ if "llama_guard_enabled" not in st.session_state:
79
+ st.session_state["llama_guard_enabled"] = True # Default value to True
80
+
81
+ # Select Question Retriever
82
+ selected_retriever_option = st.sidebar.selectbox(
83
+ "Choose Question Retriever", ("BM25", "ChromaDB")
84
+ )
85
+ if selected_retriever_option == "BM25":
86
+ retriever = QuestionRetriever_bm25()
87
+ if selected_retriever_option == "ChromaDB":
88
+ retriever = QuestionRetriever_chromaDB()
89
+
90
+ for message in st.session_state.messages:
91
+ with st.chat_message(message.get("role")):
92
+ st.write(message.get("content"))
93
+
94
+ section_visible = True
95
+
96
+
97
+ # Collect user input
98
+ user_message = st.chat_input("Type your message here:")
99
+
100
+
101
+ # Modify the checkbox call to include a unique key parameter
102
+ llama_guard_enabled = st.sidebar.checkbox(
103
+ "Enable LlamaGuard",
104
+ value=st.session_state["llama_guard_enabled"],
105
+ key="llama_guard_toggle",
106
+ )
107
+
108
+
109
+ # Update the session state based on the checkbox interaction
110
+ st.session_state["llama_guard_enabled"] = llama_guard_enabled
111
+
112
+ # Take user input
113
+ if user_message:
114
+ st.session_state.entered_text.append(user_message)
115
+
116
+ st.session_state.messages.append({"role": "user", "content": user_message})
117
+ with st.chat_message("user"):
118
+ st.write(user_message)
119
+
120
+ is_safe = True
121
+ if st.session_state["llama_guard_enabled"]:
122
+ chat = [
123
+ {"role": "user", "content": user_message},
124
+ {"role": "assistant", "content": ""},
125
+ ]
126
+ guard_status = moderate_chat(chat)
127
+ if "unsafe" in guard_status[0]["generated_text"]:
128
+ is_safe = False
129
+ print("Guard status", guard_status)
130
+
131
+ if is_safe == False:
132
+ response = "Due to eithical and safety reasons, I can't provide the help you need. Please reach out to someone who can, like a family member, friend, or therapist. In urgent situations, contact emergency services or a crisis hotline. Remember, asking for help is brave, and you're not alone."
133
+ st.session_state.messages.append({"role": "ai", "content": response})
134
+ with st.chat_message("ai"):
135
+ st.markdown(response)
136
+ else:
137
+ # Detect mental condition
138
+ with st.spinner("Processing..."):
139
+ mental_classifier.initialize_tokenizer(tokenizer_model_name)
140
+ mental_classifier.preprocess_data()
141
+ predicted_mental_category = mental_classifier.predict_category(user_message)
142
+ print("Predicted mental health condition:", predicted_mental_category)
143
+
144
+ # Detect sentiment
145
+ user_sentiment = chatbot.detect_sentiment(user_message)
146
+
147
+ # Retrieve question
148
+ if user_sentiment in ["Negative", "Moderately Negative", "Neutral"]:
149
+ question = retriever.get_response(
150
+ user_message, predicted_mental_category
151
+ )
152
+ show_question = True
153
+ else:
154
+ show_question = False
155
+ question = ""
156
+ # predicted_mental_category = ""
157
+
158
+ # Update mood history / mood_trend
159
+ chatbot.update_mood_history()
160
+ mood_trend = chatbot.check_mood_trend()
161
+
162
+ # Define rewards
163
+ if user_sentiment in ["Positive", "Moderately Positive"]:
164
+ if mood_trend == "increased":
165
+ reward = +1
166
+ mood_trend_symbol = " ⬆️"
167
+ elif mood_trend == "unchanged":
168
+ reward = +0.8
169
+ mood_trend_symbol = ""
170
+ else: # decreased
171
+ reward = -0.2
172
+ mood_trend_symbol = " ⬇️"
173
+ else:
174
+ if mood_trend == "increased":
175
+ reward = +1
176
+ mood_trend_symbol = " ⬆️"
177
+ elif mood_trend == "unchanged":
178
+ reward = -0.2
179
+ mood_trend_symbol = ""
180
+ else: # decreased
181
+ reward = -1
182
+ mood_trend_symbol = " ⬇️"
183
+
184
+ print(
185
+ f"mood_trend - sentiment - reward: {mood_trend} - {user_sentiment} - 🛑{reward}🛑"
186
+ )
187
+
188
+ # Update Q-values
189
+ chatbot.update_q_values(
190
+ user_sentiment, chatbot.actions[0], reward, user_sentiment
191
+ )
192
+
193
+ # Get recommended action based on the updated Q-values
194
+ ai_tone = chatbot.get_action(user_sentiment)
195
+ print(ai_tone)
196
+
197
+ print(st.session_state.messages)
198
+
199
+ # LLM Response Generator
200
+ load_dotenv(find_dotenv())
201
+
202
+ llm_model = LLLResponseGenerator()
203
+ temperature = 0.1
204
+ max_length = 128
205
+
206
+ # Collect all messages exchanged so far into a single text string
207
+ all_messages = "\n".join(
208
+ [message.get("content") for message in st.session_state.messages]
209
+ )
210
+
211
+ # Question asked to the user: {question}
212
+
213
+ template = """INSTRUCTIONS: {context}
214
+
215
+ Respond to the user with a tone of {ai_tone}.
216
+
217
+ Response by the user: {user_text}
218
+ Response;
219
+ """
220
+ context = f"You are a mental health supporting non-medical assistant. Provide some advice and ask a relevant question back to the user. {all_messages}"
221
+
222
+ llm_response = llm_model.llm_inference(
223
+ model_type="huggingface",
224
+ question=question,
225
+ prompt_template=template,
226
+ context=context,
227
+ ai_tone=ai_tone,
228
+ questionnaire=predicted_mental_category,
229
+ user_text=user_message,
230
+ temperature=temperature,
231
+ max_length=max_length,
232
+ )
233
+
234
+ if show_question:
235
+ llm_reponse_with_quesiton = f"{llm_response}\n\n{question}"
236
+ else:
237
+ llm_reponse_with_quesiton = llm_response
238
+
239
+ # Append the user and AI responses to the chat history
240
+ st.session_state.messages.append(
241
+ {"role": "ai", "content": llm_reponse_with_quesiton}
242
+ )
243
+
244
+ with st.chat_message("ai"):
245
+ st.markdown(llm_reponse_with_quesiton)
246
+ # st.write(f"{llm_response}")
247
+ # if show_question:
248
+ # st.write(f"{question}")
249
+ # else:
250
+ # user doesn't feel negative.
251
+ # get question to ecourage even more positive behaviour
252
+
253
+ # Update data to memory
254
+ st.session_state.user_sentiment = user_sentiment
255
+ st.session_state.mood_trend = mood_trend
256
+ st.session_state.predicted_mental_category = predicted_mental_category
257
+ st.session_state.ai_tone = ai_tone
258
+ st.session_state.mood_trend_symbol = mood_trend_symbol
259
+ st.session_state.show_question = show_question
260
+
261
+ # Show/hide "Behind the Scene" section
262
+ # section_visible = st.sidebar.button('Show/Hide Behind the Scene')
263
+
264
+ with st.sidebar.expander("Behind the Scene", expanded=section_visible):
265
+ st.subheader("What AI is doing:")
266
+ # Use the values stored in session state
267
+ st.write(
268
+ f"- Detected User Tone: {st.session_state.user_sentiment} ({st.session_state.mood_trend.capitalize()}{st.session_state.mood_trend_symbol})"
269
+ )
270
+ # if st.session_state.show_question:
271
+ st.write(
272
+ f"- Possible Mental Condition: {st.session_state.predicted_mental_category.capitalize()}"
273
+ )
274
+ st.write(f"- AI Tone: {st.session_state.ai_tone.capitalize()}")
275
+ st.write(f"- Question retrieved from: {selected_retriever_option}")
276
+ st.write(
277
+ f"- If the user feels negative, moderately negative, or neutral, at the end of the AI response, it adds a mental health condition related question. The question is retrieved from DB. The categories of questions are limited to Depression, Anxiety, ADHD, Social Media Addiction, Social Isolation, and Cyberbullying which are most associated with FOMO related to excessive social media usage."
278
+ )
279
+ st.write(
280
+ f"- Below q-table is continuously updated after each interaction with the user. If the user's mood increases, AI gets a reward. Else, AI gets a punishment."
281
+ )
282
+
283
+ # Display Q-table
284
+ st.dataframe(display_q_table(chatbot.q_values, states, actions))
xgb_mental_health.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ###
2
+ # - Author: Jaelin Lee
3
+ # - Date: Mar 23, 2024
4
+ # - Description: XGBoost mental health classfier [depression, adhd, anxiety, social_isolation, cyberbullying, social_media_addiction]. Incorporated the updated code from Aleksandra Śledziewska that fixed token size issue. The model is now loaded from pickle if the model is already saved to pickle. This saves time for each prediction without having to retrain the model.
5
+ ###
6
+
7
+ import os.path
8
+ import pickle
9
+ import pandas as pd
10
+ from transformers import AutoTokenizer
11
+ from sklearn.model_selection import train_test_split
12
+ from sklearn.feature_extraction.text import CountVectorizer
13
+ from xgboost import XGBClassifier
14
+ from sklearn.preprocessing import LabelEncoder
15
+ from clean_text_model import TextCleaner
16
+
17
+ class MentalHealthClassifier:
18
+ def __init__(self, data_path, model_path):
19
+ self.data = pd.read_csv(data_path, skip_blank_lines=True)
20
+ self.data['category'] = ['anxiety' if x == 'axienty' else x for x in self.data['category']]
21
+ # self.data.dropna(subset=['text'], inplace=True)
22
+ self.data.dropna(subset=['clean_text'], inplace=True)
23
+ self.data_selected = self.data[['clean_text', 'category']]
24
+ self.df = pd.DataFrame(self.data_selected)
25
+ self.label_encoder = LabelEncoder()
26
+ self.df['category_encoded'] = self.label_encoder.fit_transform(self.df['category'])
27
+ self.tokenizer = None
28
+ self.vectorizer = CountVectorizer()
29
+ self.text_cleaner = TextCleaner()
30
+ self.model_path = model_path
31
+ self.model = self.load_model() if os.path.exists(model_path) else XGBClassifier()
32
+
33
+ def preprocess_data(self):
34
+ tokenized_texts = [self.tokenizer.tokenize(text, padding=True, truncation=True) for text in self.df['clean_text']]
35
+ X = self.vectorizer.fit_transform([' '.join(tokens) for tokens in tokenized_texts]).toarray()
36
+ return X, self.df['category_encoded']
37
+
38
+ def train_model(self, X, y):
39
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
40
+ self.model.fit(X_train, y_train)
41
+ y_pred = self.model.predict(X_test)
42
+ return y_test, y_pred
43
+
44
+ def predict_category(self, raw_input_text):
45
+ if self.tokenizer is None:
46
+ raise ValueError("Tokenizer not initialized. Call 'initialize_tokenizer' first.")
47
+ input_text = self.text_cleaner.cleaning_text(raw_input_text)
48
+ tokenized_input = self.tokenizer.tokenize(raw_input_text, padding=True, truncation=True)
49
+ input_feature_vector = self.vectorizer.transform([' '.join(tokenized_input)]).toarray()
50
+ predicted_category_encoded = self.model.predict(input_feature_vector)
51
+ predicted_category = self.label_encoder.inverse_transform(predicted_category_encoded)
52
+ return predicted_category[0]
53
+
54
+ def initialize_tokenizer(self, model_name):
55
+ self.model_name = model_name
56
+ self.tokenizer = AutoTokenizer.from_pretrained(self.model_name)
57
+
58
+ def save_model(self):
59
+ print("saving model...to pickle...")
60
+ with open(self.model_path, 'wb') as f:
61
+ pickle.dump(self.model, f)
62
+
63
+ def load_model(self):
64
+ print("loading model...from pickle...")
65
+ with open(self.model_path, 'rb') as f:
66
+ return pickle.load(f)
67
+
68
+ if __name__ == "__main__":
69
+ tokenizer_model_name = "nlptown/bert-base-multilingual-uncased-sentiment"
70
+ data_path = 'data/processed/data.csv'
71
+ model_path = 'app/mental_health_model.pkl'
72
+ mental_classifier = MentalHealthClassifier(data_path, model_path)
73
+
74
+ if not os.path.exists(model_path):
75
+ mental_classifier.initialize_tokenizer(tokenizer_model_name)
76
+ X, y = mental_classifier.preprocess_data()
77
+ y_test, y_pred = mental_classifier.train_model(X, y)
78
+ mental_classifier.save_model()
79
+ else:
80
+ mental_classifier.load_model()
81
+ mental_classifier.initialize_tokenizer(tokenizer_model_name) # Ensure tokenizer is initialized if loading model from pickle
82
+ mental_classifier.preprocess_data()
83
+
84
+ input_text = "I feel bullied online."
85
+ predicted_category = mental_classifier.predict_category(input_text)
86
+ print("Predicted mental health condition:", predicted_category)