Spaces:
Paused
Paused
Shreyas094
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -128,6 +128,34 @@ def update_vectors(files, parser):
|
|
128 |
|
129 |
return f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files using {parser}.", display_documents()
|
130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
def generate_chunked_response(prompt, model, max_tokens=10000, num_calls=3, temperature=0.2, should_stop=False):
|
132 |
print(f"Starting generate_chunked_response with {num_calls} calls")
|
133 |
full_response = ""
|
@@ -484,7 +512,7 @@ def display_documents():
|
|
484 |
return gr.CheckboxGroup(
|
485 |
choices=[doc["name"] for doc in uploaded_documents],
|
486 |
value=[doc["name"] for doc in uploaded_documents if doc["selected"]],
|
487 |
-
label="Select documents to query"
|
488 |
)
|
489 |
|
490 |
def initial_conversation():
|
@@ -558,7 +586,7 @@ demo = gr.ChatInterface(
|
|
558 |
|
559 |
# Add file upload functionality
|
560 |
with demo:
|
561 |
-
gr.Markdown("## Upload PDF Documents")
|
562 |
|
563 |
with gr.Row():
|
564 |
file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])
|
@@ -567,6 +595,8 @@ with demo:
|
|
567 |
refresh_button = gr.Button("Refresh Document List")
|
568 |
|
569 |
update_output = gr.Textbox(label="Update Status")
|
|
|
|
|
570 |
|
571 |
# Update both the output text and the document selector
|
572 |
update_button.click(update_vectors,
|
@@ -577,6 +607,11 @@ with demo:
|
|
577 |
refresh_button.click(refresh_documents,
|
578 |
inputs=[],
|
579 |
outputs=[document_selector])
|
|
|
|
|
|
|
|
|
|
|
580 |
|
581 |
gr.Markdown(
|
582 |
"""
|
|
|
128 |
|
129 |
return f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files using {parser}.", display_documents()
|
130 |
|
131 |
+
def delete_documents(selected_docs):
|
132 |
+
global uploaded_documents
|
133 |
+
|
134 |
+
if not selected_docs:
|
135 |
+
return "No documents selected for deletion.", display_documents()
|
136 |
+
|
137 |
+
embed = get_embeddings()
|
138 |
+
database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
|
139 |
+
|
140 |
+
deleted_docs = []
|
141 |
+
for doc_name in selected_docs:
|
142 |
+
# Remove from uploaded_documents list
|
143 |
+
uploaded_documents = [doc for doc in uploaded_documents if doc["name"] != doc_name]
|
144 |
+
|
145 |
+
# Remove from FAISS database
|
146 |
+
docs_to_keep = [doc for doc in database.docstore._dict.values() if doc.metadata["source"] != doc_name]
|
147 |
+
database = FAISS.from_documents(docs_to_keep, embed)
|
148 |
+
|
149 |
+
deleted_docs.append(doc_name)
|
150 |
+
|
151 |
+
# Save updated FAISS database
|
152 |
+
database.save_local("faiss_database")
|
153 |
+
|
154 |
+
# Save updated uploaded_documents list
|
155 |
+
save_documents(uploaded_documents)
|
156 |
+
|
157 |
+
return f"Deleted documents: {', '.join(deleted_docs)}", display_documents()
|
158 |
+
|
159 |
def generate_chunked_response(prompt, model, max_tokens=10000, num_calls=3, temperature=0.2, should_stop=False):
|
160 |
print(f"Starting generate_chunked_response with {num_calls} calls")
|
161 |
full_response = ""
|
|
|
512 |
return gr.CheckboxGroup(
|
513 |
choices=[doc["name"] for doc in uploaded_documents],
|
514 |
value=[doc["name"] for doc in uploaded_documents if doc["selected"]],
|
515 |
+
label="Select documents to query or delete"
|
516 |
)
|
517 |
|
518 |
def initial_conversation():
|
|
|
586 |
|
587 |
# Add file upload functionality
|
588 |
with demo:
|
589 |
+
gr.Markdown("## Upload and Manage PDF Documents")
|
590 |
|
591 |
with gr.Row():
|
592 |
file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])
|
|
|
595 |
refresh_button = gr.Button("Refresh Document List")
|
596 |
|
597 |
update_output = gr.Textbox(label="Update Status")
|
598 |
+
document_selector = display_documents()
|
599 |
+
delete_button = gr.Button("Delete Selected Documents")
|
600 |
|
601 |
# Update both the output text and the document selector
|
602 |
update_button.click(update_vectors,
|
|
|
607 |
refresh_button.click(refresh_documents,
|
608 |
inputs=[],
|
609 |
outputs=[document_selector])
|
610 |
+
|
611 |
+
# Add the delete button functionality
|
612 |
+
delete_button.click(delete_documents,
|
613 |
+
inputs=[document_selector],
|
614 |
+
outputs=[update_output, document_selector])
|
615 |
|
616 |
gr.Markdown(
|
617 |
"""
|