mgbam commited on
Commit
0bff4c5
·
verified ·
1 Parent(s): 2b9aa0c

Update pages/3_Reports.py

Browse files
Files changed (1) hide show
  1. pages/3_Reports.py +79 -0
pages/3_Reports.py CHANGED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from datetime import datetime
3
+
4
+ from config.settings import settings
5
+ from models import ChatMessage, ChatSession, User
6
+ from models.db import get_session_context
7
+ from services.pdf_report import generate_pdf_report
8
+ from services.logger import app_logger
9
+ from services.metrics import log_report_generated
10
+
11
+ st.set_page_config(page_title=f"Reports - {settings.APP_TITLE}", layout="wide")
12
+
13
+ if not st.session_state.get("authenticated_user"):
14
+ st.warning("Please log in to access reports.")
15
+ st.switch_page("app.py")
16
+
17
+ current_user: User = st.session_state.authenticated_user
18
+
19
+ st.title("Consultation Reports")
20
+ st.markdown("View and download your past consultation sessions.")
21
+
22
+ # --- Load User's Chat Sessions ---
23
+ @st.cache_data(ttl=300) # Cache for 5 minutes
24
+ def get_user_sessions(user_id: int):
25
+ with get_session_context() as db:
26
+ sessions = db.query(ChatSession).filter(ChatSession.user_id == user_id).order_by(ChatSession.start_time.desc()).all()
27
+ return sessions
28
+
29
+ chat_sessions = get_user_sessions(current_user.id)
30
+
31
+ if not chat_sessions:
32
+ st.info("You have no past consultation sessions.")
33
+ st.stop()
34
+
35
+ # --- Display Sessions and Download Option ---
36
+ selected_session_id_str = st.selectbox(
37
+ "Select a Consultation Session:",
38
+ options=[(f"Session ID: {s.id} - Started: {s.start_time.strftime('%Y-%m-%d %H:%M')} - Title: {s.title or 'Untitled'}", str(s.id)) for s in chat_sessions],
39
+ format_func=lambda x: x[0] # Display the descriptive string
40
+ )
41
+
42
+ if selected_session_id_str:
43
+ selected_session_id = int(selected_session_id_str[1]) # Extract ID from tuple
44
+
45
+ # Find the selected session object
46
+ selected_session = next((s for s in chat_sessions if s.id == selected_session_id), None)
47
+
48
+ if selected_session:
49
+ st.subheader(f"Details for Session ID: {selected_session.id}")
50
+ st.write(f"Started: {selected_session.start_time.strftime('%Y-%m-%d %H:%M:%S UTC')}")
51
+ st.write(f"Title: {selected_session.title or 'Untitled'}")
52
+
53
+ with get_session_context() as db:
54
+ messages = db.query(ChatMessage).filter(ChatMessage.session_id == selected_session.id).order_by(ChatMessage.timestamp).all()
55
+
56
+ if messages:
57
+ with st.expander("View Chat Transcript", expanded=False):
58
+ for msg in messages:
59
+ icon = "🧑‍⚕️" if msg.role == "assistant" else "👤"
60
+ if msg.role == "tool": icon = "🛠️"
61
+ st.markdown(f"**{icon} {msg.role.capitalize()} ({msg.timestamp.strftime('%H:%M:%S')}):**")
62
+ st.markdown(f"> {msg.content}\n---")
63
+
64
+ # PDF Download Button
65
+ try:
66
+ pdf_bytes = generate_pdf_report(messages, patient_name=current_user.username) # Or a more specific patient name if available
67
+
68
+ st.download_button(
69
+ label="Download Report as PDF",
70
+ data=pdf_bytes,
71
+ file_name=f"consultation_report_{selected_session.id}_{datetime.now().strftime('%Y%m%d')}.pdf",
72
+ mime="application/pdf",
73
+ on_click=log_report_generated
74
+ )
75
+ except Exception as e:
76
+ app_logger.error(f"Error generating PDF for session {selected_session.id}: {e}")
77
+ st.error(f"Could not generate PDF report: {e}")
78
+ else:
79
+ st.info("This session has no messages.")