dev2607 commited on
Commit
bb518df
·
verified ·
1 Parent(s): 87f066f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -40
app.py CHANGED
@@ -203,7 +203,7 @@ class PDFQAAssistant:
203
  Dictionary with the answer and source documents
204
  """
205
  if self.conversation_chain is None:
206
- return {"answer": "Please load a document first before asking questions."}
207
 
208
  try:
209
  result = self.conversation_chain({"question": question})
@@ -224,7 +224,7 @@ class PDFQAAssistant:
224
 
225
  except Exception as e:
226
  st.error(f"Error processing question: {e}")
227
- return {"answer": f"Error processing your question: {e}"}
228
 
229
  def clear_memory(self) -> None:
230
  """Clear the conversation memory."""
@@ -255,14 +255,23 @@ def main():
255
  with st.sidebar:
256
  st.header("Settings")
257
 
258
- # Option to use HF token from environment or manual entry
259
- use_env_token = st.checkbox("Use HF_TOKEN from environment", value=True)
260
-
261
- if use_env_token:
262
- hf_token = os.environ.get("HF_TOKEN", None)
263
- if not hf_token:
264
- st.warning("HF_TOKEN not found in environment variables.")
265
  else:
 
 
 
 
 
 
 
 
 
266
  hf_token = st.text_input("Enter Hugging Face API Token:", type="password")
267
 
268
  # Model selection
@@ -291,21 +300,27 @@ def main():
291
  if uploaded_files:
292
  process_btn = st.button("Process Documents")
293
  if process_btn:
294
- # Initialize the assistant
295
- assistant = PDFQAAssistant(
296
- hf_token=hf_token,
297
- model_name=model_name,
298
- embedding_model_name=embedding_model
299
- )
300
-
301
- # Process each uploaded file
302
- for pdf_file in uploaded_files:
303
- file_name = pdf_file.name
304
- st.session_state.file_names.append(file_name)
305
- assistant.process_pdf(pdf_file, file_name)
306
-
307
- # Store the assistant in session state
308
- st.session_state.assistant = assistant
 
 
 
 
 
 
309
 
310
  # Document management
311
  if st.session_state.document_processed:
@@ -352,7 +367,7 @@ def main():
352
  st.chat_message("user").write(message["content"])
353
  else:
354
  st.chat_message("assistant").write(message["content"])
355
- if "sources" in message:
356
  with st.expander("View Sources"):
357
  for source in message["sources"]:
358
  st.write(f"- {source['source']} (chunk {source['chunk']})")
@@ -371,21 +386,25 @@ def main():
371
  # Get the answer
372
  with st.chat_message("assistant"):
373
  with st.spinner("Thinking..."):
374
- result = st.session_state.assistant.ask(question)
375
- st.write(result["answer"])
376
-
377
- # Show sources if available
378
- if result["sources"]:
379
- with st.expander("View Sources"):
380
- for source in result["sources"]:
381
- st.write(f"- {source['source']} (chunk {source['chunk']})")
382
-
383
- # Add assistant response to chat history
384
- st.session_state.chat_history.append({
385
- "role": "assistant",
386
- "content": result["answer"],
387
- "sources": result["sources"]
388
- })
 
 
 
 
389
 
390
  if __name__ == "__main__":
391
  main()
 
203
  Dictionary with the answer and source documents
204
  """
205
  if self.conversation_chain is None:
206
+ return {"answer": "Please load a document first before asking questions.", "sources": []}
207
 
208
  try:
209
  result = self.conversation_chain({"question": question})
 
224
 
225
  except Exception as e:
226
  st.error(f"Error processing question: {e}")
227
+ return {"answer": f"Error processing your question: {e}", "sources": []}
228
 
229
  def clear_memory(self) -> None:
230
  """Clear the conversation memory."""
 
255
  with st.sidebar:
256
  st.header("Settings")
257
 
258
+ # Get HF_TOKEN from secrets or environment
259
+ if "HF_TOKEN" in st.secrets:
260
+ hf_token = st.secrets["HF_TOKEN"]
261
+ token_source = "Using HF_TOKEN from app secrets"
262
+ elif os.environ.get("HF_TOKEN"):
263
+ hf_token = os.environ.get("HF_TOKEN")
264
+ token_source = "Using HF_TOKEN from environment variables"
265
  else:
266
+ hf_token = None
267
+ token_source = "No HF_TOKEN found"
268
+
269
+ st.info(token_source)
270
+
271
+ # Option to manually enter token if needed
272
+ use_manual_token = st.checkbox("Enter token manually", value=not hf_token)
273
+
274
+ if use_manual_token:
275
  hf_token = st.text_input("Enter Hugging Face API Token:", type="password")
276
 
277
  # Model selection
 
300
  if uploaded_files:
301
  process_btn = st.button("Process Documents")
302
  if process_btn:
303
+ if not hf_token:
304
+ st.error("Please provide a valid Hugging Face API token.")
305
+ else:
306
+ # Initialize the assistant
307
+ try:
308
+ assistant = PDFQAAssistant(
309
+ hf_token=hf_token,
310
+ model_name=model_name,
311
+ embedding_model_name=embedding_model
312
+ )
313
+
314
+ # Process each uploaded file
315
+ for pdf_file in uploaded_files:
316
+ file_name = pdf_file.name
317
+ st.session_state.file_names.append(file_name)
318
+ assistant.process_pdf(pdf_file, file_name)
319
+
320
+ # Store the assistant in session state
321
+ st.session_state.assistant = assistant
322
+ except Exception as e:
323
+ st.error(f"Error initializing assistant: {e}")
324
 
325
  # Document management
326
  if st.session_state.document_processed:
 
367
  st.chat_message("user").write(message["content"])
368
  else:
369
  st.chat_message("assistant").write(message["content"])
370
+ if message.get("sources"): # Use .get() with default to avoid KeyError
371
  with st.expander("View Sources"):
372
  for source in message["sources"]:
373
  st.write(f"- {source['source']} (chunk {source['chunk']})")
 
386
  # Get the answer
387
  with st.chat_message("assistant"):
388
  with st.spinner("Thinking..."):
389
+ try:
390
+ result = st.session_state.assistant.ask(question)
391
+
392
+ st.write(result["answer"])
393
+
394
+ # Show sources if available
395
+ if result.get("sources"): # Use .get() with default to avoid KeyError
396
+ with st.expander("View Sources"):
397
+ for source in result["sources"]:
398
+ st.write(f"- {source['source']} (chunk {source['chunk']})")
399
+
400
+ # Add assistant response to chat history
401
+ st.session_state.chat_history.append({
402
+ "role": "assistant",
403
+ "content": result["answer"],
404
+ "sources": result.get("sources", []) # Use .get() with default to avoid KeyError
405
+ })
406
+ except Exception as e:
407
+ st.error(f"Error getting response: {e}")
408
 
409
  if __name__ == "__main__":
410
  main()