Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import os
|
|
| 2 |
import tempfile
|
| 3 |
import gc
|
| 4 |
import logging
|
| 5 |
-
import hashlib
|
| 6 |
import time
|
| 7 |
import streamlit as st
|
| 8 |
from groq import Groq, APIError
|
|
@@ -34,13 +33,148 @@ else:
|
|
| 34 |
# ---------------- STREAMLIT UI SETUP ----------------
|
| 35 |
st.set_page_config(page_title="PDF Assistant", page_icon="📘", layout="wide")
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
# ---------------- CSS ----------------
|
| 41 |
st.markdown(f"""
|
| 42 |
-
<style data-
|
| 43 |
-
/* CSS
|
| 44 |
/* 1. GLOBAL RESET & SCROLL LOCK */
|
| 45 |
html, body {{
|
| 46 |
overflow: hidden;
|
|
@@ -67,23 +201,6 @@ footer {{
|
|
| 67 |
width: 20rem;
|
| 68 |
overflow-y: auto !important;
|
| 69 |
z-index: 99999;
|
| 70 |
-
|
| 71 |
-
/* AGGRESSIVE SIDEBAR PROTECTION - Prevents caching issues */
|
| 72 |
-
display: block !important;
|
| 73 |
-
visibility: visible !important;
|
| 74 |
-
opacity: 1 !important;
|
| 75 |
-
transform: translateX(0) !important;
|
| 76 |
-
}}
|
| 77 |
-
/* Override any hidden sidebar states */
|
| 78 |
-
[data-testid="stSidebar"][style*="display: none"],
|
| 79 |
-
[data-testid="stSidebar"][style*="visibility: hidden"],
|
| 80 |
-
[data-testid="stSidebar"][style*="opacity: 0"],
|
| 81 |
-
[data-testid="stSidebar"][style*="transform: translateX(-100%)"] {{
|
| 82 |
-
display: block !important;
|
| 83 |
-
visibility: visible !important;
|
| 84 |
-
opacity: 1 !important;
|
| 85 |
-
transform: translateX(0) !important;
|
| 86 |
-
width: 20rem !important;
|
| 87 |
}}
|
| 88 |
[data-testid="stSidebar"]::-webkit-scrollbar {{
|
| 89 |
width: 6px;
|
|
@@ -186,54 +303,6 @@ section[data-testid="stFileUploader"] small {{
|
|
| 186 |
</style>
|
| 187 |
""", unsafe_allow_html=True)
|
| 188 |
|
| 189 |
-
# Add JavaScript to prevent caching and ensure sidebar visibility
|
| 190 |
-
st.markdown("""
|
| 191 |
-
<script>
|
| 192 |
-
// Prevent CSS caching and ensure sidebar is always visible
|
| 193 |
-
(function() {
|
| 194 |
-
// 1. Add cache control meta tags
|
| 195 |
-
const meta = document.createElement('meta');
|
| 196 |
-
meta.httpEquiv = "Cache-Control";
|
| 197 |
-
meta.content = "no-cache, no-store, must-revalidate";
|
| 198 |
-
document.head.appendChild(meta);
|
| 199 |
-
|
| 200 |
-
const meta2 = document.createElement('meta');
|
| 201 |
-
meta2.httpEquiv = "Pragma";
|
| 202 |
-
meta2.content = "no-cache";
|
| 203 |
-
document.head.appendChild(meta2);
|
| 204 |
-
|
| 205 |
-
const meta3 = document.createElement('meta');
|
| 206 |
-
meta3.httpEquiv = "Expires";
|
| 207 |
-
meta3.content = "0";
|
| 208 |
-
document.head.appendChild(meta3);
|
| 209 |
-
|
| 210 |
-
// 2. Force sidebar visibility on load
|
| 211 |
-
function ensureSidebarVisible() {
|
| 212 |
-
const sidebar = document.querySelector('[data-testid="stSidebar"]');
|
| 213 |
-
if (sidebar) {
|
| 214 |
-
sidebar.style.display = 'block';
|
| 215 |
-
sidebar.style.visibility = 'visible';
|
| 216 |
-
sidebar.style.opacity = '1';
|
| 217 |
-
sidebar.style.transform = 'translateX(0)';
|
| 218 |
-
sidebar.style.width = '20rem';
|
| 219 |
-
}
|
| 220 |
-
}
|
| 221 |
-
|
| 222 |
-
// Run on multiple events
|
| 223 |
-
document.addEventListener('DOMContentLoaded', ensureSidebarVisible);
|
| 224 |
-
window.addEventListener('load', ensureSidebarVisible);
|
| 225 |
-
|
| 226 |
-
// Run immediately if already loaded
|
| 227 |
-
if (document.readyState !== 'loading') {
|
| 228 |
-
ensureSidebarVisible();
|
| 229 |
-
}
|
| 230 |
-
|
| 231 |
-
// 3. Periodically check sidebar (in case Streamlit tries to hide it)
|
| 232 |
-
setInterval(ensureSidebarVisible, 1000);
|
| 233 |
-
})();
|
| 234 |
-
</script>
|
| 235 |
-
""", unsafe_allow_html=True)
|
| 236 |
-
|
| 237 |
# ---------------- FIXED HEADER ----------------
|
| 238 |
st.markdown("""
|
| 239 |
<div class="fixed-header">
|
|
@@ -402,4 +471,44 @@ if st.session_state.chat:
|
|
| 402 |
if role == "user":
|
| 403 |
st.markdown(f"<div class='chat-user'>{msg}</div>", unsafe_allow_html=True)
|
| 404 |
else:
|
| 405 |
-
st.markdown(f"<div class='chat-bot'>{msg}</div>", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import tempfile
|
| 3 |
import gc
|
| 4 |
import logging
|
|
|
|
| 5 |
import time
|
| 6 |
import streamlit as st
|
| 7 |
from groq import Groq, APIError
|
|
|
|
| 33 |
# ---------------- STREAMLIT UI SETUP ----------------
|
| 34 |
st.set_page_config(page_title="PDF Assistant", page_icon="📘", layout="wide")
|
| 35 |
|
| 36 |
+
# ---------------- AGGRESSIVE SIDEBAR FIX ----------------
|
| 37 |
+
# This JavaScript MUST come BEFORE any CSS to prevent caching
|
| 38 |
+
st.markdown("""
|
| 39 |
+
<script>
|
| 40 |
+
// NUCLEAR OPTION: Force sidebar to always be visible
|
| 41 |
+
(function() {
|
| 42 |
+
// 1. OVERRIDE STREAMLIT'S SIDEBAR HIDING
|
| 43 |
+
const originalSetAttribute = Element.prototype.setAttribute;
|
| 44 |
+
Element.prototype.setAttribute = function(name, value) {
|
| 45 |
+
if (name === 'style' && this.getAttribute('data-testid') === 'stSidebar') {
|
| 46 |
+
// Block any attempts to hide the sidebar
|
| 47 |
+
value = value.replace(/display:\\s*none/g, 'display: block')
|
| 48 |
+
.replace(/visibility:\\s*hidden/g, 'visibility: visible')
|
| 49 |
+
.replace(/opacity:\\s*0/g, 'opacity: 1')
|
| 50 |
+
.replace(/transform:[^;]*/g, 'transform: translateX(0)');
|
| 51 |
+
}
|
| 52 |
+
return originalSetAttribute.call(this, name, value);
|
| 53 |
+
};
|
| 54 |
+
|
| 55 |
+
// 2. FORCE SIDEBAR VISIBLE ON LOAD
|
| 56 |
+
function forceSidebar() {
|
| 57 |
+
const sidebar = document.querySelector('[data-testid="stSidebar"]');
|
| 58 |
+
if (sidebar) {
|
| 59 |
+
// Apply inline styles that override any CSS
|
| 60 |
+
sidebar.style.cssText = `
|
| 61 |
+
display: block !important;
|
| 62 |
+
visibility: visible !important;
|
| 63 |
+
opacity: 1 !important;
|
| 64 |
+
transform: translateX(0) !important;
|
| 65 |
+
position: fixed !important;
|
| 66 |
+
top: 0 !important;
|
| 67 |
+
left: 0 !important;
|
| 68 |
+
width: 20rem !important;
|
| 69 |
+
height: 100vh !important;
|
| 70 |
+
z-index: 99999 !important;
|
| 71 |
+
overflow-y: auto !important;
|
| 72 |
+
background-color: #0e1117 !important;
|
| 73 |
+
`;
|
| 74 |
+
|
| 75 |
+
// Force all children to be visible too
|
| 76 |
+
const children = sidebar.querySelectorAll('*');
|
| 77 |
+
children.forEach(child => {
|
| 78 |
+
child.style.display = 'block';
|
| 79 |
+
child.style.visibility = 'visible';
|
| 80 |
+
});
|
| 81 |
+
} else {
|
| 82 |
+
// Sidebar not found yet, check again
|
| 83 |
+
setTimeout(forceSidebar, 100);
|
| 84 |
+
}
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
// 3. RUN IMMEDIATELY AND REPEATEDLY
|
| 88 |
+
forceSidebar();
|
| 89 |
+
setInterval(forceSidebar, 500);
|
| 90 |
+
|
| 91 |
+
// 4. PREVENT BROWSER CACHING
|
| 92 |
+
const meta1 = document.createElement('meta');
|
| 93 |
+
meta1.httpEquiv = "Cache-Control";
|
| 94 |
+
meta1.content = "no-cache, no-store, must-revalidate";
|
| 95 |
+
document.head.prepend(meta1);
|
| 96 |
+
|
| 97 |
+
const meta2 = document.createElement('meta');
|
| 98 |
+
meta2.httpEquiv = "Pragma";
|
| 99 |
+
meta2.content = "no-cache";
|
| 100 |
+
document.head.prepend(meta2);
|
| 101 |
+
|
| 102 |
+
const meta3 = document.createElement('meta');
|
| 103 |
+
meta3.httpEquiv = "Expires";
|
| 104 |
+
meta3.content = "0";
|
| 105 |
+
document.head.prepend(meta3);
|
| 106 |
+
|
| 107 |
+
// 5. ADD HARD REFRESH REMINDER
|
| 108 |
+
if (performance && performance.navigation && performance.navigation.type === 1) {
|
| 109 |
+
// Page was reloaded, but if sidebar still broken, show message
|
| 110 |
+
setTimeout(() => {
|
| 111 |
+
const sidebar = document.querySelector('[data-testid="stSidebar"]');
|
| 112 |
+
if (!sidebar || sidebar.offsetWidth < 100) {
|
| 113 |
+
const msg = document.createElement('div');
|
| 114 |
+
msg.style.cssText = `
|
| 115 |
+
position: fixed;
|
| 116 |
+
top: 70px;
|
| 117 |
+
left: 10px;
|
| 118 |
+
background: #ff4444;
|
| 119 |
+
color: white;
|
| 120 |
+
padding: 10px;
|
| 121 |
+
border-radius: 5px;
|
| 122 |
+
z-index: 9999999;
|
| 123 |
+
font-size: 12px;
|
| 124 |
+
`;
|
| 125 |
+
msg.innerHTML = '⚠️ Press <b>Ctrl+F5</b> to fix sidebar';
|
| 126 |
+
document.body.appendChild(msg);
|
| 127 |
+
setTimeout(() => msg.remove(), 5000);
|
| 128 |
+
}
|
| 129 |
+
}, 1000);
|
| 130 |
+
}
|
| 131 |
+
})();
|
| 132 |
+
</script>
|
| 133 |
+
|
| 134 |
+
<style id="sidebar-override">
|
| 135 |
+
/* INLINE STYLES THAT CAN'T BE CACHED */
|
| 136 |
+
/* These will override any cached CSS */
|
| 137 |
+
[data-testid="stSidebar"] {
|
| 138 |
+
display: block !important;
|
| 139 |
+
visibility: visible !important;
|
| 140 |
+
opacity: 1 !important;
|
| 141 |
+
transform: translateX(0) !important;
|
| 142 |
+
position: fixed !important;
|
| 143 |
+
top: 0 !important;
|
| 144 |
+
left: 0 !important;
|
| 145 |
+
width: 20rem !important;
|
| 146 |
+
height: 100vh !important;
|
| 147 |
+
z-index: 99999 !important;
|
| 148 |
+
overflow-y: auto !important;
|
| 149 |
+
background-color: #0e1117 !important;
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
/* Block any hidden states */
|
| 153 |
+
[data-testid="stSidebar"][style*="display: none"] {
|
| 154 |
+
display: block !important;
|
| 155 |
+
}
|
| 156 |
+
[data-testid="stSidebar"][style*="visibility: hidden"] {
|
| 157 |
+
visibility: visible !important;
|
| 158 |
+
}
|
| 159 |
+
[data-testid="stSidebar"][style*="opacity: 0"] {
|
| 160 |
+
opacity: 1 !important;
|
| 161 |
+
}
|
| 162 |
+
[data-testid="stSidebar"][style*="width: 0"] {
|
| 163 |
+
width: 20rem !important;
|
| 164 |
+
}
|
| 165 |
+
[data-testid="stSidebar"][style*="transform: translateX(-100%)"] {
|
| 166 |
+
transform: translateX(0) !important;
|
| 167 |
+
}
|
| 168 |
+
</style>
|
| 169 |
+
""", unsafe_allow_html=True)
|
| 170 |
+
|
| 171 |
+
# ---------------- CSS (ORIGINAL, BUT WITH CACHE-BUSTING) ----------------
|
| 172 |
+
# Add timestamp to prevent CSS caching
|
| 173 |
+
css_timestamp = int(time.time() * 1000)
|
| 174 |
|
|
|
|
| 175 |
st.markdown(f"""
|
| 176 |
+
<style data-timestamp="{css_timestamp}">
|
| 177 |
+
/* CSS loaded at: {css_timestamp} */
|
| 178 |
/* 1. GLOBAL RESET & SCROLL LOCK */
|
| 179 |
html, body {{
|
| 180 |
overflow: hidden;
|
|
|
|
| 201 |
width: 20rem;
|
| 202 |
overflow-y: auto !important;
|
| 203 |
z-index: 99999;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
}}
|
| 205 |
[data-testid="stSidebar"]::-webkit-scrollbar {{
|
| 206 |
width: 6px;
|
|
|
|
| 303 |
</style>
|
| 304 |
""", unsafe_allow_html=True)
|
| 305 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
# ---------------- FIXED HEADER ----------------
|
| 307 |
st.markdown("""
|
| 308 |
<div class="fixed-header">
|
|
|
|
| 471 |
if role == "user":
|
| 472 |
st.markdown(f"<div class='chat-user'>{msg}</div>", unsafe_allow_html=True)
|
| 473 |
else:
|
| 474 |
+
st.markdown(f"<div class='chat-bot'>{msg}</div>", unsafe_allow_html=True)
|
| 475 |
+
|
| 476 |
+
# ---------------- FINAL NUCLEAR OPTION ----------------
|
| 477 |
+
# One more JavaScript layer that runs last
|
| 478 |
+
st.markdown("""
|
| 479 |
+
<script>
|
| 480 |
+
// Final safety check - runs after everything
|
| 481 |
+
setTimeout(() => {
|
| 482 |
+
const sidebar = document.querySelector('[data-testid="stSidebar"]');
|
| 483 |
+
if (!sidebar || sidebar.offsetWidth < 100) {
|
| 484 |
+
console.warn('Sidebar still hidden! Applying emergency fix...');
|
| 485 |
+
|
| 486 |
+
// Create emergency sidebar if needed
|
| 487 |
+
const existingEmergency = document.getElementById('emergency-sidebar');
|
| 488 |
+
if (!existingEmergency) {
|
| 489 |
+
const emergencyDiv = document.createElement('div');
|
| 490 |
+
emergencyDiv.id = 'emergency-sidebar';
|
| 491 |
+
emergencyDiv.style.cssText = `
|
| 492 |
+
position: fixed;
|
| 493 |
+
left: 0;
|
| 494 |
+
top: 0;
|
| 495 |
+
width: 20rem;
|
| 496 |
+
height: 100vh;
|
| 497 |
+
background: #0e1117;
|
| 498 |
+
z-index: 999999;
|
| 499 |
+
overflow-y: auto;
|
| 500 |
+
padding: 20px;
|
| 501 |
+
color: white;
|
| 502 |
+
`;
|
| 503 |
+
emergencyDiv.innerHTML = `
|
| 504 |
+
<h4>📘 PDF Assistant</h4>
|
| 505 |
+
<p><em>Sidebar recovery mode</em></p>
|
| 506 |
+
<hr>
|
| 507 |
+
<p>If buttons don't work, refresh page with <b>Ctrl+F5</b></p>
|
| 508 |
+
`;
|
| 509 |
+
document.body.appendChild(emergencyDiv);
|
| 510 |
+
}
|
| 511 |
+
}
|
| 512 |
+
}, 2000);
|
| 513 |
+
</script>
|
| 514 |
+
""", unsafe_allow_html=True)
|