Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -41,13 +41,34 @@ html, body {
|
|
| 41 |
height: 100%;
|
| 42 |
margin: 0;
|
| 43 |
}
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
header[data-testid="stHeader"] {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
display: none;
|
| 47 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
footer {
|
| 49 |
display: none;
|
| 50 |
}
|
|
|
|
| 51 |
/* 3. SIDEBAR STYLING (INDEPENDENT LEFT PANEL SCROLL) */
|
| 52 |
[data-testid="stSidebar"] {
|
| 53 |
position: fixed;
|
|
@@ -57,7 +78,6 @@ footer {
|
|
| 57 |
width: 20rem;
|
| 58 |
overflow-y: auto !important;
|
| 59 |
z-index: 99999;
|
| 60 |
-
transition: transform 0.3s ease;
|
| 61 |
}
|
| 62 |
[data-testid="stSidebar"]::-webkit-scrollbar {
|
| 63 |
width: 6px;
|
|
@@ -66,6 +86,7 @@ footer {
|
|
| 66 |
background: #2d3748;
|
| 67 |
border-radius: 3px;
|
| 68 |
}
|
|
|
|
| 69 |
/* 4. FIXED HEADER STYLING */
|
| 70 |
.fixed-header {
|
| 71 |
position: fixed;
|
|
@@ -81,6 +102,7 @@ footer {
|
|
| 81 |
align-items: center;
|
| 82 |
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
|
| 83 |
}
|
|
|
|
| 84 |
/* 5. MAIN CONTENT SCROLLING (INDEPENDENT RIGHT PANEL SCROLL) */
|
| 85 |
.main .block-container {
|
| 86 |
margin-top: 6rem;
|
|
@@ -96,6 +118,7 @@ footer {
|
|
| 96 |
background: #2d3748;
|
| 97 |
border-radius: 4px;
|
| 98 |
}
|
|
|
|
| 99 |
/* 6. SIDEBAR BUTTON STYLING */
|
| 100 |
[data-testid="stSidebar"] .stButton button {
|
| 101 |
width: 100%;
|
|
@@ -103,6 +126,7 @@ footer {
|
|
| 103 |
font-weight: 600;
|
| 104 |
margin-bottom: 6px;
|
| 105 |
}
|
|
|
|
| 106 |
/* 7. HIDE UPLOADED FILE LIST & NAME */
|
| 107 |
[data-testid='stFileUploaderFile'] {
|
| 108 |
display: none;
|
|
@@ -113,6 +137,7 @@ section[data-testid="stFileUploader"] ul {
|
|
| 113 |
section[data-testid="stFileUploader"] small {
|
| 114 |
display: none;
|
| 115 |
}
|
|
|
|
| 116 |
/* 8. CHAT BUBBLES */
|
| 117 |
.chat-user {
|
| 118 |
background: #2d3748;
|
|
@@ -132,10 +157,7 @@ section[data-testid="stFileUploader"] small {
|
|
| 132 |
text-align: left;
|
| 133 |
color: #ffffff;
|
| 134 |
}
|
| 135 |
-
|
| 136 |
-
.sources {
|
| 137 |
-
display: none;
|
| 138 |
-
}
|
| 139 |
/* 9. TITLE TEXT */
|
| 140 |
.title-text {
|
| 141 |
font-size: 2.5rem;
|
|
@@ -152,177 +174,15 @@ section[data-testid="stFileUploader"] small {
|
|
| 152 |
color: #4da6ff;
|
| 153 |
text-decoration: none;
|
| 154 |
}
|
|
|
|
| 155 |
/* 10. INPUT FORM STYLING */
|
| 156 |
[data-testid="stForm"] {
|
| 157 |
border: none;
|
| 158 |
padding: 0;
|
| 159 |
}
|
| 160 |
-
/* 11. EXPAND SIDEBAR BUTTON */
|
| 161 |
-
.expand-sidebar-btn {
|
| 162 |
-
position: fixed;
|
| 163 |
-
top: 1rem;
|
| 164 |
-
left: 1rem;
|
| 165 |
-
z-index: 999999;
|
| 166 |
-
background: #1e3a8a;
|
| 167 |
-
color: white;
|
| 168 |
-
border: none;
|
| 169 |
-
border-radius: 50%;
|
| 170 |
-
width: 50px;
|
| 171 |
-
height: 50px;
|
| 172 |
-
font-size: 1.5rem;
|
| 173 |
-
cursor: pointer;
|
| 174 |
-
display: none;
|
| 175 |
-
align-items: center;
|
| 176 |
-
justify-content: center;
|
| 177 |
-
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
|
| 178 |
-
transition: all 0.3s ease;
|
| 179 |
-
opacity: 0.9;
|
| 180 |
-
}
|
| 181 |
-
|
| 182 |
-
.expand-sidebar-btn:hover {
|
| 183 |
-
background: #2563eb;
|
| 184 |
-
transform: scale(1.1);
|
| 185 |
-
opacity: 1;
|
| 186 |
-
}
|
| 187 |
-
|
| 188 |
-
/* 12. SIDEBAR CLOSE BUTTON CUSTOMIZATION */
|
| 189 |
-
[data-testid="stSidebar"] [data-testid="baseButton-headerNoPadding"] {
|
| 190 |
-
position: absolute;
|
| 191 |
-
top: 0.75rem;
|
| 192 |
-
right: 0.75rem;
|
| 193 |
-
z-index: 100000;
|
| 194 |
-
background: transparent;
|
| 195 |
-
color: #ccc;
|
| 196 |
-
border: none;
|
| 197 |
-
font-size: 1.2rem;
|
| 198 |
-
padding: 4px 8px;
|
| 199 |
-
border-radius: 4px;
|
| 200 |
-
}
|
| 201 |
-
|
| 202 |
-
[data-testid="stSidebar"] [data-testid="baseButton-headerNoPadding"]:hover {
|
| 203 |
-
background: rgba(255, 255, 255, 0.1);
|
| 204 |
-
color: white;
|
| 205 |
-
}
|
| 206 |
</style>
|
| 207 |
""", unsafe_allow_html=True)
|
| 208 |
|
| 209 |
-
# ---------------- SIDEBAR EXPAND BUTTON JAVASCRIPT ----------------
|
| 210 |
-
st.markdown("""
|
| 211 |
-
<div id="expandSidebarBtn" class="expand-sidebar-btn" title="Expand Sidebar">☰</div>
|
| 212 |
-
|
| 213 |
-
<script>
|
| 214 |
-
// Function to check if sidebar is collapsed
|
| 215 |
-
function isSidebarCollapsed() {
|
| 216 |
-
const sidebar = document.querySelector('[data-testid="stSidebar"]');
|
| 217 |
-
if (!sidebar) return true;
|
| 218 |
-
|
| 219 |
-
// Check various indicators of collapsed sidebar
|
| 220 |
-
const computedStyle = window.getComputedStyle(sidebar);
|
| 221 |
-
const isHidden = computedStyle.transform === 'matrix(1, 0, 0, 1, -320, 0)' ||
|
| 222 |
-
computedStyle.display === 'none' ||
|
| 223 |
-
computedStyle.visibility === 'hidden' ||
|
| 224 |
-
sidebar.offsetWidth === 0 ||
|
| 225 |
-
sidebar.style.transform === 'translateX(-320px)' ||
|
| 226 |
-
sidebar.style.transform === 'translateX(-100%)';
|
| 227 |
-
|
| 228 |
-
return isHidden;
|
| 229 |
-
}
|
| 230 |
-
|
| 231 |
-
// Function to expand the sidebar
|
| 232 |
-
function expandSidebar() {
|
| 233 |
-
const sidebar = document.querySelector('[data-testid="stSidebar"]');
|
| 234 |
-
const expandBtn = document.getElementById('expandSidebarBtn');
|
| 235 |
-
|
| 236 |
-
if (sidebar) {
|
| 237 |
-
// Remove any hiding transforms
|
| 238 |
-
sidebar.style.transform = 'translateX(0)';
|
| 239 |
-
sidebar.style.display = 'block';
|
| 240 |
-
sidebar.style.visibility = 'visible';
|
| 241 |
-
|
| 242 |
-
// Trigger Streamlit's internal sidebar show mechanism
|
| 243 |
-
const event = new Event('sidebarStateChange');
|
| 244 |
-
window.dispatchEvent(event);
|
| 245 |
-
|
| 246 |
-
// Hide the expand button
|
| 247 |
-
if (expandBtn) {
|
| 248 |
-
expandBtn.style.display = 'none';
|
| 249 |
-
}
|
| 250 |
-
|
| 251 |
-
console.log('Sidebar expanded');
|
| 252 |
-
}
|
| 253 |
-
}
|
| 254 |
-
|
| 255 |
-
// Function to update expand button visibility
|
| 256 |
-
function updateExpandButton() {
|
| 257 |
-
const expandBtn = document.getElementById('expandSidebarBtn');
|
| 258 |
-
if (!expandBtn) return;
|
| 259 |
-
|
| 260 |
-
if (isSidebarCollapsed()) {
|
| 261 |
-
expandBtn.style.display = 'flex';
|
| 262 |
-
} else {
|
| 263 |
-
expandBtn.style.display = 'none';
|
| 264 |
-
}
|
| 265 |
-
}
|
| 266 |
-
|
| 267 |
-
// Initialize when DOM is loaded
|
| 268 |
-
document.addEventListener('DOMContentLoaded', function() {
|
| 269 |
-
// Create expand button if it doesn't exist
|
| 270 |
-
if (!document.getElementById('expandSidebarBtn')) {
|
| 271 |
-
const btn = document.createElement('div');
|
| 272 |
-
btn.id = 'expandSidebarBtn';
|
| 273 |
-
btn.className = 'expand-sidebar-btn';
|
| 274 |
-
btn.title = 'Expand Sidebar';
|
| 275 |
-
btn.innerHTML = '☰';
|
| 276 |
-
btn.addEventListener('click', expandSidebar);
|
| 277 |
-
document.body.appendChild(btn);
|
| 278 |
-
}
|
| 279 |
-
|
| 280 |
-
// Add click event to existing button
|
| 281 |
-
const expandBtn = document.getElementById('expandSidebarBtn');
|
| 282 |
-
if (expandBtn) {
|
| 283 |
-
expandBtn.addEventListener('click', expandSidebar);
|
| 284 |
-
}
|
| 285 |
-
|
| 286 |
-
// Initial check
|
| 287 |
-
updateExpandButton();
|
| 288 |
-
|
| 289 |
-
// Set up interval to check sidebar state
|
| 290 |
-
setInterval(updateExpandButton, 500);
|
| 291 |
-
|
| 292 |
-
// Also check on window resize and scroll
|
| 293 |
-
window.addEventListener('resize', updateExpandButton);
|
| 294 |
-
window.addEventListener('scroll', updateExpandButton);
|
| 295 |
-
|
| 296 |
-
// Observe DOM changes for sidebar state
|
| 297 |
-
const observer = new MutationObserver(function(mutations) {
|
| 298 |
-
mutations.forEach(function(mutation) {
|
| 299 |
-
if (mutation.type === 'attributes' &&
|
| 300 |
-
mutation.target.hasAttribute('data-testid') &&
|
| 301 |
-
mutation.target.getAttribute('data-testid') === 'stSidebar') {
|
| 302 |
-
updateExpandButton();
|
| 303 |
-
}
|
| 304 |
-
});
|
| 305 |
-
});
|
| 306 |
-
|
| 307 |
-
const sidebar = document.querySelector('[data-testid="stSidebar"]');
|
| 308 |
-
if (sidebar) {
|
| 309 |
-
observer.observe(sidebar, { attributes: true });
|
| 310 |
-
}
|
| 311 |
-
});
|
| 312 |
-
|
| 313 |
-
// Override Streamlit's sidebar close behavior to show our button
|
| 314 |
-
document.addEventListener('click', function(e) {
|
| 315 |
-
// Check if click is on sidebar close button
|
| 316 |
-
if (e.target.closest('[data-testid="baseButton-headerNoPadding"]')) {
|
| 317 |
-
// Show our expand button after a short delay
|
| 318 |
-
setTimeout(() => {
|
| 319 |
-
updateExpandButton();
|
| 320 |
-
}, 100);
|
| 321 |
-
}
|
| 322 |
-
});
|
| 323 |
-
</script>
|
| 324 |
-
""", unsafe_allow_html=True)
|
| 325 |
-
|
| 326 |
# ---------------- FIXED HEADER ----------------
|
| 327 |
st.markdown("""
|
| 328 |
<div class="fixed-header">
|
|
@@ -448,6 +308,7 @@ with st.sidebar:
|
|
| 448 |
chunks = process_pdf(uploaded)
|
| 449 |
if chunks:
|
| 450 |
st.session_state.uploaded_file_name = uploaded.name
|
|
|
|
| 451 |
st.success("✅ PDF Processed!")
|
| 452 |
else:
|
| 453 |
st.error("❌ Failed.")
|
|
@@ -477,7 +338,6 @@ if submit_btn and user_question:
|
|
| 477 |
with st.spinner("Thinking..."):
|
| 478 |
answer, sources, error = ask_question(user_question)
|
| 479 |
if answer:
|
| 480 |
-
# CHANGED: Removed the 'Context Chunks Used' HTML from the message
|
| 481 |
bot_msg = answer
|
| 482 |
st.session_state.chat.append(("bot", bot_msg))
|
| 483 |
else:
|
|
|
|
| 41 |
height: 100%;
|
| 42 |
margin: 0;
|
| 43 |
}
|
| 44 |
+
|
| 45 |
+
/* 2. HEADER MODIFICATIONS (The Fix) */
|
| 46 |
+
/* Instead of hiding the header completely, make it transparent and click-through */
|
| 47 |
header[data-testid="stHeader"] {
|
| 48 |
+
background: transparent;
|
| 49 |
+
pointer-events: none;
|
| 50 |
+
}
|
| 51 |
+
/* Hide specific header elements we don't want */
|
| 52 |
+
[data-testid="stToolbar"],
|
| 53 |
+
[data-testid="stStatusWidget"],
|
| 54 |
+
[data-testid="stDecoration"] {
|
| 55 |
display: none;
|
| 56 |
}
|
| 57 |
+
/* Force the sidebar expand button (arrow) to be visible, clickable, and on top */
|
| 58 |
+
[data-testid="stSidebarCollapsedControl"] {
|
| 59 |
+
display: block !important;
|
| 60 |
+
pointer-events: auto !important;
|
| 61 |
+
z-index: 100000 !important; /* Higher than your custom header */
|
| 62 |
+
color: white !important;
|
| 63 |
+
background-color: #0e1117; /* Match header background */
|
| 64 |
+
border-radius: 4px;
|
| 65 |
+
margin-top: 4px;
|
| 66 |
+
margin-left: 4px;
|
| 67 |
+
}
|
| 68 |
footer {
|
| 69 |
display: none;
|
| 70 |
}
|
| 71 |
+
|
| 72 |
/* 3. SIDEBAR STYLING (INDEPENDENT LEFT PANEL SCROLL) */
|
| 73 |
[data-testid="stSidebar"] {
|
| 74 |
position: fixed;
|
|
|
|
| 78 |
width: 20rem;
|
| 79 |
overflow-y: auto !important;
|
| 80 |
z-index: 99999;
|
|
|
|
| 81 |
}
|
| 82 |
[data-testid="stSidebar"]::-webkit-scrollbar {
|
| 83 |
width: 6px;
|
|
|
|
| 86 |
background: #2d3748;
|
| 87 |
border-radius: 3px;
|
| 88 |
}
|
| 89 |
+
|
| 90 |
/* 4. FIXED HEADER STYLING */
|
| 91 |
.fixed-header {
|
| 92 |
position: fixed;
|
|
|
|
| 102 |
align-items: center;
|
| 103 |
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
|
| 104 |
}
|
| 105 |
+
|
| 106 |
/* 5. MAIN CONTENT SCROLLING (INDEPENDENT RIGHT PANEL SCROLL) */
|
| 107 |
.main .block-container {
|
| 108 |
margin-top: 6rem;
|
|
|
|
| 118 |
background: #2d3748;
|
| 119 |
border-radius: 4px;
|
| 120 |
}
|
| 121 |
+
|
| 122 |
/* 6. SIDEBAR BUTTON STYLING */
|
| 123 |
[data-testid="stSidebar"] .stButton button {
|
| 124 |
width: 100%;
|
|
|
|
| 126 |
font-weight: 600;
|
| 127 |
margin-bottom: 6px;
|
| 128 |
}
|
| 129 |
+
|
| 130 |
/* 7. HIDE UPLOADED FILE LIST & NAME */
|
| 131 |
[data-testid='stFileUploaderFile'] {
|
| 132 |
display: none;
|
|
|
|
| 137 |
section[data-testid="stFileUploader"] small {
|
| 138 |
display: none;
|
| 139 |
}
|
| 140 |
+
|
| 141 |
/* 8. CHAT BUBBLES */
|
| 142 |
.chat-user {
|
| 143 |
background: #2d3748;
|
|
|
|
| 157 |
text-align: left;
|
| 158 |
color: #ffffff;
|
| 159 |
}
|
| 160 |
+
|
|
|
|
|
|
|
|
|
|
| 161 |
/* 9. TITLE TEXT */
|
| 162 |
.title-text {
|
| 163 |
font-size: 2.5rem;
|
|
|
|
| 174 |
color: #4da6ff;
|
| 175 |
text-decoration: none;
|
| 176 |
}
|
| 177 |
+
|
| 178 |
/* 10. INPUT FORM STYLING */
|
| 179 |
[data-testid="stForm"] {
|
| 180 |
border: none;
|
| 181 |
padding: 0;
|
| 182 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
</style>
|
| 184 |
""", unsafe_allow_html=True)
|
| 185 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
# ---------------- FIXED HEADER ----------------
|
| 187 |
st.markdown("""
|
| 188 |
<div class="fixed-header">
|
|
|
|
| 308 |
chunks = process_pdf(uploaded)
|
| 309 |
if chunks:
|
| 310 |
st.session_state.uploaded_file_name = uploaded.name
|
| 311 |
+
st.session_state.chat = [] # Clear chat on new upload
|
| 312 |
st.success("✅ PDF Processed!")
|
| 313 |
else:
|
| 314 |
st.error("❌ Failed.")
|
|
|
|
| 338 |
with st.spinner("Thinking..."):
|
| 339 |
answer, sources, error = ask_question(user_question)
|
| 340 |
if answer:
|
|
|
|
| 341 |
bot_msg = answer
|
| 342 |
st.session_state.chat.append(("bot", bot_msg))
|
| 343 |
else:
|