absiitr commited on
Commit
3055cf6
·
verified ·
1 Parent(s): f9960c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -229
app.py CHANGED
@@ -2,7 +2,6 @@ import os
2
  import tempfile
3
  import gc
4
  import logging
5
- import time
6
  import streamlit as st
7
  from groq import Groq, APIError
8
  from langchain_community.document_loaders import PyPDFLoader
@@ -33,167 +32,24 @@ else:
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;
181
  height: 100%;
182
  margin: 0;
183
- }}
184
  /* 2. HIDE DEFAULT STREAMLIT ELEMENTS */
185
- header[data-testid="stHeader"] {{
186
  display: none;
187
- }}
188
- footer {{
189
  display: none;
190
- }}
191
  /* 3. SIDEBAR STYLING (INDEPENDENT LEFT PANEL SCROLL) */
192
- [data-testid="stSidebar"] > div {{
193
- height: 100vh;
194
- overflow-y: auto !important;
195
- }}
196
- [data-testid="stSidebar"] {{
197
  position: fixed;
198
  top: 0;
199
  left: 0;
@@ -201,16 +57,16 @@ footer {{
201
  width: 20rem;
202
  overflow-y: auto !important;
203
  z-index: 99999;
204
- }}
205
- [data-testid="stSidebar"]::-webkit-scrollbar {{
206
  width: 6px;
207
- }}
208
- [data-testid="stSidebar"]::-webkit-scrollbar-thumb {{
209
  background: #2d3748;
210
  border-radius: 3px;
211
- }}
212
  /* 4. FIXED HEADER STYLING */
213
- .fixed-header {{
214
  position: fixed;
215
  top: 0;
216
  left: 0;
@@ -223,41 +79,41 @@ footer {{
223
  justify-content: center;
224
  align-items: center;
225
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
226
- }}
227
  /* 5. MAIN CONTENT SCROLLING (INDEPENDENT RIGHT PANEL SCROLL) */
228
- .main .block-container {{
229
  margin-top: 6rem;
230
  height: calc(100vh - 6rem);
231
  overflow-y: auto;
232
  padding-top: 1rem;
233
  padding-bottom: 5rem;
234
- }}
235
- .main .block-container::-webkit-scrollbar {{
236
  width: 8px;
237
- }}
238
- .main .block-container::-webkit-scrollbar-thumb {{
239
  background: #2d3748;
240
  border-radius: 4px;
241
- }}
242
  /* 6. SIDEBAR BUTTON STYLING */
243
- [data-testid="stSidebar"] .stButton button {{
244
  width: 100%;
245
  border-radius: 8px;
246
  font-weight: 600;
247
  margin-bottom: 6px;
248
- }}
249
  /* 7. HIDE UPLOADED FILE LIST & NAME */
250
- [data-testid='stFileUploaderFile'] {{
251
  display: none;
252
- }}
253
- section[data-testid="stFileUploader"] ul {{
254
  display: none;
255
- }}
256
- section[data-testid="stFileUploader"] small {{
257
  display: none;
258
- }}
259
  /* 8. CHAT BUBBLES */
260
- .chat-user {{
261
  background: #2d3748;
262
  padding: 12px;
263
  border-radius: 10px 10px 2px 10px;
@@ -265,8 +121,8 @@ section[data-testid="stFileUploader"] small {{
265
  max-width: 85%;
266
  text-align: right;
267
  color: #f0f2f6;
268
- }}
269
- .chat-bot {{
270
  background: #1e3a8a;
271
  padding: 12px;
272
  border-radius: 10px 10px 10px 2px;
@@ -274,32 +130,32 @@ section[data-testid="stFileUploader"] small {{
274
  max-width: 85%;
275
  text-align: left;
276
  color: #ffffff;
277
- }}
278
  /* Sources CSS removed/hidden as it is no longer used */
279
- .sources {{
280
  display: none;
281
- }}
282
  /* 9. TITLE TEXT */
283
- .title-text {{
284
  font-size: 2.5rem;
285
  font-weight: 800;
286
  margin: 0;
287
  line-height: 1.2;
288
- }}
289
- .creator-text {{
290
  font-size: 1rem;
291
  font-weight: 500;
292
  color: #cccccc;
293
- }}
294
- .creator-text a {{
295
  color: #4da6ff;
296
  text-decoration: none;
297
- }}
298
  /* 10. INPUT FORM STYLING */
299
- [data-testid="stForm"] {{
300
  border: none;
301
  padding: 0;
302
- }}
303
  </style>
304
  """, unsafe_allow_html=True)
305
 
@@ -471,44 +327,4 @@ if st.session_state.chat:
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)
 
2
  import tempfile
3
  import gc
4
  import logging
 
5
  import streamlit as st
6
  from groq import Groq, APIError
7
  from langchain_community.document_loaders import PyPDFLoader
 
32
  # ---------------- STREAMLIT UI SETUP ----------------
33
  st.set_page_config(page_title="PDF Assistant", page_icon="📘", layout="wide")
34
 
35
+ # ---------------- CSS ----------------
 
36
  st.markdown("""
37
+ <style>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  /* 1. GLOBAL RESET & SCROLL LOCK */
39
+ html, body {
40
  overflow: hidden;
41
  height: 100%;
42
  margin: 0;
43
+ }
44
  /* 2. HIDE DEFAULT STREAMLIT ELEMENTS */
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;
54
  top: 0;
55
  left: 0;
 
57
  width: 20rem;
58
  overflow-y: auto !important;
59
  z-index: 99999;
60
+ }
61
+ [data-testid="stSidebar"]::-webkit-scrollbar {
62
  width: 6px;
63
+ }
64
+ [data-testid="stSidebar"]::-webkit-scrollbar-thumb {
65
  background: #2d3748;
66
  border-radius: 3px;
67
+ }
68
  /* 4. FIXED HEADER STYLING */
69
+ .fixed-header {
70
  position: fixed;
71
  top: 0;
72
  left: 0;
 
79
  justify-content: center;
80
  align-items: center;
81
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
82
+ }
83
  /* 5. MAIN CONTENT SCROLLING (INDEPENDENT RIGHT PANEL SCROLL) */
84
+ .main .block-container {
85
  margin-top: 6rem;
86
  height: calc(100vh - 6rem);
87
  overflow-y: auto;
88
  padding-top: 1rem;
89
  padding-bottom: 5rem;
90
+ }
91
+ .main .block-container::-webkit-scrollbar {
92
  width: 8px;
93
+ }
94
+ .main .block-container::-webkit-scrollbar-thumb {
95
  background: #2d3748;
96
  border-radius: 4px;
97
+ }
98
  /* 6. SIDEBAR BUTTON STYLING */
99
+ [data-testid="stSidebar"] .stButton button {
100
  width: 100%;
101
  border-radius: 8px;
102
  font-weight: 600;
103
  margin-bottom: 6px;
104
+ }
105
  /* 7. HIDE UPLOADED FILE LIST & NAME */
106
+ [data-testid='stFileUploaderFile'] {
107
  display: none;
108
+ }
109
+ section[data-testid="stFileUploader"] ul {
110
  display: none;
111
+ }
112
+ section[data-testid="stFileUploader"] small {
113
  display: none;
114
+ }
115
  /* 8. CHAT BUBBLES */
116
+ .chat-user {
117
  background: #2d3748;
118
  padding: 12px;
119
  border-radius: 10px 10px 2px 10px;
 
121
  max-width: 85%;
122
  text-align: right;
123
  color: #f0f2f6;
124
+ }
125
+ .chat-bot {
126
  background: #1e3a8a;
127
  padding: 12px;
128
  border-radius: 10px 10px 10px 2px;
 
130
  max-width: 85%;
131
  text-align: left;
132
  color: #ffffff;
133
+ }
134
  /* Sources CSS removed/hidden as it is no longer used */
135
+ .sources {
136
  display: none;
137
+ }
138
  /* 9. TITLE TEXT */
139
+ .title-text {
140
  font-size: 2.5rem;
141
  font-weight: 800;
142
  margin: 0;
143
  line-height: 1.2;
144
+ }
145
+ .creator-text {
146
  font-size: 1rem;
147
  font-weight: 500;
148
  color: #cccccc;
149
+ }
150
+ .creator-text a {
151
  color: #4da6ff;
152
  text-decoration: none;
153
+ }
154
  /* 10. INPUT FORM STYLING */
155
+ [data-testid="stForm"] {
156
  border: none;
157
  padding: 0;
158
+ }
159
  </style>
160
  """, unsafe_allow_html=True)
161
 
 
327
  if role == "user":
328
  st.markdown(f"<div class='chat-user'>{msg}</div>", unsafe_allow_html=True)
329
  else:
330
+ st.markdown(f"<div class='chat-bot'>{msg}</div>", unsafe_allow_html=True)