absiitr commited on
Commit
748fa2f
Β·
verified Β·
1 Parent(s): 58ee917

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -19
app.py CHANGED
@@ -30,7 +30,13 @@ else:
30
  st.warning("⚠️ GROQ_API_KEY not found. Please add it to Hugging Face secrets.")
31
 
32
  # ---------------- STREAMLIT UI SETUP ----------------
33
- st.set_page_config(page_title="PDF Assistant", page_icon="πŸ“˜", layout="wide")
 
 
 
 
 
 
34
 
35
  # ---------------- CSS ----------------
36
  st.markdown("""
@@ -41,13 +47,26 @@ html, body {
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;
@@ -65,6 +84,7 @@ footer {
65
  background: #2d3748;
66
  border-radius: 3px;
67
  }
 
68
  /* 4. FIXED HEADER STYLING */
69
  .fixed-header {
70
  position: fixed;
@@ -80,6 +100,7 @@ footer {
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;
@@ -95,6 +116,7 @@ footer {
95
  background: #2d3748;
96
  border-radius: 4px;
97
  }
 
98
  /* 6. SIDEBAR BUTTON STYLING */
99
  [data-testid="stSidebar"] .stButton button {
100
  width: 100%;
@@ -102,6 +124,7 @@ footer {
102
  font-weight: 600;
103
  margin-bottom: 6px;
104
  }
 
105
  /* 7. HIDE UPLOADED FILE LIST & NAME */
106
  [data-testid='stFileUploaderFile'] {
107
  display: none;
@@ -112,6 +135,7 @@ section[data-testid="stFileUploader"] ul {
112
  section[data-testid="stFileUploader"] small {
113
  display: none;
114
  }
 
115
  /* 8. CHAT BUBBLES */
116
  .chat-user {
117
  background: #2d3748;
@@ -131,10 +155,10 @@ section[data-testid="stFileUploader"] small {
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;
@@ -151,6 +175,7 @@ section[data-testid="stFileUploader"] small {
151
  color: #4da6ff;
152
  text-decoration: none;
153
  }
 
154
  /* 10. INPUT FORM STYLING */
155
  [data-testid="stForm"] {
156
  border: none;
@@ -169,20 +194,15 @@ st.markdown("""
169
  </div>
170
  """, unsafe_allow_html=True)
171
 
172
-
173
  # ---------------- SESSION STATE ----------------
174
  if "chat" not in st.session_state:
175
  st.session_state.chat = []
176
-
177
  if "vectorstore" not in st.session_state:
178
  st.session_state.vectorstore = None
179
-
180
  if "retriever" not in st.session_state:
181
  st.session_state.retriever = None
182
-
183
  if "uploaded_file_name" not in st.session_state:
184
  st.session_state.uploaded_file_name = None
185
-
186
  if "uploader_key" not in st.session_state:
187
  st.session_state.uploader_key = 0
188
 
@@ -241,13 +261,14 @@ def ask_question(question):
241
  docs = st.session_state.retriever.invoke(question)
242
  context = "\n\n".join(d.page_content for d in docs)
243
 
244
- prompt = f"""
245
- You are a strict RAG Q&A assistant.
246
- Summarize the context based on user question and return best answer. If the answer is not found, reply: "I cannot find this in the PDF."
247
- CONTEXT = {context}
 
248
  QUESTION = {question}
249
- Answer on your behalf, don't say based on the context...
250
- """
251
 
252
  response = client.chat.completions.create(
253
  model=GROQ_MODEL,
@@ -266,16 +287,17 @@ with st.sidebar:
266
  st.write("")
267
  if st.button("πŸ—‘οΈ Clear Chat History", use_container_width=True):
268
  clear_chat_history()
269
- if st.button("πŸ”₯ Clear PDF Memory", on_click=clear_memory, use_container_width=True):
270
- st.success("Memory Cleared!")
271
-
 
272
  st.markdown("---")
273
 
274
  upload_label = "βœ… PDF Uploaded!" if st.session_state.uploaded_file_name else "Upload PDF"
275
  uploaded = st.file_uploader(
276
  upload_label, type=["pdf"], key=st.session_state.uploader_key, label_visibility="collapsed"
277
  )
278
-
279
  if uploaded:
280
  if uploaded.name != st.session_state.uploaded_file_name:
281
  st.session_state.uploaded_file_name = None
@@ -313,7 +335,6 @@ if submit_btn and user_question:
313
  with st.spinner("Thinking..."):
314
  answer, sources, error = ask_question(user_question)
315
  if answer:
316
- # CHANGED: Removed the 'Context Chunks Used' HTML from the message
317
  bot_msg = answer
318
  st.session_state.chat.append(("bot", bot_msg))
319
  else:
 
30
  st.warning("⚠️ GROQ_API_KEY not found. Please add it to Hugging Face secrets.")
31
 
32
  # ---------------- STREAMLIT UI SETUP ----------------
33
+ # CHANGED: Added initial_sidebar_state="expanded" to ensure it starts open
34
+ st.set_page_config(
35
+ page_title="PDF Assistant",
36
+ page_icon="πŸ“˜",
37
+ layout="wide",
38
+ initial_sidebar_state="expanded"
39
+ )
40
 
41
  # ---------------- CSS ----------------
42
  st.markdown("""
 
47
  height: 100%;
48
  margin: 0;
49
  }
