Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -128,7 +128,9 @@ def build_knowledge_base(embeddings):
|
|
128 |
chunks = text_splitter.split_documents(documents)
|
129 |
|
130 |
vector_store = FAISS.from_documents(chunks, embeddings)
|
131 |
-
|
|
|
|
|
132 |
|
133 |
end_time = time.time()
|
134 |
build_time = end_time - start_time
|
@@ -179,6 +181,51 @@ def load_chat_history():
|
|
179 |
return []
|
180 |
return []
|
181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
182 |
# Main function
|
183 |
def main():
|
184 |
# Initialize models
|
@@ -278,7 +325,7 @@ Response Guidelines:
|
|
278 |
|
279 |
st.write(response)
|
280 |
|
281 |
-
#
|
282 |
chat_entry = {
|
283 |
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
284 |
"question": question,
|
@@ -286,8 +333,13 @@ Response Guidelines:
|
|
286 |
"context": context_text
|
287 |
}
|
288 |
|
|
|
|
|
|
|
|
|
|
|
|
|
289 |
st.session_state.chat_history.append(chat_entry)
|
290 |
-
save_chat_to_file(st.session_state.chat_history)
|
291 |
|
292 |
st.session_state.messages.append({
|
293 |
"question": question,
|
|
|
128 |
chunks = text_splitter.split_documents(documents)
|
129 |
|
130 |
vector_store = FAISS.from_documents(chunks, embeddings)
|
131 |
+
|
132 |
+
# Force save the vector store
|
133 |
+
force_save_vector_store(vector_store)
|
134 |
|
135 |
end_time = time.time()
|
136 |
build_time = end_time - start_time
|
|
|
181 |
return []
|
182 |
return []
|
183 |
|
184 |
+
def force_save_vector_store(vector_store):
|
185 |
+
"""Ensures vector store is properly saved to disk"""
|
186 |
+
try:
|
187 |
+
# Ensure directory exists
|
188 |
+
os.makedirs(VECTOR_STORE_PATH, exist_ok=True)
|
189 |
+
|
190 |
+
# Save vector store
|
191 |
+
vector_store.save_local(VECTOR_STORE_PATH)
|
192 |
+
|
193 |
+
# Verify files were created
|
194 |
+
if not os.path.exists(os.path.join(VECTOR_STORE_PATH, "index.faiss")):
|
195 |
+
raise Exception("Vector store files were not created")
|
196 |
+
|
197 |
+
st.sidebar.success("Vector store saved successfully")
|
198 |
+
except Exception as e:
|
199 |
+
st.sidebar.error(f"Failed to save vector store: {e}")
|
200 |
+
|
201 |
+
def force_save_chat_history(chat_entry):
|
202 |
+
"""Ensures chat history is properly saved to disk"""
|
203 |
+
try:
|
204 |
+
# Ensure directory exists
|
205 |
+
os.makedirs("chat_history", exist_ok=True)
|
206 |
+
|
207 |
+
current_date = datetime.now().strftime("%Y-%m-%d")
|
208 |
+
filename = f"chat_history/chat_history_{current_date}.json"
|
209 |
+
|
210 |
+
# Load existing history
|
211 |
+
existing_history = []
|
212 |
+
if os.path.exists(filename):
|
213 |
+
with open(filename, 'r', encoding='utf-8') as f:
|
214 |
+
existing_history = json.load(f)
|
215 |
+
|
216 |
+
# Add new entry
|
217 |
+
existing_history.append(chat_entry)
|
218 |
+
|
219 |
+
# Save updated history with fsync to ensure disk write
|
220 |
+
with open(filename, 'w', encoding='utf-8') as f:
|
221 |
+
json.dump(existing_history, f, ensure_ascii=False, indent=2)
|
222 |
+
f.flush()
|
223 |
+
os.fsync(f.fileno())
|
224 |
+
|
225 |
+
st.sidebar.success("Chat history saved successfully")
|
226 |
+
except Exception as e:
|
227 |
+
st.sidebar.error(f"Failed to save chat history: {e}")
|
228 |
+
|
229 |
# Main function
|
230 |
def main():
|
231 |
# Initialize models
|
|
|
325 |
|
326 |
st.write(response)
|
327 |
|
328 |
+
# Create chat entry
|
329 |
chat_entry = {
|
330 |
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
331 |
"question": question,
|
|
|
333 |
"context": context_text
|
334 |
}
|
335 |
|
336 |
+
# Force save chat history
|
337 |
+
force_save_chat_history(chat_entry)
|
338 |
+
|
339 |
+
# Update session state
|
340 |
+
if "chat_history" not in st.session_state:
|
341 |
+
st.session_state.chat_history = []
|
342 |
st.session_state.chat_history.append(chat_entry)
|
|
|
343 |
|
344 |
st.session_state.messages.append({
|
345 |
"question": question,
|