Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -98,6 +98,40 @@ index = initialize_faiss()
|
|
98 |
# Save FAISS index after modifications
|
99 |
save_faiss_index(index)
|
100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
# Function to upload document
|
102 |
def upload_document(file_path, embed_model):
|
103 |
try:
|
@@ -279,11 +313,16 @@ def generate_response(user_input, model_id):
|
|
279 |
|
280 |
# Append chat history
|
281 |
func_caller = []
|
282 |
-
|
283 |
query_vector = bertmodel.encode(user_input).reshape(1, -1).astype("float32")
|
284 |
D, I = index.search(query_vector, 1)
|
285 |
-
|
286 |
-
|
|
|
|
|
|
|
|
|
|
|
287 |
|
288 |
# Construct the knowledge prompt
|
289 |
prompt = f"Use the following knowledge:\n{retrieved_knowledge}"
|
|
|
98 |
# Save FAISS index after modifications
|
99 |
save_faiss_index(index)
|
100 |
|
101 |
+
# Load document store and populate FAISS index
|
102 |
+
knowledgebase_file = = os.path.join(UPLOAD_DIR, "knowledge_text.txt") # Ensure this path is correct
|
103 |
+
|
104 |
+
def load_document_store():
|
105 |
+
"""Loads knowledgebase.txt into a dictionary where FAISS IDs map to text and embeddings"""
|
106 |
+
global document_store
|
107 |
+
document_store = {} # Reset document store
|
108 |
+
all_texts = []
|
109 |
+
|
110 |
+
if os.path.exists(knowledgebase_file):
|
111 |
+
with open(knowledgebase_file, "r", encoding="utf-8") as f:
|
112 |
+
lines = f.readlines()
|
113 |
+
|
114 |
+
for i, line in enumerate(lines):
|
115 |
+
text = line.strip()
|
116 |
+
if text:
|
117 |
+
document_store[i] = {"text": text} # Store text mapped to FAISS ID
|
118 |
+
all_texts.append(text) # Collect all texts for embedding
|
119 |
+
|
120 |
+
print(f"Loaded {len(document_store)} documents into document_store.")
|
121 |
+
else:
|
122 |
+
print("Error: knowledgebase.txt not found!")
|
123 |
+
|
124 |
+
# Generate embeddings for all documents
|
125 |
+
embeddings = bertmodel.encode(all_texts)
|
126 |
+
embeddings = embeddings.astype("float32")
|
127 |
+
|
128 |
+
# Add embeddings to FAISS index
|
129 |
+
index.add_with_ids(embeddings, np.array(list(document_store.keys()), dtype=np.int64))
|
130 |
+
print(f"Added {len(all_texts)} document embeddings to FAISS index.")
|
131 |
+
|
132 |
+
# Load document store and index the documents
|
133 |
+
load_document_store()
|
134 |
+
|
135 |
# Function to upload document
|
136 |
def upload_document(file_path, embed_model):
|
137 |
try:
|
|
|
313 |
|
314 |
# Append chat history
|
315 |
func_caller = []
|
316 |
+
|
317 |
query_vector = bertmodel.encode(user_input).reshape(1, -1).astype("float32")
|
318 |
D, I = index.search(query_vector, 1)
|
319 |
+
|
320 |
+
# Retrieve document
|
321 |
+
retrieved_id = I[0][0]
|
322 |
+
retrieved_knowledge = (
|
323 |
+
document_store.get(retrieved_id, {}).get("text", "No relevant information found.")
|
324 |
+
if retrieved_id != -1 else "No relevant information found."
|
325 |
+
)
|
326 |
|
327 |
# Construct the knowledge prompt
|
328 |
prompt = f"Use the following knowledge:\n{retrieved_knowledge}"
|