ASledziewska commited on
Commit
2cbbd1d
β€’
1 Parent(s): 5d8c448

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -31
app.py CHANGED
@@ -45,9 +45,22 @@ if "entered_text" not in st.session_state:
45
  st.session_state.entered_text = []
46
  if "entered_mood" not in st.session_state:
47
  st.session_state.entered_mood = []
48
-
49
  if "messages" not in st.session_state:
50
  st.session_state.messages = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  # Select Question Retriever
53
  selected_retriever_option = st.sidebar.selectbox(
@@ -62,6 +75,9 @@ for message in st.session_state.messages:
62
  with st.chat_message(message.get("role")):
63
  st.write(message.get("content"))
64
 
 
 
 
65
  # Collect user input
66
  user_message = st.chat_input("Type your message here:")
67
 
@@ -72,8 +88,8 @@ if user_message:
72
  st.session_state.messages.append({"role": "user", "content": user_message})
73
  with st.chat_message("user"):
74
  st.write(user_message)
75
- # Detect mental condition
76
 
 
77
  with st.spinner("Processing..."):
78
  mental_classifier.initialize_tokenizer(tokenizer_model_name)
79
  mental_classifier.preprocess_data()
@@ -84,7 +100,7 @@ if user_message:
84
  user_sentiment = chatbot.detect_sentiment(user_message)
85
 
86
  # Retrieve question
87
- if user_sentiment in ["Negative", "Moderately Negative"]:
88
  question = retriever.get_response(user_message, predicted_mental_category)
89
  show_question = True
90
  else:
@@ -92,7 +108,7 @@ if user_message:
92
  question = ""
93
  predicted_mental_category = ""
94
 
95
- # Update mood history / moode_trend
96
  chatbot.update_mood_history()
97
  mood_trend = chatbot.check_mood_trend()
98
 
@@ -104,10 +120,9 @@ if user_message:
104
  elif mood_trend == "unchanged":
105
  reward = +0.8
106
  mood_trend_symbol = ""
107
- else: # decresed
108
  reward = -0.2
109
  mood_trend_symbol = " ⬇️"
110
-
111
  else:
112
  if mood_trend == "increased":
113
  reward = +1
@@ -120,7 +135,7 @@ if user_message:
120
  mood_trend_symbol = " ⬇️"
121
 
122
  print(
123
- f"mood_trend - sentiment - reward: {mood_trend} - {user_sentiment} - πŸ›‘{reward}πŸ›‘ -- (a)"
124
  )
125
 
126
  # Update Q-values
@@ -131,7 +146,7 @@ if user_message:
131
  # Get recommended action based on the updated Q-values
132
  ai_tone = chatbot.get_action(user_sentiment)
133
  print(ai_tone)
134
- # --------------
135
  # LLM Response Generator
136
  HUGGINGFACEHUB_API_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN')
137
 
@@ -139,11 +154,13 @@ if user_message:
139
  temperature = 0.1
140
  max_length = 128
141
 
 
 
142
  template = """INSTRUCTIONS: {context}
143
 
144
  Respond to the user with a tone of {ai_tone}.
145
 
146
- Question asked to the user: {question}
147
 
148
  Response by the user: {user_text}
149
  Response;
@@ -161,44 +178,53 @@ if user_message:
161
  temperature=temperature,
162
  max_length=max_length,
163
  )
164
- st.session_state.messages.append({"role": "ai", "content": llm_response})
 
 
 
 
 
 
165
 
166
  with st.chat_message("ai"):
167
- st.markdown(llm_response)
168
  # st.write(f"{llm_response}")
169
- if show_question:
170
- st.write(f"{question}")
171
  # else:
172
  # user doesn't feel negative.
173
  # get question to ecourage even more positive behaviour
174
 
175
- st.subheader("Behind the Scence - What AI is doing:")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
  st.write(
177
- f"- Detected User Tone: {user_sentiment} ({mood_trend.capitalize()}{mood_trend_symbol})"
178
  )
179
- if show_question:
180
  st.write(
181
- f"- Possible Mental Condition: {predicted_mental_category.capitalize()}"
182
  )
183
- st.write(f"- AI Tone: {ai_tone.capitalize()}")
184
  st.write(f"- Question retrieved from: {selected_retriever_option}")
185
  st.write(
186
- f"- If the user feels neagative or moderately negative, at the end of the AI response, it adds a mental health condition realted question. The question is retrieved from DB. The categories of questions are limited to Depression, Anxiety, and ADHD which are most associated with FOMO related to excessive social media usage."
187
  )
188
  st.write(
189
- f"- Below q-table is continously updated after each interaction with the user. If the user's mood increases, AI gets reward. Else, AI gets punishment."
190
  )
191
 
192
- # Display results
193
- # st.subheader(f"{user_sentiment.capitalize()}")
194
- # st.write("->" + f"{ai_tone.capitalize()}")
195
- # st.write(f"Mood {chatbot.check_mood_trend()}")
196
- # st.write(f"{ai_tone.capitalize()}, {chatbot.check_mood_trend()}")
197
-
198
  # Display Q-table
199
  st.dataframe(display_q_table(chatbot.q_values, states, actions))
200
-
201
- # Display mood history
202
- # st.subheader("Mood History (Recent 5):")
203
- # for mood_now in reversed(chatbot.mood_history[-5:]): #st.session_state.entered_mood[-5:], chatbot.mood_history[-5:]): #st.session_state.entered_text[-5:]
204
- # st.write(f"{mood_now}")
 
45
  st.session_state.entered_text = []
46
  if "entered_mood" not in st.session_state:
47
  st.session_state.entered_mood = []
 
48
  if "messages" not in st.session_state:
49
  st.session_state.messages = []
50
+ if "user_sentiment" not in st.session_state:
51
+ st.session_state.user_sentiment = "Neutral"
52
+ if "mood_trend" not in st.session_state:
53
+ st.session_state.mood_trend = "Unchanged"
54
+ if "predicted_mental_category" not in st.session_state:
55
+ st.session_state.predicted_mental_category = ""
56
+ if "ai_tone" not in st.session_state:
57
+ st.session_state.ai_tone = "Empathy"
58
+ if "mood_trend_symbol" not in st.session_state:
59
+ st.session_state.mood_trend_symbol = ""
60
+ if "show_question" not in st.session_state:
61
+ st.session_state.show_question = False
62
+ if "asked_questions" not in st.session_state:
63
+ st.session_state.asked_questions = []
64
 
65
  # Select Question Retriever
66
  selected_retriever_option = st.sidebar.selectbox(
 
75
  with st.chat_message(message.get("role")):
76
  st.write(message.get("content"))
77
 
78
+ section_visible = False
79
+
80
+
81
  # Collect user input
82
  user_message = st.chat_input("Type your message here:")
83
 
 
88
  st.session_state.messages.append({"role": "user", "content": user_message})
89
  with st.chat_message("user"):
90
  st.write(user_message)
 
91
 
92
+ # Detect mental condition
93
  with st.spinner("Processing..."):
94
  mental_classifier.initialize_tokenizer(tokenizer_model_name)
95
  mental_classifier.preprocess_data()
 
100
  user_sentiment = chatbot.detect_sentiment(user_message)
101
 
102
  # Retrieve question
103
+ if user_sentiment in ["Negative", "Moderately Negative", "Neutral"]:
104
  question = retriever.get_response(user_message, predicted_mental_category)
105
  show_question = True
106
  else:
 
108
  question = ""
109
  predicted_mental_category = ""
110
 
111
+ # Update mood history / mood_trend
112
  chatbot.update_mood_history()
113
  mood_trend = chatbot.check_mood_trend()
114
 
 
120
  elif mood_trend == "unchanged":
121
  reward = +0.8
122
  mood_trend_symbol = ""
123
+ else: # decreased
124
  reward = -0.2
125
  mood_trend_symbol = " ⬇️"
 
126
  else:
127
  if mood_trend == "increased":
128
  reward = +1
 
135
  mood_trend_symbol = " ⬇️"
136
 
137
  print(
138
+ f"mood_trend - sentiment - reward: {mood_trend} - {user_sentiment} - πŸ›‘{reward}πŸ›‘"
139
  )
140
 
141
  # Update Q-values
 
146
  # Get recommended action based on the updated Q-values
147
  ai_tone = chatbot.get_action(user_sentiment)
148
  print(ai_tone)
149
+
150
  # LLM Response Generator
151
  HUGGINGFACEHUB_API_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN')
152
 
 
154
  temperature = 0.1
155
  max_length = 128
156
 
157
+ #Question asked to the user: {question}
158
+
159
  template = """INSTRUCTIONS: {context}
160
 
161
  Respond to the user with a tone of {ai_tone}.
162
 
163
+ Question asked to the user: "None"
164
 
165
  Response by the user: {user_text}
166
  Response;
 
178
  temperature=temperature,
179
  max_length=max_length,
180
  )
