Spaces:
Build error
Build error
Update components/chat.py
Browse files- components/chat.py +77 -11
components/chat.py
CHANGED
|
@@ -2,7 +2,23 @@ import streamlit as st
|
|
| 2 |
from langchain_core.messages import HumanMessage, AIMessage
|
| 3 |
from utils.database import verify_vector_store
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def display_chat_interface():
|
| 8 |
"""Display modern chat interface with clean formatting."""
|
|
@@ -37,6 +53,7 @@ def display_chat_interface():
|
|
| 37 |
font-weight: bold;
|
| 38 |
color: #0f52ba;
|
| 39 |
margin-top: 1rem;
|
|
|
|
| 40 |
}
|
| 41 |
|
| 42 |
.source-info {
|
|
@@ -45,23 +62,53 @@ def display_chat_interface():
|
|
| 45 |
border-left: 3px solid #0f52ba;
|
| 46 |
margin-top: 1rem;
|
| 47 |
font-size: 0.9rem;
|
|
|
|
| 48 |
}
|
| 49 |
|
| 50 |
-
/*
|
| 51 |
-
.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
margin: 0.5rem 0;
|
|
|
|
|
|
|
|
|
|
| 53 |
}
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
}
|
| 59 |
</style>
|
| 60 |
""", unsafe_allow_html=True)
|
| 61 |
-
|
|
|
|
| 62 |
if 'qa_system' not in st.session_state or st.session_state.qa_system is None:
|
| 63 |
st.warning("Please upload documents first to initialize the chat system.")
|
| 64 |
return
|
|
|
|
| 65 |
# Initialize chat history
|
| 66 |
if 'messages' not in st.session_state:
|
| 67 |
st.session_state.messages = []
|
|
@@ -76,14 +123,31 @@ def display_chat_interface():
|
|
| 76 |
""", unsafe_allow_html=True)
|
| 77 |
elif isinstance(message, AIMessage):
|
| 78 |
# Clean and structure the response
|
| 79 |
-
response_content = message.content
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
st.markdown(f"""
|
| 84 |
<div class="assistant-message">
|
| 85 |
🤖 <strong>Assistant:</strong><br>
|
| 86 |
-
{
|
| 87 |
</div>
|
| 88 |
""", unsafe_allow_html=True)
|
| 89 |
|
|
@@ -112,4 +176,6 @@ def display_chat_interface():
|
|
| 112 |
st.error(f"Error: {e}")
|
| 113 |
import traceback
|
| 114 |
st.error(traceback.format_exc())
|
|
|
|
|
|
|
| 115 |
|
|
|
|
| 2 |
from langchain_core.messages import HumanMessage, AIMessage
|
| 3 |
from utils.database import verify_vector_store
|
| 4 |
|
| 5 |
+
def clean_response_content(response_content):
|
| 6 |
+
"""Clean the response content of any technical metadata."""
|
| 7 |
+
# Remove the content= prefix if it exists
|
| 8 |
+
if "content='" in response_content:
|
| 9 |
+
response_content = response_content.split("content='")[1]
|
| 10 |
+
|
| 11 |
+
# Remove response metadata
|
| 12 |
+
if "response_metadata=" in response_content:
|
| 13 |
+
response_content = response_content.split("response_metadata=")[0]
|
| 14 |
+
|
| 15 |
+
# Remove any trailing quotes or metadata artifacts
|
| 16 |
+
response_content = response_content.rstrip("' ")
|
| 17 |
+
|
| 18 |
+
# Replace escaped newlines with actual newlines
|
| 19 |
+
response_content = response_content.replace('\\n', '\n')
|
| 20 |
+
|
| 21 |
+
return response_content
|
| 22 |
|
| 23 |
def display_chat_interface():
|
| 24 |
"""Display modern chat interface with clean formatting."""
|
|
|
|
| 53 |
font-weight: bold;
|
| 54 |
color: #0f52ba;
|
| 55 |
margin-top: 1rem;
|
| 56 |
+
font-size: 1.2em;
|
| 57 |
}
|
| 58 |
|
| 59 |
.source-info {
|
|
|
|
| 62 |
border-left: 3px solid #0f52ba;
|
| 63 |
margin-top: 1rem;
|
| 64 |
font-size: 0.9rem;
|
| 65 |
+
font-style: italic;
|
| 66 |
}
|
| 67 |
|
| 68 |
+
/* Summary section */
|
| 69 |
+
.summary-section {
|
| 70 |
+
background-color: #f8f9fa;
|
| 71 |
+
padding: 1rem;
|
| 72 |
+
border-radius: 6px;
|
| 73 |
+
margin-bottom: 1rem;
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
/* Key points */
|
| 77 |
+
.key-points {
|
| 78 |
+
margin: 1rem 0;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
.key-point-item {
|
| 82 |
margin: 0.5rem 0;
|
| 83 |
+
line-height: 1.5;
|
| 84 |
+
padding-left: 1.5rem;
|
| 85 |
+
position: relative;
|
| 86 |
}
|
| 87 |
|
| 88 |
+
.key-point-item:before {
|
| 89 |
+
content: "•";
|
| 90 |
+
position: absolute;
|
| 91 |
+
left: 0.5rem;
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
/* Content sections */
|
| 95 |
+
.content-section {
|
| 96 |
+
margin: 1rem 0;
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
/* Bold text */
|
| 100 |
+
.highlight {
|
| 101 |
+
font-weight: 600;
|
| 102 |
+
color: #1a365d;
|
| 103 |
}
|
| 104 |
</style>
|
| 105 |
""", unsafe_allow_html=True)
|
| 106 |
+
|
| 107 |
+
# Check if QA system is initialized
|
| 108 |
if 'qa_system' not in st.session_state or st.session_state.qa_system is None:
|
| 109 |
st.warning("Please upload documents first to initialize the chat system.")
|
| 110 |
return
|
| 111 |
+
|
| 112 |
# Initialize chat history
|
| 113 |
if 'messages' not in st.session_state:
|
| 114 |
st.session_state.messages = []
|
|
|
|
| 123 |
""", unsafe_allow_html=True)
|
| 124 |
elif isinstance(message, AIMessage):
|
| 125 |
# Clean and structure the response
|
| 126 |
+
response_content = clean_response_content(message.content)
|
| 127 |
+
|
| 128 |
+
# Format the content into sections
|
| 129 |
+
formatted_content = ""
|
| 130 |
+
sections = response_content.split('\n\n')
|
| 131 |
+
|
| 132 |
+
for section in sections:
|
| 133 |
+
if section.startswith('Summary:'):
|
| 134 |
+
formatted_content += f'<div class="summary-section">{section}</div>'
|
| 135 |
+
elif section.startswith('Key Points:'):
|
| 136 |
+
formatted_content += '<div class="key-points">'
|
| 137 |
+
formatted_content += f'<div class="section-header">{section.split(":")[0]}:</div>'
|
| 138 |
+
points = [p.strip() for p in section.split('\n')[1:] if p.strip()]
|
| 139 |
+
for point in points:
|
| 140 |
+
formatted_content += f'<div class="key-point-item">{point.lstrip("- ")}</div>'
|
| 141 |
+
formatted_content += '</div>'
|
| 142 |
+
elif section.startswith('Source'):
|
| 143 |
+
formatted_content += f'<div class="source-info">{section}</div>'
|
| 144 |
+
else:
|
| 145 |
+
formatted_content += f'<div class="content-section">{section}</div>'
|
| 146 |
|
| 147 |
st.markdown(f"""
|
| 148 |
<div class="assistant-message">
|
| 149 |
🤖 <strong>Assistant:</strong><br>
|
| 150 |
+
{formatted_content}
|
| 151 |
</div>
|
| 152 |
""", unsafe_allow_html=True)
|
| 153 |
|
|
|
|
| 176 |
st.error(f"Error: {e}")
|
| 177 |
import traceback
|
| 178 |
st.error(traceback.format_exc())
|
| 179 |
+
|
| 180 |
+
|
| 181 |
|