Create chat_display.py
Browse files- src/chat_display.py +96 -0
src/chat_display.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
This module handles the chat display components and rendering.
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import streamlit as st
|
| 6 |
+
import os
|
| 7 |
+
from utils import has_meaningful_content, remove_reasoning_and_sources, clean_explanation, get_image_base64
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def get_avatars():
|
| 11 |
+
"""
|
| 12 |
+
Get avatar images for the user and assistant.
|
| 13 |
+
|
| 14 |
+
Returns:
|
| 15 |
+
tuple: (user_avatar, assistant_avatar) - URLs or emoji fallbacks
|
| 16 |
+
"""
|
| 17 |
+
# Define paths to your company logo images
|
| 18 |
+
user_logo_path = "src/assets/icon3.png"
|
| 19 |
+
assistant_logo_path = "src/assets/logo.png"
|
| 20 |
+
|
| 21 |
+
# Get base64 encoded images or use default emoji as fallback
|
| 22 |
+
user_logo_base64 = get_image_base64(user_logo_path)
|
| 23 |
+
assistant_logo_base64 = get_image_base64(assistant_logo_path)
|
| 24 |
+
|
| 25 |
+
# Create avatar URLs with base64 data
|
| 26 |
+
user_avatar = f"data:image/png;base64,{user_logo_base64}" if user_logo_base64 else "👤"
|
| 27 |
+
assistant_avatar = f"data:image/png;base64,{assistant_logo_base64}" if assistant_logo_base64 else "🤖"
|
| 28 |
+
|
| 29 |
+
return user_avatar, assistant_avatar
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def display_chat_history():
|
| 33 |
+
"""
|
| 34 |
+
Display the chat history from session state.
|
| 35 |
+
"""
|
| 36 |
+
user_avatar, assistant_avatar = get_avatars()
|
| 37 |
+
|
| 38 |
+
for message in st.session_state.history:
|
| 39 |
+
if message["role"] == "user":
|
| 40 |
+
# Right-aligned container for user messages
|
| 41 |
+
with st.container():
|
| 42 |
+
col1, col2 = st.columns([2, 10])
|
| 43 |
+
with col2:
|
| 44 |
+
with st.chat_message("user", avatar=user_avatar):
|
| 45 |
+
st.write(message["content"])
|
| 46 |
+
else:
|
| 47 |
+
# Left-aligned container for assistant messages
|
| 48 |
+
with st.container():
|
| 49 |
+
col1, col2 = st.columns([10, 2])
|
| 50 |
+
with col1:
|
| 51 |
+
with st.chat_message("assistant", avatar=assistant_avatar):
|
| 52 |
+
# Display the response text without reasoning or sources sections
|
| 53 |
+
cleaned_response = remove_reasoning_and_sources(message["content"])
|
| 54 |
+
st.markdown(cleaned_response)
|
| 55 |
+
|
| 56 |
+
# Only display the explanation in an expander if it exists AND has actual content
|
| 57 |
+
if message.get("explanation") and has_meaningful_content(message.get("explanation")):
|
| 58 |
+
# Clean up the explanation text
|
| 59 |
+
cleaned_explanation = clean_explanation(message["explanation"])
|
| 60 |
+
|
| 61 |
+
with st.expander("Show Reasoning"):
|
| 62 |
+
st.markdown(cleaned_explanation)
|
| 63 |
+
if message.get("evidence"):
|
| 64 |
+
st.markdown("---")
|
| 65 |
+
st.markdown("**Legend:** 🔓 = Open Access (full text available)")
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def show_typing_indicator():
|
| 69 |
+
"""
|
| 70 |
+
Display a typing indicator when the system is processing a response.
|
| 71 |
+
"""
|
| 72 |
+
if st.session_state.processing:
|
| 73 |
+
user_avatar, assistant_avatar = get_avatars()
|
| 74 |
+
with st.container():
|
| 75 |
+
col1, col2 = st.columns([10, 2])
|
| 76 |
+
with col1:
|
| 77 |
+
with st.chat_message("assistant", avatar=assistant_avatar):
|
| 78 |
+
st.markdown("""
|
| 79 |
+
<div class="typing-indicator">
|
| 80 |
+
<span></span>
|
| 81 |
+
<span></span>
|
| 82 |
+
<span></span>
|
| 83 |
+
</div>
|
| 84 |
+
<p style="margin: 0; color: rgba(255,255,255,0.6) !important; font-size: 0.8rem;">Analyzing your query...</p>
|
| 85 |
+
""", unsafe_allow_html=True)
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def display_legal_disclaimer():
|
| 89 |
+
"""
|
| 90 |
+
Display the legal disclaimer at the bottom of the page.
|
| 91 |
+
"""
|
| 92 |
+
st.markdown("""
|
| 93 |
+
<div class="footer-text">
|
| 94 |
+
For informational purposes only. Not a substitute for professional medical advice.
|
| 95 |
+
</div>
|
| 96 |
+
""", unsafe_allow_html=True)
|