Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -66,19 +66,7 @@ def load_document(file: NamedTemporaryFile, parser: str = "llamaparse") -> List[
|
|
66 |
def get_embeddings():
|
67 |
return HuggingFaceEmbeddings(model_name="sentence-transformers/stsb-roberta-large")
|
68 |
|
69 |
-
def
|
70 |
-
with open("uploaded_documents.json", "w") as f:
|
71 |
-
json.dump(uploaded_documents, f)
|
72 |
-
|
73 |
-
def load_uploaded_documents():
|
74 |
-
global uploaded_documents
|
75 |
-
if os.path.exists("uploaded_documents.json"):
|
76 |
-
with open("uploaded_documents.json", "r") as f:
|
77 |
-
uploaded_documents = json.load(f)
|
78 |
-
else:
|
79 |
-
uploaded_documents = []
|
80 |
-
|
81 |
-
def update_vectors(files: List[NamedTemporaryFile], parser: str):
|
82 |
global uploaded_documents
|
83 |
logging.info(f"Entering update_vectors with {len(files)} files and parser: {parser}")
|
84 |
|
@@ -94,7 +82,6 @@ def update_vectors(files: List[NamedTemporaryFile], parser: str):
|
|
94 |
total_chunks = 0
|
95 |
|
96 |
all_data = []
|
97 |
-
new_documents = []
|
98 |
for file in files:
|
99 |
logging.info(f"Processing file: {file.name}")
|
100 |
try:
|
@@ -102,22 +89,15 @@ def update_vectors(files: List[NamedTemporaryFile], parser: str):
|
|
102 |
logging.info(f"Loaded {len(data)} chunks from {file.name}")
|
103 |
all_data.extend(data)
|
104 |
total_chunks += len(data)
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
if existing_doc:
|
109 |
-
logging.info(f"Document already exists in uploaded_documents: {file.name}")
|
110 |
-
existing_doc["selected"] = True # Ensure it's selected
|
111 |
-
else:
|
112 |
-
new_doc = {"name": file.name, "selected": True}
|
113 |
-
new_documents.append(new_doc)
|
114 |
logging.info(f"Added new document to uploaded_documents: {file.name}")
|
|
|
|
|
115 |
except Exception as e:
|
116 |
logging.error(f"Error processing file {file.name}: {str(e)}")
|
117 |
|
118 |
-
# Add new documents to the list
|
119 |
-
uploaded_documents.extend(new_documents)
|
120 |
-
|
121 |
logging.info(f"Total chunks processed: {total_chunks}")
|
122 |
|
123 |
if os.path.exists("faiss_database"):
|
@@ -131,10 +111,6 @@ def update_vectors(files: List[NamedTemporaryFile], parser: str):
|
|
131 |
database.save_local("faiss_database")
|
132 |
logging.info("FAISS database saved")
|
133 |
|
134 |
-
# Save the updated uploaded_documents list
|
135 |
-
save_uploaded_documents()
|
136 |
-
logging.info("Uploaded documents list saved")
|
137 |
-
|
138 |
return f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files using {parser}.", gr.CheckboxGroup(
|
139 |
choices=[doc["name"] for doc in uploaded_documents],
|
140 |
value=[doc["name"] for doc in uploaded_documents if doc["selected"]],
|
@@ -516,8 +492,6 @@ use_web_search = gr.Checkbox(label="Use Web Search", value=True)
|
|
516 |
|
517 |
custom_placeholder = "Ask a question (Note: You can toggle between Web Search and PDF Chat in Additional Inputs below)"
|
518 |
|
519 |
-
load_uploaded_documents()
|
520 |
-
|
521 |
demo = gr.ChatInterface(
|
522 |
respond,
|
523 |
additional_inputs=[
|
|
|
66 |
def get_embeddings():
|
67 |
return HuggingFaceEmbeddings(model_name="sentence-transformers/stsb-roberta-large")
|
68 |
|
69 |
+
def update_vectors(files, parser):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
global uploaded_documents
|
71 |
logging.info(f"Entering update_vectors with {len(files)} files and parser: {parser}")
|
72 |
|
|
|
82 |
total_chunks = 0
|
83 |
|
84 |
all_data = []
|
|
|
85 |
for file in files:
|
86 |
logging.info(f"Processing file: {file.name}")
|
87 |
try:
|
|
|
89 |
logging.info(f"Loaded {len(data)} chunks from {file.name}")
|
90 |
all_data.extend(data)
|
91 |
total_chunks += len(data)
|
92 |
+
# Append new documents instead of replacing
|
93 |
+
if not any(doc["name"] == file.name for doc in uploaded_documents):
|
94 |
+
uploaded_documents.append({"name": file.name, "selected": True})
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
logging.info(f"Added new document to uploaded_documents: {file.name}")
|
96 |
+
else:
|
97 |
+
logging.info(f"Document already exists in uploaded_documents: {file.name}")
|
98 |
except Exception as e:
|
99 |
logging.error(f"Error processing file {file.name}: {str(e)}")
|
100 |
|
|
|
|
|
|
|
101 |
logging.info(f"Total chunks processed: {total_chunks}")
|
102 |
|
103 |
if os.path.exists("faiss_database"):
|
|
|
111 |
database.save_local("faiss_database")
|
112 |
logging.info("FAISS database saved")
|
113 |
|
|
|
|
|
|
|
|
|
114 |
return f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files using {parser}.", gr.CheckboxGroup(
|
115 |
choices=[doc["name"] for doc in uploaded_documents],
|
116 |
value=[doc["name"] for doc in uploaded_documents if doc["selected"]],
|
|
|
492 |
|
493 |
custom_placeholder = "Ask a question (Note: You can toggle between Web Search and PDF Chat in Additional Inputs below)"
|
494 |
|
|
|
|
|
495 |
demo = gr.ChatInterface(
|
496 |
respond,
|
497 |
additional_inputs=[
|