Spaces:
Runtime error
Runtime error
Update pages/previous_chats.py
Browse files- pages/previous_chats.py +21 -31
pages/previous_chats.py
CHANGED
|
@@ -3,6 +3,21 @@
|
|
| 3 |
import streamlit as st
|
| 4 |
from datetime import datetime
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def show_previous_chats_tab(storage_manager):
|
| 7 |
"""Display previous chats tab"""
|
| 8 |
st.title("📚 Previous Analysis Sessions")
|
|
@@ -16,7 +31,10 @@ def show_previous_chats_tab(storage_manager):
|
|
| 16 |
|
| 17 |
# Display chats
|
| 18 |
for chat in chats:
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
# Show associated images if any
|
| 21 |
if 'images' in chat:
|
| 22 |
cols = st.columns(min(len(chat['images']), 3))
|
|
@@ -24,39 +42,11 @@ def show_previous_chats_tab(storage_manager):
|
|
| 24 |
cols[idx % 3].image(img_data, caption=f"Chart {idx + 1}")
|
| 25 |
|
| 26 |
# Show analysis history
|
| 27 |
-
for entry in chat.get('chat_history', []):
|
| 28 |
if entry.get('analysis_type') in ['Individual', 'Correlated Analysis']:
|
| 29 |
st.markdown("**Initial Analysis:**")
|
| 30 |
elif entry.get('question'):
|
| 31 |
st.markdown(f"**Follow-up Question:** {entry['question']}")
|
| 32 |
|
| 33 |
st.markdown(entry.get('analysis', ''))
|
| 34 |
-
st.markdown("---")
|
| 35 |
-
|
| 36 |
-
# Add option to continue analysis
|
| 37 |
-
if st.button("Continue This Analysis", key=f"continue_{chat['id']}"):
|
| 38 |
-
# Load this chat's context into current session
|
| 39 |
-
st.session_state.chat_history = chat.get('chat_history', [])
|
| 40 |
-
st.session_state.current_analysis = chat.get('chat_history', [])[-1]['analysis']
|
| 41 |
-
if 'images' in chat:
|
| 42 |
-
st.session_state.current_images = chat['images']
|
| 43 |
-
st.success("Context loaded! Switch to Chart Analysis tab to continue.")
|
| 44 |
-
|
| 45 |
-
# Add export option
|
| 46 |
-
if st.button("Export Analysis", key=f"export_{chat['id']}"):
|
| 47 |
-
# Create markdown export
|
| 48 |
-
export_text = f"# Analysis Session {chat['timestamp']}\n\n"
|
| 49 |
-
for entry in chat.get('chat_history', []):
|
| 50 |
-
if entry.get('analysis_type'):
|
| 51 |
-
export_text += f"\n## {entry['analysis_type']}\n"
|
| 52 |
-
if entry.get('question'):
|
| 53 |
-
export_text += f"\n### Question: {entry['question']}\n"
|
| 54 |
-
export_text += f"\n{entry.get('analysis', '')}\n"
|
| 55 |
-
|
| 56 |
-
# Provide download button
|
| 57 |
-
st.download_button(
|
| 58 |
-
"Download Analysis",
|
| 59 |
-
export_text,
|
| 60 |
-
file_name=f"analysis_{chat['id']}.md",
|
| 61 |
-
mime="text/markdown"
|
| 62 |
-
)
|
|
|
|
| 3 |
import streamlit as st
|
| 4 |
from datetime import datetime
|
| 5 |
|
| 6 |
+
def format_timestamp(timestamp_str):
|
| 7 |
+
"""Format timestamp for display, handling both ISO and custom formats"""
|
| 8 |
+
try:
|
| 9 |
+
# Try parsing as ISO format
|
| 10 |
+
dt = datetime.fromisoformat(timestamp_str)
|
| 11 |
+
return dt.strftime('%Y-%m-%d %H:%M')
|
| 12 |
+
except ValueError:
|
| 13 |
+
# If not ISO format, try parsing the custom format
|
| 14 |
+
try:
|
| 15 |
+
dt = datetime.strptime(timestamp_str, "%Y%m%d_%H%M%S")
|
| 16 |
+
return dt.strftime('%Y-%m-%d %H:%M')
|
| 17 |
+
except ValueError:
|
| 18 |
+
# If all parsing fails, return the original string
|
| 19 |
+
return timestamp_str
|
| 20 |
+
|
| 21 |
def show_previous_chats_tab(storage_manager):
|
| 22 |
"""Display previous chats tab"""
|
| 23 |
st.title("📚 Previous Analysis Sessions")
|
|
|
|
| 31 |
|
| 32 |
# Display chats
|
| 33 |
for chat in chats:
|
| 34 |
+
# Use formatted timestamp or fall back to timestamp
|
| 35 |
+
display_time = format_timestamp(chat.get('formatted_timestamp', chat.get('timestamp', 'No date')))
|
| 36 |
+
|
| 37 |
+
with st.expander(f"Session from {display_time}"):
|
| 38 |
# Show associated images if any
|
| 39 |
if 'images' in chat:
|
| 40 |
cols = st.columns(min(len(chat['images']), 3))
|
|
|
|
| 42 |
cols[idx % 3].image(img_data, caption=f"Chart {idx + 1}")
|
| 43 |
|
| 44 |
# Show analysis history
|
| 45 |
+
for entry in chat.get('data', {}).get('chat_history', []):
|
| 46 |
if entry.get('analysis_type') in ['Individual', 'Correlated Analysis']:
|
| 47 |
st.markdown("**Initial Analysis:**")
|
| 48 |
elif entry.get('question'):
|
| 49 |
st.markdown(f"**Follow-up Question:** {entry['question']}")
|
| 50 |
|
| 51 |
st.markdown(entry.get('analysis', ''))
|
| 52 |
+
st.markdown("---")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|