Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
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 |
-
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"
|
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 |
-
|
1144 |
-
|
1145 |
-
|
1146 |
-
|
1147 |
-
|
|
|
|
|
1148 |
st.write(conversation_summary)
|
1149 |
-
|
1150 |
-
|
1151 |
-
|
1152 |
-
|
1153 |
-
|
1154 |
-
|
1155 |
-
|
1156 |
-
|
1157 |
-
|
1158 |
-
|
1159 |
-
|
1160 |
-
|
1161 |
-
|
1162 |
-
|
1163 |
-
|
1164 |
-
|
1165 |
-
|
1166 |
-
|
1167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
1168 |
else:
|
1169 |
-
st.write("No
|
1170 |
-
|
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?":
|