Shreyas094 commited on
Commit
fde3f64
·
verified ·
1 Parent(s): f8b2943

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -24
app.py CHANGED
@@ -68,19 +68,17 @@ def get_embeddings():
68
  def update_vectors(files, parser):
69
  global uploaded_documents
70
  if not files:
71
- return "Please upload at least one PDF file.", []
72
 
73
  embed = get_embeddings()
74
  total_chunks = 0
75
 
76
- new_documents = []
77
  for file in files:
78
  data = load_document(file, parser)
79
  all_data.extend(data)
80
  total_chunks += len(data)
81
- new_documents.append({"name": file.name, "selected": True})
82
-
83
- uploaded_documents.extend(new_documents)
84
 
85
  if os.path.exists("faiss_database"):
86
  database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
@@ -90,8 +88,7 @@ def update_vectors(files, parser):
90
 
91
  database.save_local("faiss_database")
92
 
93
- status_message = f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files using {parser}."
94
- return status_message, [doc["name"] for doc in uploaded_documents]
95
 
96
  def generate_chunked_response(prompt, model, max_tokens=1000, num_calls=3, temperature=0.2, should_stop=False):
97
  print(f"Starting generate_chunked_response with {num_calls} calls")
@@ -413,10 +410,10 @@ css = """
413
 
414
  uploaded_documents = []
415
 
416
- def display_documents(document_list):
417
  return gr.CheckboxGroup(
418
- choices=document_list,
419
- value=document_list,
420
  label="Select documents to query"
421
  )
422
 
@@ -426,13 +423,14 @@ document_selector = gr.CheckboxGroup(label="Select documents to query")
426
  use_web_search = gr.Checkbox(label="Use Web Search", value=False)
427
 
428
  demo = gr.ChatInterface(
429
- respond,
430
- additional_inputs=[
431
- gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[0]),
432
- gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
433
- gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),
434
- use_web_search
435
- ],
 
436
  title="AI-powered Web Search and PDF Chat Assistant",
437
  description="Chat with your PDFs or use web search to answer questions.",
438
  theme=gr.themes.Soft(
@@ -471,16 +469,13 @@ with demo:
471
  file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])
472
  parser_dropdown = gr.Dropdown(choices=["pypdf", "llamaparse"], label="Select PDF Parser", value="llamaparse")
473
  update_button = gr.Button("Upload Document")
474
- document_selector = gr.CheckboxGroup(label="Select documents to query")
475
 
476
  update_output = gr.Textbox(label="Update Status")
477
- update_button.click(
478
- update_vectors,
479
- inputs=[file_input, parser_dropdown],
480
- outputs=[update_output, document_selector]
481
- )
482
 
483
- gr.Markdown(
484
  """
485
  ## How to use
486
  1. Upload PDF documents using the file input at the top.
 
68
  def update_vectors(files, parser):
69
  global uploaded_documents
70
  if not files:
71
+ return "Please upload at least one PDF file."
72
 
73
  embed = get_embeddings()
74
  total_chunks = 0
75
 
76
+ all_data = []
77
  for file in files:
78
  data = load_document(file, parser)
79
  all_data.extend(data)
80
  total_chunks += len(data)
81
+ uploaded_documents.append({"name": file.name, "selected": True})
 
 
82
 
83
  if os.path.exists("faiss_database"):
84
  database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
 
88
 
89
  database.save_local("faiss_database")
90
 
91
+ return f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files using {parser}."
 
92
 
93
  def generate_chunked_response(prompt, model, max_tokens=1000, num_calls=3, temperature=0.2, should_stop=False):
94
  print(f"Starting generate_chunked_response with {num_calls} calls")
 
410
 
411
  uploaded_documents = []
412
 
413
+ def display_documents():
414
  return gr.CheckboxGroup(
415
+ choices=[doc["name"] for doc in uploaded_documents],
416
+ value=[doc["name"] for doc in uploaded_documents if doc["selected"]],
417
  label="Select documents to query"
418
  )
419
 
 
423
  use_web_search = gr.Checkbox(label="Use Web Search", value=False)
424
 
425
  demo = gr.ChatInterface(
426
+ respond,
427
+ additional_inputs=[
428
+ gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[0]),
429
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
430
+ gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),
431
+ use_web_search,
432
+ document_selector # Add this line
433
+ ],
434
  title="AI-powered Web Search and PDF Chat Assistant",
435
  description="Chat with your PDFs or use web search to answer questions.",
436
  theme=gr.themes.Soft(
 
469
  file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])
470
  parser_dropdown = gr.Dropdown(choices=["pypdf", "llamaparse"], label="Select PDF Parser", value="llamaparse")
471
  update_button = gr.Button("Upload Document")
472
+ document_selector
473
 
474
  update_output = gr.Textbox(label="Update Status")
475
+ update_button.click(update_vectors, inputs=[file_input, parser_dropdown], outputs=update_output)
476
+ update_button.click(display_documents, outputs=document_selector)
 
 
 
477
 
478
+ gr.Markdown(
479
  """
480
  ## How to use
481
  1. Upload PDF documents using the file input at the top.