181
+
182
+ if show_question:
183
+ llm_reponse_with_quesiton = f"{llm_response}\n\n{question}"
184
+ else:
185
+ llm_reponse_with_quesiton = llm_response
186
+
187
+ st.session_state.messages.append({"role": "ai", "content": llm_reponse_with_quesiton})
188
 
189
  with st.chat_message("ai"):
190
+ st.markdown(llm_reponse_with_quesiton)
191
  # st.write(f"{llm_response}")
192
+ # if show_question:
193
+ # st.write(f"{question}")
194
  # else:
195
  # user doesn't feel negative.
196
  # get question to ecourage even more positive behaviour
197
 
198
+
199
+ # Update data to memory
200
+ st.session_state.user_sentiment = user_sentiment
201
+ st.session_state.mood_trend = mood_trend
202
+ st.session_state.predicted_mental_category = predicted_mental_category
203
+ st.session_state.ai_tone = ai_tone
204
+ st.session_state.mood_trend_symbol = mood_trend_symbol
205
+ st.session_state.show_question = show_question
206
+
207
+ # Show/hide "Behind the Scene" section
208
+ # section_visible = st.sidebar.button('Show/Hide Behind the Scene')
209
+
210
+ with st.sidebar.expander('Behind the Scene', expanded=section_visible):
211
+ st.subheader("What AI is doing:")
212
+ # Use the values stored in session state
213
  st.write(
214
+ f"- Detected User Tone: {st.session_state.user_sentiment} ({st.session_state.mood_trend.capitalize()}{st.session_state.mood_trend_symbol})"
215
  )
216
+ if st.session_state.show_question:
217
  st.write(
218
+ f"- Possible Mental Condition: {st.session_state.predicted_mental_category.capitalize()}"
219
  )
220
+ st.write(f"- AI Tone: {st.session_state.ai_tone.capitalize()}")
221
  st.write(f"- Question retrieved from: {selected_retriever_option}")
222
  st.write(
223
+ f"- If the user feels negative or moderately negative, 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, and ADHD which are most associated with FOMO related to excessive social media usage."
224
  )
225
  st.write(
226
+ 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."
227
  )
228
 
 
 
 
 
 
 
229
  # Display Q-table
230
  st.dataframe(display_q_table(chatbot.q_values, states, actions))