samiee2213 commited on
Commit
87bde93
·
verified ·
1 Parent(s): fda12fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -54
app.py CHANGED
@@ -675,33 +675,39 @@ def summarize_conversation(messages):
675
  summary_response = chat([SystemMessage(content=summary_prompt)])
676
  return summary_response.content.strip()
677
 
678
- # Generate insights for the selected session
679
- def generate_insights(session):
680
- messages = session['messages']
681
- if not messages:
682
- return "No messages to summarize.", pd.DataFrame()
683
-
684
- conversation_summary = summarize_conversation(messages)
685
-
686
- mood_labels = ['Positive', 'Neutral', 'Negative']
687
- mood_counts = [0, 0, 0]
688
- for message in messages:
689
- if isinstance(message, HumanMessage):
690
- content = message.content.lower()
691
- if any(word in content for word in ['happy', 'good', 'great', 'awesome']):
692
- mood_counts[0] += 1
693
- elif any(word in content for word in ['okay', 'fine', 'alright', 'normal']):
694
- mood_counts[1] += 1
695
- elif any(word in content for word in ['sad', 'bad', 'terrible', 'awful']):
696
- mood_counts[2] += 1
697
-
698
- mood_data = [count if count != 0 else 0.1 for count in mood_counts] # Ensure no NaNs
699
- mood_df = pd.DataFrame({
700
- 'Mood': mood_labels,
701
- 'Count': mood_data
702
- })
 
 
 
 
 
 
 
703
 
704
- return conversation_summary, mood_df
705
 
706
  if page == "Today":
707
  # CSS Injection
@@ -1135,40 +1141,50 @@ elif page == "Therapists":
1135
 
1136
  elif page == "Insights":
1137
  st.subheader("Insights")
 
 
1138
  session_titles = [session["title"] for session in st.session_state['sessions']]
1139
- selected_session_title = st.selectbox("Select a session",, session_titles)
1140
 
1141
  if selected_session_title:
 
1142
  selected_session = next(session for session in st.session_state['sessions'] if session["title"] == selected_session_title)
1143
- if selected_session and 'messages' in selected _session:
1144
- message, conversation_summary, mood_dt = generate_insights (selected_session_title)
1145
- st.write (f"Messages: {message}")
1146
- st.write(f" nMood: {mood_df}")
1147
- st.markdown ("### Conversation Summary")
 
 
1148
  st.write(conversation_summary)
1149
- if not mood_df.empty:
1150
- st.markdown ("### Mood Analysis")
1151
- fig, ax = plt.subplots)
1152
- ax.pie(mood_df['Count'], labels=mood
1153
- _df['Mood'], autopct='%1.1f%%', startangle=90,
1154
- colors=['#4CAF50', '#FFC107', '#F44336'])
1155
- ax.axis ('equal')
1156
- st.pyplot(fig)
1157
- st.markdown ("### Mood Over Time")
1158
- mood_timeline = pd.Series([msg.content.lower) for msg in selected_session ['messages'] if
1159
- isinstance(msg, HumanMessage)]).apply(
1160
- lambda x: 1 if any(word in x for word in ['happy', 'good', 'great', 'awesome']) else ( O if any(word in x for word in ['okay', 'fine', 'alright', 'normal']) else -1
1161
- )
1162
- mood _timeline.index = pd.to_datetime(mood_timeline.index, unit='s')
1163
- mood_timeline_df = mood_timeline.reset_
1164
- _index)
1165
- mood.
1166
- _timeline_df.columns = ['Time', 'Mood']
1167
- st.line_chart(mood_timeline_df.set_index('Time'))
 
 
 
 
 
 
1168
  else:
1169
- st.write("No mood data to display.")
1170
- else:
1171
- st.write("No messages to summarize.")
1172
 
1173
 
1174
  elif page == "How to use?":
 
675
  summary_response = chat([SystemMessage(content=summary_prompt)])
676
  return summary_response.content.strip()
