Spaces:
Runtime error
Runtime error
Create previous_chats.py
Browse files- pages/previous_chats.py +62 -0
pages/previous_chats.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# pages/previous_chats.py
|
| 2 |
+
|
| 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")
|
| 9 |
+
|
| 10 |
+
# Get all chats
|
| 11 |
+
chats = storage_manager.get_all_chats()
|
| 12 |
+
|
| 13 |
+
if not chats:
|
| 14 |
+
st.info("No previous analysis sessions found.")
|
| 15 |
+
return
|
| 16 |
+
|
| 17 |
+
# Display chats
|
| 18 |
+
for chat in chats:
|
| 19 |
+
with st.expander(f"Session from {datetime.fromisoformat(chat['timestamp']).strftime('%Y-%m-%d %H:%M')}"):
|
| 20 |
+
# Show associated images if any
|
| 21 |
+
if 'images' in chat:
|
| 22 |
+
cols = st.columns(min(len(chat['images']), 3))
|
| 23 |
+
for idx, img_data in enumerate(chat['images']):
|
| 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 |
+
)
|