Update src/session_state.py
Browse files- src/session_state.py +96 -12
src/session_state.py
CHANGED
|
@@ -4,43 +4,127 @@ This module handles session state initialization and management.
|
|
| 4 |
|
| 5 |
import streamlit as st
|
| 6 |
import uuid
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
def initialize_session_state():
|
| 10 |
"""
|
| 11 |
Initialize all session state variables needed for the application.
|
| 12 |
"""
|
| 13 |
-
#
|
| 14 |
-
if '
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# Unique consultation ID for tracking sessions
|
| 18 |
if 'consultation_id' not in st.session_state:
|
| 19 |
st.session_state.consultation_id = str(uuid.uuid4())[:8]
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
# RAG feature toggle
|
| 22 |
if 'use_rag' not in st.session_state:
|
| 23 |
st.session_state.use_rag = True
|
| 24 |
-
|
| 25 |
# Processing state for showing typing indicator
|
| 26 |
if 'processing' not in st.session_state:
|
| 27 |
st.session_state.processing = False
|
| 28 |
-
|
| 29 |
# Report generation state
|
| 30 |
if 'show_report_form' not in st.session_state:
|
| 31 |
st.session_state.show_report_form = False
|
| 32 |
-
|
| 33 |
if 'report_step' not in st.session_state:
|
| 34 |
st.session_state.report_step = 0
|
| 35 |
-
|
| 36 |
# Patient information for reports
|
| 37 |
if 'patient_info' not in st.session_state:
|
| 38 |
st.session_state.patient_info = {"name": "", "age": "", "gender": ""}
|
| 39 |
-
|
| 40 |
# PDF report data
|
| 41 |
if 'pdf_data' not in st.session_state:
|
| 42 |
st.session_state.pdf_data = None
|
| 43 |
-
|
| 44 |
# Email form visibility
|
| 45 |
if 'show_email_form' not in st.session_state:
|
| 46 |
-
st.session_state.show_email_form = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
import streamlit as st
|
| 6 |
import uuid
|
| 7 |
+
from database import get_db_client
|
| 8 |
|
| 9 |
|
| 10 |
def initialize_session_state():
|
| 11 |
"""
|
| 12 |
Initialize all session state variables needed for the application.
|
| 13 |
"""
|
| 14 |
+
# Database client
|
| 15 |
+
if 'db_client' not in st.session_state:
|
| 16 |
+
try:
|
| 17 |
+
st.session_state.db_client = get_db_client()
|
| 18 |
+
except Exception as e:
|
| 19 |
+
st.error(f"Failed to initialize database: {str(e)}")
|
| 20 |
+
st.session_state.db_client = None
|
| 21 |
+
|
| 22 |
# Unique consultation ID for tracking sessions
|
| 23 |
if 'consultation_id' not in st.session_state:
|
| 24 |
st.session_state.consultation_id = str(uuid.uuid4())[:8]
|
| 25 |
+
# Create new conversation in database
|
| 26 |
+
if st.session_state.db_client:
|
| 27 |
+
try:
|
| 28 |
+
st.session_state.db_client.create_conversation(st.session_state.consultation_id)
|
| 29 |
+
except Exception as e:
|
| 30 |
+
st.error(f"Failed to create conversation in database: {str(e)}")
|
| 31 |
+
|
| 32 |
+
# Main conversation history (now acts as a cache)
|
| 33 |
+
if 'history' not in st.session_state:
|
| 34 |
+
st.session_state.history = []
|
| 35 |
+
# Try to load from database if consultation_id exists
|
| 36 |
+
if hasattr(st.session_state, 'consultation_id') and st.session_state.db_client:
|
| 37 |
+
try:
|
| 38 |
+
db_history = st.session_state.db_client.get_conversation_history(st.session_state.consultation_id)
|
| 39 |
+
st.session_state.history = db_history
|
| 40 |
+
except Exception as e:
|
| 41 |
+
# Silently continue with empty history if database fetch fails
|
| 42 |
+
pass
|
| 43 |
+
|
| 44 |
# RAG feature toggle
|
| 45 |
if 'use_rag' not in st.session_state:
|
| 46 |
st.session_state.use_rag = True
|
| 47 |
+
|
| 48 |
# Processing state for showing typing indicator
|
| 49 |
if 'processing' not in st.session_state:
|
| 50 |
st.session_state.processing = False
|
| 51 |
+
|
| 52 |
# Report generation state
|
| 53 |
if 'show_report_form' not in st.session_state:
|
| 54 |
st.session_state.show_report_form = False
|
| 55 |
+
|
| 56 |
if 'report_step' not in st.session_state:
|
| 57 |
st.session_state.report_step = 0
|
| 58 |
+
|
| 59 |
# Patient information for reports
|
| 60 |
if 'patient_info' not in st.session_state:
|
| 61 |
st.session_state.patient_info = {"name": "", "age": "", "gender": ""}
|
| 62 |
+
|
| 63 |
# PDF report data
|
| 64 |
if 'pdf_data' not in st.session_state:
|
| 65 |
st.session_state.pdf_data = None
|
| 66 |
+
|
| 67 |
# Email form visibility
|
| 68 |
if 'show_email_form' not in st.session_state:
|
| 69 |
+
st.session_state.show_email_form = False
|
| 70 |
+
|
| 71 |
+
def add_message_to_history(message):
|
| 72 |
+
"""
|
| 73 |
+
Add a message to the conversation history and persist it to database.
|
| 74 |
+
|
| 75 |
+
Args:
|
| 76 |
+
message (dict): The message to add to history
|
| 77 |
+
"""
|
| 78 |
+
# Add to local history
|
| 79 |
+
st.session_state.history.append(message)
|
| 80 |
+
|
| 81 |
+
# Persist to database if available
|
| 82 |
+
if hasattr(st.session_state, 'db_client') and st.session_state.db_client:
|
| 83 |
+
try:
|
| 84 |
+
st.session_state.db_client.save_message(
|
| 85 |
+
st.session_state.consultation_id,
|
| 86 |
+
message
|
| 87 |
+
)
|
| 88 |
+
except Exception as e:
|
| 89 |
+
st.error(f"Failed to save message to database: {str(e)}")
|
| 90 |
+
|
| 91 |
+
def get_full_history():
|
| 92 |
+
"""
|
| 93 |
+
Get the complete conversation history from the database.
|
| 94 |
+
|
| 95 |
+
Returns:
|
| 96 |
+
list: Full conversation history
|
| 97 |
+
"""
|
| 98 |
+
if hasattr(st.session_state, 'db_client') and st.session_state.db_client:
|
| 99 |
+
try:
|
| 100 |
+
return st.session_state.db_client.get_conversation_history(
|
| 101 |
+
st.session_state.consultation_id
|
| 102 |
+
)
|
| 103 |
+
except Exception as e:
|
| 104 |
+
st.error(f"Failed to retrieve history from database: {str(e)}")
|
| 105 |
+
|
| 106 |
+
# Fallback to session state history if database retrieval fails
|
| 107 |
+
return st.session_state.history
|
| 108 |
+
|
| 109 |
+
def end_conversation():
|
| 110 |
+
"""
|
| 111 |
+
End the current conversation and clean up resources.
|
| 112 |
+
"""
|
| 113 |
+
if hasattr(st.session_state, 'db_client') and st.session_state.db_client:
|
| 114 |
+
try:
|
| 115 |
+
st.session_state.db_client.delete_conversation(
|
| 116 |
+
st.session_state.consultation_id
|
| 117 |
+
)
|
| 118 |
+
except Exception as e:
|
| 119 |
+
st.error(f"Failed to delete conversation from database: {str(e)}")
|
| 120 |
+
|
| 121 |
+
# Reset session state
|
| 122 |
+
st.session_state.history = []
|
| 123 |
+
st.session_state.consultation_id = str(uuid.uuid4())[:8]
|
| 124 |
+
|
| 125 |
+
# Create new conversation in database
|
| 126 |
+
if hasattr(st.session_state, 'db_client') and st.session_state.db_client:
|
| 127 |
+
try:
|
| 128 |
+
st.session_state.db_client.create_conversation(st.session_state.consultation_id)
|
| 129 |
+
except Exception as e:
|
| 130 |
+
st.error(f"Failed to create new conversation in database: {str(e)}")
|