Spaces:
Runtime error
Runtime error
Update pages/previous_chats.py
Browse files- pages/previous_chats.py +44 -10
pages/previous_chats.py
CHANGED
|
@@ -31,8 +31,9 @@ def show_previous_chats_tab(storage_manager):
|
|
| 31 |
|
| 32 |
# Display chats
|
| 33 |
for chat in chats:
|
| 34 |
-
#
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
with st.expander(f"Session from {display_time}"):
|
| 38 |
# Show associated images if any
|
|
@@ -41,12 +42,45 @@ def show_previous_chats_tab(storage_manager):
|
|
| 41 |
for idx, img_data in enumerate(chat['images']):
|
| 42 |
cols[idx % 3].image(img_data, caption=f"Chart {idx + 1}")
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
# Show analysis history
|
| 45 |
-
for entry in
|
| 46 |
-
if entry
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Display chats
|
| 33 |
for chat in chats:
|
| 34 |
+
# Get the timestamp from metadata
|
| 35 |
+
timestamp = chat.get('timestamp') or chat.get('formatted_timestamp', 'No date')
|
| 36 |
+
display_time = format_timestamp(timestamp)
|
| 37 |
|
| 38 |
with st.expander(f"Session from {display_time}"):
|
| 39 |
# Show associated images if any
|
|
|
|
| 42 |
for idx, img_data in enumerate(chat['images']):
|
| 43 |
cols[idx % 3].image(img_data, caption=f"Chart {idx + 1}")
|
| 44 |
|
| 45 |
+
# Get the chat history from the correct location in the data structure
|
| 46 |
+
chat_history = chat.get('data', [])
|
| 47 |
+
if isinstance(chat_history, list):
|
| 48 |
+
# Direct list of chat entries
|
| 49 |
+
entries = chat_history
|
| 50 |
+
else:
|
| 51 |
+
# Nested under 'chat_history' key
|
| 52 |
+
entries = chat_history.get('chat_history', [])
|
| 53 |
+
|
| 54 |
# Show analysis history
|
| 55 |
+
for entry in entries:
|
| 56 |
+
if isinstance(entry, dict): # Ensure entry is a dictionary
|
| 57 |
+
analysis_type = entry.get('analysis_type', '')
|
| 58 |
+
if analysis_type in ['Individual', 'Correlated Analysis']:
|
| 59 |
+
st.markdown("**Initial Analysis:**")
|
| 60 |
+
elif 'question' in entry:
|
| 61 |
+
st.markdown(f"**Follow-up Question:** {entry['question']}")
|
| 62 |
+
|
| 63 |
+
analysis = entry.get('analysis', '')
|
| 64 |
+
if analysis:
|
| 65 |
+
st.markdown(analysis)
|
| 66 |
+
st.markdown("---")
|
| 67 |
+
|
| 68 |
+
# Add buttons for chat actions
|
| 69 |
+
col1, col2 = st.columns(2)
|
| 70 |
+
with col1:
|
| 71 |
+
if st.button("Continue Analysis", key=f"continue_{chat.get('id', '')}"):
|
| 72 |
+
# Load this chat into current session
|
| 73 |
+
st.session_state.chat_history = entries
|
| 74 |
+
if len(entries) > 0:
|
| 75 |
+
st.session_state.current_analysis = entries[-1].get('analysis', '')
|
| 76 |
+
if 'images' in chat:
|
| 77 |
+
st.session_state.current_images = chat['images']
|
| 78 |
+
st.success("Analysis loaded! Switch to Chart Analysis tab to continue.")
|
| 79 |
+
|
| 80 |
+
with col2:
|
| 81 |
+
if st.button("Delete Chat", key=f"delete_{chat.get('id', '')}"):
|
| 82 |
+
if storage_manager.delete_chat(chat.get('id', '')):
|
| 83 |
+
st.success("Chat deleted successfully!")
|
| 84 |
+
st.rerun()
|
| 85 |
+
else:
|
| 86 |
+
st.error("Failed to delete chat.")
|