Rulga commited on
Commit
61abcaa
·
1 Parent(s): cee780d

fix: update directory creation to use absolute paths for chat history and vector store

Browse files
Files changed (1) hide show
  1. app.py +18 -11
app.py CHANGED
@@ -14,11 +14,16 @@ import requests
14
  import json
15
  from datetime import datetime
16
 
17
- # Create required directories
18
- REQUIRED_DIRS = ['chat_history', 'vector_store']
19
- for dir_name in REQUIRED_DIRS:
20
- os.makedirs(dir_name, exist_ok=True)
21
- gitkeep_path = os.path.join(dir_name, '.gitkeep')
 
 
 
 
 
22
  if not os.path.exists(gitkeep_path):
23
  with open(gitkeep_path, 'w') as f:
24
  pass
@@ -129,7 +134,7 @@ def build_knowledge_base(embeddings):
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()
@@ -158,8 +163,9 @@ def build_knowledge_base(embeddings):
158
 
159
  # Function to save chat history
160
  def save_chat_to_file(chat_history):
 
161
  current_date = datetime.now().strftime("%Y-%m-%d")
162
- filename = f"chat_history/chat_history_{current_date}.json"
163
 
164
  try:
165
  with open(filename, 'w', encoding='utf-8') as f:
@@ -169,8 +175,9 @@ def save_chat_to_file(chat_history):
169
 
170
  # Function to load chat history
171
  def load_chat_history():
 
172
  current_date = datetime.now().strftime("%Y-%m-%d")
173
- filename = f"chat_history/chat_history_{current_date}.json"
174
 
175
  if os.path.exists(filename):
176
  try:
@@ -202,10 +209,10 @@ 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 = []
@@ -243,7 +250,7 @@ def main():
243
  st.rerun()
244
  except Exception as e:
245
  st.error(f"Error creating knowledge base: {e}")
246
- return # Exit if no knowledge base exists
247
 
248
  # Load existing knowledge base
249
  if 'vector_store' not in st.session_state:
 
14
  import json
15
  from datetime import datetime
16
 
17
+ # Define base directory and absolute paths
18
+ BASE_DIR = os.path.dirname(os.path.abspath(__file__))
19
+ VECTOR_STORE_PATH = os.path.join(BASE_DIR, "vector_store")
20
+ CHAT_HISTORY_DIR = os.path.join(BASE_DIR, "chat_history")
21
+
22
+ # Create required directories with absolute paths
23
+ REQUIRED_DIRS = [CHAT_HISTORY_DIR, VECTOR_STORE_PATH]
24
+ for dir_path in REQUIRED_DIRS:
25
+ os.makedirs(dir_path, exist_ok=True)
26
+ gitkeep_path = os.path.join(dir_path, '.gitkeep')
27
  if not os.path.exists(gitkeep_path):
28
  with open(gitkeep_path, 'w') as f:
29
  pass
 
134
 
135
  vector_store = FAISS.from_documents(chunks, embeddings)
136
 
137
+ # Force save the vector store using absolute path
138
  force_save_vector_store(vector_store)
139
 
140
  end_time = time.time()
 
163
 
164
  # Function to save chat history
165
  def save_chat_to_file(chat_history):
166
+ """Save chat history to file using absolute path"""
167
  current_date = datetime.now().strftime("%Y-%m-%d")
168
+ filename = os.path.join(CHAT_HISTORY_DIR, f"chat_history_{current_date}.json")
169
 
170
  try:
171
  with open(filename, 'w', encoding='utf-8') as f:
 
175
 
176
  # Function to load chat history
177
  def load_chat_history():
178
+ """Load chat history from file using absolute path"""
179
  current_date = datetime.now().strftime("%Y-%m-%d")
180
+ filename = os.path.join(CHAT_HISTORY_DIR, f"chat_history_{current_date}.json")
181
 
182
  if os.path.exists(filename):
183
  try:
 
209
  """Ensures chat history is properly saved to disk"""
210
  try:
211
  # Ensure directory exists
212
+ os.makedirs(CHAT_HISTORY_DIR, exist_ok=True)
213
 
214
  current_date = datetime.now().strftime("%Y-%m-%d")
215
+ filename = os.path.join(CHAT_HISTORY_DIR, f"chat_history_{current_date}.json")
216
 
217
  # Load existing history
218
  existing_history = []
 
250
  st.rerun()
251
  except Exception as e:
252
  st.error(f"Error creating knowledge base: {e}")
253
+ return
254
 
255
  # Load existing knowledge base
256
  if 'vector_store' not in st.session_state: