Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -122,6 +122,35 @@ def load_document_store():
|
|
122 |
else:
|
123 |
print("Error: knowledgebase.txt not found!")
|
124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
|
126 |
# Function to upload document
|
127 |
def upload_document(file_path, embed_model):
|
@@ -222,7 +251,8 @@ def handle_upload():
|
|
222 |
|
223 |
# Process the document using the upload_document function
|
224 |
try:
|
225 |
-
|
|
|
226 |
except Exception as e:
|
227 |
return jsonify({"error": f"Error processing file: {e}"}), 503
|
228 |
|
|
|
122 |
else:
|
123 |
print("Error: knowledgebase.txt not found!")
|
124 |
|
125 |
+
|
126 |
+
def load_document_store_once(file_path):
|
127 |
+
"""Loads knowledgebase.txt into a dictionary where FAISS IDs map to text and embeddings"""
|
128 |
+
global document_store
|
129 |
+
document_store = {} # Reset document store
|
130 |
+
all_texts = []
|
131 |
+
file_location = os.path.join(UPLOAD_DIR, os.path.basename(file_path))
|
132 |
+
if os.path.exists(file_location):
|
133 |
+
with open(file_location, "r", encoding="utf-8") as f:
|
134 |
+
lines = f.readlines()
|
135 |
+
|
136 |
+
for i, line in enumerate(lines):
|
137 |
+
text = line.strip()
|
138 |
+
if text:
|
139 |
+
document_store[i] = {"text": text} # Store text mapped to FAISS ID
|
140 |
+
all_texts.append(text) # Collect all texts for embedding
|
141 |
+
|
142 |
+
print(f"Loaded {len(document_store)} documents into document_store.")
|
143 |
+
else:
|
144 |
+
print("Error: knowledgebase.txt not found!")
|
145 |
+
|
146 |
+
# Generate embeddings for all documents
|
147 |
+
embeddings = bertmodel.encode(all_texts)
|
148 |
+
embeddings = embeddings.astype("float32")
|
149 |
+
|
150 |
+
# Add embeddings to FAISS index
|
151 |
+
index.add_with_ids(embeddings, np.array(list(document_store.keys()), dtype=np.int64))
|
152 |
+
print(f"Added {len(all_texts)} document embeddings to FAISS index.")
|
153 |
+
|
154 |
|
155 |
# Function to upload document
|
156 |
def upload_document(file_path, embed_model):
|
|
|
251 |
|
252 |
# Process the document using the upload_document function
|
253 |
try:
|
254 |
+
load_document_store_once(file_path)
|
255 |
+
# upload_document(file_path, bertmodel) # Assuming 'bertmodel' is defined elsewhere
|
256 |
except Exception as e:
|
257 |
return jsonify({"error": f"Error processing file: {e}"}), 503
|
258 |
|