677
 
678
+ def generate_insights(filename):
679
+ with open(f'{filename}.txt', 'r') as file:
680
+ messages = file.read()
681
+ # messages = sessions_table['messages']
682
+ print("\nMessages \n", messages)
683
+
684
+ if not messages:
685
+ return "No messages to summarize.", pd.DataFrame()
686
+
687
+ conversation_summary = messages
688
+ mood_labels = ['Positive', 'Neutral', 'Negative']
689
+ mood_counts = [10, 0, 0] # Example starting counts
690
+
691
+ for message in messages:
692
+ if isinstance(message, HumanMessage):
693
+ content = message.content.lower()
694
+ if any(word in content for word in ['happy', 'good', 'great', 'awesome']):
695
+ mood_counts[0] += 1
696
+ elif any(word in content for word in ['okay', 'fine', 'alright', 'normal']):
697
+ mood_counts[1] += 1
698
+ elif any(word in content for word in ['sad', 'bad', 'terrible', 'awful']):
699
+ mood_counts[2] += 1
700
+
701
+ # Ensure no NaNs in mood data
702
+ mood_data = [count if count != 0 else 0.1 for count in mood_counts]
703
+
704
+ mood_df = pd.DataFrame({
705
+ 'Mood': mood_labels,
706
+ 'Count': mood_data
707
+ })
708
+
709
+ return messages, conversation_summary, mood_df
710
 
 
711
 
712
  if page == "Today":
713
  # CSS Injection
 
1141
 
1142
  elif page == "Insights":
1143
  st.subheader("Insights")
1144
+
1145
+ # Retrieve session titles for the selectbox
1146
  session_titles = [session["title"] for session in st.session_state['sessions']]
1147
+ selected_session_title = st.selectbox("Select a session", session_titles)
1148
 
1149
  if selected_session_title:
1150
+ # Find the selected session based on the title
1151
  selected_session = next(session for session in st.session_state['sessions'] if session["title"] == selected_session_title)
1152
+
1153
+ if selected_session and 'messages' in selected_session:
1154
+ # Generate insights based on the selected session
1155
+ message, conversation_summary, mood_df = generate_insights(selected_session_title)
1156
+
1157
+ st.write(f"Messages: {message}")
1158
+ st.markdown("### Conversation Summary")
1159
  st.write(conversation_summary)
1160
+
1161
+ # If mood data exists, display mood analysis
1162
+ if not mood_df.empty:
1163
+ st.markdown("### Mood Analysis")
1164
+
1165
+ # Plot a pie chart of the mood analysis
1166
+ fig, ax = plt.subplots()
1167
+ ax.pie(mood_df['Count'], labels=mood_df['Mood'], autopct='%1.1f%%', startangle=90, colors=['#4CAF50', '#FFC107', '#F44336'])
1168
+ ax.axis('equal')
1169
+ st.pyplot(fig)
1170
+
1171
+ # Mood over time chart
1172
+ st.markdown("### Mood Over Time")
1173
+ mood_timeline = pd.Series([msg.content.lower() for msg in selected_session['messages'] if isinstance(msg, HumanMessage)]).apply(
1174
+ lambda x: 1 if any(word in x for word in ['happy', 'good', 'great', 'awesome']) else (
1175
+ 0 if any(word in x for word in ['okay', 'fine', 'alright', 'normal']) else -1
1176
+ )
1177
+ )
1178
+
1179
+ mood_timeline.index = pd.to_datetime(mood_timeline.index, unit='s')
1180
+ mood_timeline_df = mood_timeline.reset_index()
1181
+ mood_timeline_df.columns = ['Time', 'Mood']
1182
+ st.line_chart(mood_timeline_df.set_index('Time'))
1183
+ else:
1184
+ st.write("No mood data to display.")
1185
  else:
1186
+ st.write("No messages to summarize.")
1187
+
 
1188
 
1189
 
1190
  elif page == "How to use?":