Update src/streamlit_app.py
Browse files- src/streamlit_app.py +1 -98
src/streamlit_app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from styles import apply_styling
|
| 3 |
-
from utils import remove_reasoning_and_sources, clean_explanation
|
| 4 |
from session_state import initialize_session_state, add_message_to_history, get_full_history
|
| 5 |
from chat_display import display_chat_history, show_typing_indicator, display_legal_disclaimer
|
| 6 |
from model import (
|
|
@@ -48,100 +48,3 @@ with st.sidebar:
|
|
| 48 |
# Reset report step for a new report
|
| 49 |
st.session_state.report_step = 1
|
| 50 |
st.rerun()
|
| 51 |
-
|
| 52 |
-
# # Add End Conversation button
|
| 53 |
-
# if st.button("End Conversation", use_container_width=True):
|
| 54 |
-
# end_conversation()
|
| 55 |
-
# st.rerun()
|
| 56 |
-
|
| 57 |
-
# Show report form when button is clicked - only if not processing conversation
|
| 58 |
-
if st.session_state.get('show_report_form', False) and not st.session_state.get('processing', False) and not st.session_state.get('conversation_lock', False):
|
| 59 |
-
st.subheader("Report Information")
|
| 60 |
-
generate_and_download_report()
|
| 61 |
-
|
| 62 |
-
# Main app area
|
| 63 |
-
st.title("How are you Feeling Today?")
|
| 64 |
-
|
| 65 |
-
# Display chat history
|
| 66 |
-
display_chat_history()
|
| 67 |
-
|
| 68 |
-
# Display typing indicator if processing
|
| 69 |
-
show_typing_indicator()
|
| 70 |
-
|
| 71 |
-
# Chat input
|
| 72 |
-
if prompt := st.chat_input("Describe your symptoms or ask a medical question..."):
|
| 73 |
-
# Set conversation lock to prioritize conversation over report generation
|
| 74 |
-
st.session_state.conversation_lock = True
|
| 75 |
-
|
| 76 |
-
# Add user message to history using the database-backed function
|
| 77 |
-
add_message_to_history({"role": "user", "content": prompt})
|
| 78 |
-
|
| 79 |
-
# Set processing flag to true
|
| 80 |
-
st.session_state.processing = True
|
| 81 |
-
|
| 82 |
-
# Force refresh to show the typing indicator
|
| 83 |
-
st.rerun()
|
| 84 |
-
|
| 85 |
-
# Check if we need to process a response (this block runs after the rerun if processing is True)
|
| 86 |
-
if st.session_state.processing:
|
| 87 |
-
try:
|
| 88 |
-
# Get the full history from database
|
| 89 |
-
full_history = get_full_history()
|
| 90 |
-
|
| 91 |
-
# Get the last user message
|
| 92 |
-
last_user_message = next((msg["content"] for msg in reversed(full_history)
|
| 93 |
-
if msg["role"] == "user"), None)
|
| 94 |
-
|
| 95 |
-
if last_user_message:
|
| 96 |
-
# Process as normal conversation
|
| 97 |
-
# Filter out the last user message as it will be processed separately
|
| 98 |
-
previous_messages = [msg for msg in full_history if msg["role"] == "assistant" or
|
| 99 |
-
(msg["role"] == "user" and msg["content"] != last_user_message)]
|
| 100 |
-
|
| 101 |
-
# Updated to handle the new response format with follow-up questions and sources
|
| 102 |
-
reply, explanation, follow_up_questions, sources, evidence = orchestrator_chat(
|
| 103 |
-
previous_messages,
|
| 104 |
-
last_user_message,
|
| 105 |
-
use_rag=st.session_state.use_rag,
|
| 106 |
-
is_follow_up=len(previous_messages) > 0
|
| 107 |
-
)
|
| 108 |
-
|
| 109 |
-
# Clean up the response and explanation
|
| 110 |
-
cleaned_reply = remove_reasoning_and_sources(reply) if reply else ""
|
| 111 |
-
cleaned_explanation = clean_explanation(explanation) if explanation else ""
|
| 112 |
-
|
| 113 |
-
# Format follow-up questions for better display
|
| 114 |
-
formatted_follow_up = format_follow_up_questions(follow_up_questions) if follow_up_questions else ""
|
| 115 |
-
|
| 116 |
-
# Add assistant response to history with the new fields
|
| 117 |
-
assistant_message = {
|
| 118 |
-
"role": "assistant",
|
| 119 |
-
"content": cleaned_reply,
|
| 120 |
-
"explanation": cleaned_explanation,
|
| 121 |
-
"follow_up_questions": formatted_follow_up,
|
| 122 |
-
"sources": sources,
|
| 123 |
-
"evidence": evidence if evidence else []
|
| 124 |
-
}
|
| 125 |
-
|
| 126 |
-
# Save to database-backed history
|
| 127 |
-
add_message_to_history(assistant_message)
|
| 128 |
-
finally:
|
| 129 |
-
# Set processing back to false regardless of success/failure
|
| 130 |
-
st.session_state.processing = False
|
| 131 |
-
# Release conversation lock after response is processed
|
| 132 |
-
st.session_state.conversation_lock = False
|
| 133 |
-
|
| 134 |
-
# Force refresh to update the UI with the response
|
| 135 |
-
st.rerun()
|
| 136 |
-
|
| 137 |
-
# Small, unobtrusive legal disclaimer
|
| 138 |
-
display_legal_disclaimer()
|
| 139 |
-
|
| 140 |
-
# This ensures app.py can be used as a direct entry point for deployment platforms like Hugging Face
|
| 141 |
-
if __name__ == "__main__":
|
| 142 |
-
# Add the current directory to the Python path if needed
|
| 143 |
-
import os
|
| 144 |
-
import sys
|
| 145 |
-
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 146 |
-
if current_dir not in sys.path:
|
| 147 |
-
sys.path.insert(0, current_dir)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from styles import apply_styling
|
| 3 |
+
from utils import remove_reasoning_and_sources, clean_explanation
|
| 4 |
from session_state import initialize_session_state, add_message_to_history, get_full_history
|
| 5 |
from chat_display import display_chat_history, show_typing_indicator, display_legal_disclaimer
|
| 6 |
from model import (
|
|
|
|
| 48 |
# Reset report step for a new report
|
| 49 |
st.session_state.report_step = 1
|
| 50 |
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|