Spaces:
Build error
Build error
Create components/styled_response.py
Browse files- components/styled_response.py +143 -0
components/styled_response.py
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# components/styled_response.py
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from utils.response_formatter import display_formatted_response
|
| 5 |
+
|
| 6 |
+
def add_custom_styles():
|
| 7 |
+
"""Add custom CSS styles for response formatting."""
|
| 8 |
+
st.markdown("""
|
| 9 |
+
<style>
|
| 10 |
+
/* Section styling */
|
| 11 |
+
h3 {
|
| 12 |
+
color: #1f77b4;
|
| 13 |
+
border-bottom: 2px solid #1f77b4;
|
| 14 |
+
padding-bottom: 8px;
|
| 15 |
+
margin-top: 20px;
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
/* Bullet points styling */
|
| 19 |
+
.stMarkdown ul {
|
| 20 |
+
list-style-type: none;
|
| 21 |
+
padding-left: 20px;
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
.stMarkdown ul li:before {
|
| 25 |
+
content: "•";
|
| 26 |
+
color: #1f77b4;
|
| 27 |
+
font-weight: bold;
|
| 28 |
+
display: inline-block;
|
| 29 |
+
width: 1em;
|
| 30 |
+
margin-left: -1em;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
/* Source section styling */
|
| 34 |
+
.source-section {
|
| 35 |
+
background-color: #f0f2f6;
|
| 36 |
+
border-left: 4px solid #1f77b4;
|
| 37 |
+
padding: 10px;
|
| 38 |
+
margin-top: 20px;
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
/* Copy section styling */
|
| 42 |
+
.copy-section {
|
| 43 |
+
background-color: #ffffff;
|
| 44 |
+
border: 1px solid #e0e0e0;
|
| 45 |
+
border-radius: 4px;
|
| 46 |
+
padding: 15px;
|
| 47 |
+
margin-top: 10px;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
/* Important points highlighting */
|
| 51 |
+
.highlight {
|
| 52 |
+
background-color: #fff3cd;
|
| 53 |
+
padding: 2px 5px;
|
| 54 |
+
border-radius: 3px;
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
/* Metrics styling */
|
| 58 |
+
.metric-container {
|
| 59 |
+
border: 1px solid #e0e0e0;
|
| 60 |
+
border-radius: 4px;
|
| 61 |
+
padding: 10px;
|
| 62 |
+
margin: 10px 0;
|
| 63 |
+
background-color: #f8f9fa;
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
.metric-value {
|
| 67 |
+
font-size: 1.2em;
|
| 68 |
+
font-weight: bold;
|
| 69 |
+
color: #1f77b4;
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
/* Table styling */
|
| 73 |
+
.styled-table {
|
| 74 |
+
border-collapse: collapse;
|
| 75 |
+
width: 100%;
|
| 76 |
+
margin: 10px 0;
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
.styled-table th {
|
| 80 |
+
background-color: #1f77b4;
|
| 81 |
+
color: white;
|
| 82 |
+
padding: 12px;
|
| 83 |
+
text-align: left;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
.styled-table td {
|
| 87 |
+
padding: 8px;
|
| 88 |
+
border-bottom: 1px solid #e0e0e0;
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
/* Quote styling */
|
| 92 |
+
blockquote {
|
| 93 |
+
border-left: 4px solid #1f77b4;
|
| 94 |
+
margin: 10px 0;
|
| 95 |
+
padding-left: 15px;
|
| 96 |
+
color: #666;
|
| 97 |
+
}
|
| 98 |
+
</style>
|
| 99 |
+
""", unsafe_allow_html=True)
|
| 100 |
+
|
| 101 |
+
def display_styled_chat_message(message, is_user=False):
|
| 102 |
+
"""
|
| 103 |
+
Display a styled chat message with formatting.
|
| 104 |
+
|
| 105 |
+
Args:
|
| 106 |
+
message: The chat message to display
|
| 107 |
+
is_user (bool): Whether the message is from the user
|
| 108 |
+
"""
|
| 109 |
+
# Add custom styles
|
| 110 |
+
add_custom_styles()
|
| 111 |
+
|
| 112 |
+
if is_user:
|
| 113 |
+
st.chat_message("user").write(message)
|
| 114 |
+
else:
|
| 115 |
+
with st.chat_message("assistant"):
|
| 116 |
+
display_formatted_response(
|
| 117 |
+
message.content,
|
| 118 |
+
metadata=getattr(message, 'metadata', None)
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
def display_copyable_sections(content: str):
|
| 122 |
+
"""
|
| 123 |
+
Display content in copyable sections.
|
| 124 |
+
|
| 125 |
+
Args:
|
| 126 |
+
content (str): The content to display
|
| 127 |
+
"""
|
| 128 |
+
sections = content.split("###")[1:] # Skip the first empty section
|
| 129 |
+
|
| 130 |
+
for section in sections:
|
| 131 |
+
if section.strip():
|
| 132 |
+
lines = section.strip().split("\n")
|
| 133 |
+
title = lines[0].strip()
|
| 134 |
+
content = "\n".join(lines[1:]).strip()
|
| 135 |
+
|
| 136 |
+
with st.expander(f"{title}", expanded=False):
|
| 137 |
+
st.markdown(f"### {title}")
|
| 138 |
+
st.markdown(content)
|
| 139 |
+
|
| 140 |
+
# Add copy button
|
| 141 |
+
if st.button(f"Copy {title}", key=f"copy_{title}"):
|
| 142 |
+
st.code(content, language="text")
|
| 143 |
+
st.success(f"{title} content copied to clipboard!")
|