50
+
51
+ /* 2. HIDE DEFAULT STREAMLIT ELEMENTS & SIDEBAR TOGGLES */
52
  header[data-testid="stHeader"] {
53
  display: none;
54
  }
55
  footer {
56
  display: none;
57
  }
58
+
59
+ /* --- NEW CODE START: HIDE SIDEBAR TOGGLE BUTTONS --- */
60
+ /* Hide the 'Close Sidebar' (<<) button inside the sidebar */
61
+ section[data-testid="stSidebar"] > div > div:first-child {
62
+ display: none;
63
+ }
64
+ /* Hide the 'Open Sidebar' (>) button on main screen */
65
+ [data-testid="collapsedControl"] {
66
+ display: none;
67
+ }
68
+ /* --- NEW CODE END --- */
69
+
70
  /* 3. SIDEBAR STYLING (INDEPENDENT LEFT PANEL SCROLL) */
71
  [data-testid="stSidebar"] {
72
  position: fixed;
 
84
  background: #2d3748;
85
  border-radius: 3px;
86
  }
87
+
88
  /* 4. FIXED HEADER STYLING */
89
  .fixed-header {
90
  position: fixed;
 
100
  align-items: center;
101
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
102
  }
103
+
104
  /* 5. MAIN CONTENT SCROLLING (INDEPENDENT RIGHT PANEL SCROLL) */
105
  .main .block-container {
106
  margin-top: 6rem;
 
116
  background: #2d3748;
117
  border-radius: 4px;
118
  }
119
+
120
  /* 6. SIDEBAR BUTTON STYLING */
121
  [data-testid="stSidebar"] .stButton button {
122
  width: 100%;
 
124
  font-weight: 600;
125
  margin-bottom: 6px;
126
  }
127
+
128
  /* 7. HIDE UPLOADED FILE LIST & NAME */
129
  [data-testid='stFileUploaderFile'] {
130
  display: none;
 
135
  section[data-testid="stFileUploader"] small {
136
  display: none;
137
  }
138
+
139
  /* 8. CHAT BUBBLES */
140
  .chat-user {
141
  background: #2d3748;
 
155
  text-align: left;
156
  color: #ffffff;
157
  }
 
158
  .sources {
159
  display: none;
160
  }
161
+
162
  /* 9. TITLE TEXT */
163
  .title-text {
164
  font-size: 2.5rem;
 
175
  color: #4da6ff;
176
  text-decoration: none;
177
  }
178
+
179
  /* 10. INPUT FORM STYLING */
180
  [data-testid="stForm"] {
181
  border: none;
 
194
  </div>
195
  """, unsafe_allow_html=True)
196
 
 
197
  # ---------------- SESSION STATE ----------------
198
  if "chat" not in st.session_state:
199
  st.session_state.chat = []
 
200
  if "vectorstore" not in st.session_state:
201
  st.session_state.vectorstore = None
 
202
  if "retriever" not in st.session_state:
203
  st.session_state.retriever = None
 
204
  if "uploaded_file_name" not in st.session_state:
205
  st.session_state.uploaded_file_name = None
 
206
  if "uploader_key" not in st.session_state:
207
  st.session_state.uploader_key = 0
208
 
 
261
  docs = st.session_state.retriever.invoke(question)
262
  context = "\n\n".join(d.page_content for d in docs)
263
 
264
+ prompt = f"""You are a strict RAG Q&A assistant.
265
+ Summarize the context based on user question and return best answer.
266
+ If the answer is not found, reply: "I cannot find this in the PDF."
267
+
268
+ CONTEXT = {context}
269
  QUESTION = {question}
270
+
271
+ Answer on your behalf, don't say based on the context..."""
272
 
273
  response = client.chat.completions.create(
274
  model=GROQ_MODEL,
 
287
  st.write("")
288
  if st.button("πŸ—‘οΈ Clear Chat History", use_container_width=True):
289
  clear_chat_history()
290
+
291
+ if st.button("πŸ”₯ Clear PDF Memory", on_click=clear_memory, use_container_width=True):
292
+ st.success("Memory Cleared!")
293
+
294
  st.markdown("---")
295
 
296
  upload_label = "βœ… PDF Uploaded!" if st.session_state.uploaded_file_name else "Upload PDF"
297
  uploaded = st.file_uploader(
298
  upload_label, type=["pdf"], key=st.session_state.uploader_key, label_visibility="collapsed"
299
  )
300
+
301
  if uploaded:
302
  if uploaded.name != st.session_state.uploaded_file_name:
303
  st.session_state.uploaded_file_name = None
 
335
  with st.spinner("Thinking..."):
336
  answer, sources, error = ask_question(user_question)
337
  if answer:
 
338
  bot_msg = answer
339
  st.session_state.chat.append(("bot", bot_msg))
340
  else: