JirasakJo commited on
Commit
ada34cb
·
verified ·
1 Parent(s): 4d7ce01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -59
app.py CHANGED
@@ -13,7 +13,7 @@ from calendar_rag import (
13
  )
14
 
15
  # Define repository path
16
- os.environ["GITHUB_REPO_PATH"] = r"D:\Last SWU\swu-chat-bot-project"
17
 
18
  # Initialize global state
19
  if 'all_history' not in st.session_state:
@@ -131,66 +131,60 @@ def load_qa_history():
131
 
132
  def save_qa_history(history_entry):
133
  """
134
- Save QA history entry to local JSON file and push to GitHub with improved error handling
135
- and configurable paths.
136
  """
137
  try:
138
- # Get absolute paths
139
- current_dir = Path.cwd()
140
- history_file = current_dir / "qa_history.json"
141
- repo_path = Path(os.getenv('GITHUB_REPO_PATH', ''))
142
 
143
- if not repo_path.exists():
144
- print(f"Repository path does not exist: {repo_path}")
145
- return False
146
-
147
- # Initialize or load existing history
148
  history_data = []
149
  if history_file.exists():
150
  try:
151
  with open(history_file, "r", encoding="utf-8") as f:
152
  history_data = json.load(f)
153
  except json.JSONDecodeError:
154
- print("Error reading existing history file, starting fresh")
155
- pass
156
 
157
- # Append new entries
158
  if isinstance(history_entry, list):
159
  history_data.extend(history_entry)
160
  else:
161
  history_data.append(history_entry)
162
 
163
- # Save locally first
164
  with open(history_file, "w", encoding="utf-8") as f:
165
  json.dump(history_data, f, ensure_ascii=False, indent=2)
166
 
167
- # Copy file to repository if it's in a different location
168
- repo_history_file = repo_path / "qa_history.json"
169
- if history_file != repo_history_file:
170
- import shutil
171
- shutil.copy2(history_file, repo_history_file)
172
-
173
  # Git operations
174
  try:
175
- repo = Repo(repo_path)
176
- repo.git.add(str(repo_history_file))
177
 
178
  if repo.is_dirty():
179
- repo.index.commit("Update QA history")
180
  origin = repo.remote('origin')
181
- origin.push()
182
- print("Successfully pushed to GitHub")
 
 
 
 
 
 
 
 
 
183
  else:
184
  print("No changes to commit")
185
-
186
- return True
187
 
188
  except Exception as git_error:
189
  print(f"Git operation failed: {str(git_error)}")
190
  return False
191
 
192
  except Exception as e:
193
- print(f"Error in save_qa_history: {str(e)}")
194
  return False
195
 
196
  def add_to_qa_history(query: str, answer: str):
@@ -268,55 +262,78 @@ def display_chat_history():
268
 
269
  def initialize_github_sync():
270
  """
271
- Initialize GitHub repository connection and validate configuration.
272
- Returns True if GitHub sync is properly configured, False otherwise.
273
  """
274
  try:
275
- # Get repository path
276
- repo_path = Path(os.getenv('GITHUB_REPO_PATH', ''))
 
277
 
278
- # Validate repository path
279
- if not repo_path or not repo_path.exists():
280
- print(f"Repository directory does not exist: {repo_path}")
281
  return False
282
 
283
  try:
284
  # Initialize repository
285
- repo = Repo(repo_path)
286
 
287
- # Ensure it's a valid git repository
288
- if not repo.git_dir:
289
- print("Invalid Git repository")
290
  return False
291
 
292
- # Check for origin remote
293
  try:
294
  origin = repo.remote('origin')
295
- # Validate origin URL
296
- if not origin.urls:
297
- print("Remote 'origin' has no URL configured")
298
  return False
299
- except ValueError:
300
- print("Remote 'origin' not found")
301
- return False
 
 
 
 
302
 
303
- # Test Git operations
304
- try:
305
- # Test git status
306
- repo.git.status()
307
- # Test if we can get the current branch
308
- repo.active_branch
309
- return True
310
- except Exception as git_error:
311
- print(f"Git operations test failed: {str(git_error)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
312
  return False
313
 
314
  except Exception as repo_error:
315
- print(f"Error validating Git repository: {str(repo_error)}")
316
  return False
317
 
318
  except Exception as e:
319
- print(f"Error initializing GitHub sync: {str(e)}")
320
  return False
321
 
322
  def main():
 
13
  )
14
 
15
  # Define repository path
16
+ os.environ["GITHUB_REPO_PATH"] = "D:\Last SWU\swu-chat-bot-project"
17
 
18
  # Initialize global state
19
  if 'all_history' not in st.session_state:
 
131
 
132
  def save_qa_history(history_entry):
133
  """
134
+ Save QA history to Git with improved authentication handling.
 
135
  """
136
  try:
137
+ repo_path = Path(os.getenv('GITHUB_REPO_PATH', '')).resolve()
138
+ history_file = repo_path / "qa_history.json"
 
 
139
 
140
+ # Load existing history
 
 
 
 
141
  history_data = []
142
  if history_file.exists():
143
  try:
144
  with open(history_file, "r", encoding="utf-8") as f:
145
  history_data = json.load(f)
146
  except json.JSONDecodeError:
147
+ print("Creating new history file")
 
148
 
149
+ # Update history
150
  if isinstance(history_entry, list):
151
  history_data.extend(history_entry)
152
  else:
153
  history_data.append(history_entry)
154
 
155
+ # Save updated history
156
  with open(history_file, "w", encoding="utf-8") as f:
157
  json.dump(history_data, f, ensure_ascii=False, indent=2)
158
 
 
 
 
 
 
 
159
  # Git operations
160
  try:
161
+ repo = Repo(str(repo_path))
162
+ repo.index.add([str(history_file)])
163
 
164
  if repo.is_dirty():
165
+ commit = repo.index.commit("Update QA history")
166
  origin = repo.remote('origin')
167
+
168
+ try:
169
+ origin.push()
170
+ print(f"Successfully pushed commit {commit.hexsha}")
171
+ return True
172
+ except git.GitCommandError as push_error:
173
+ if "Authentication failed" in str(push_error):
174
+ print("Authentication failed - please check your Git credentials")
175
+ else:
176
+ print(f"Push failed: {push_error}")
177
+ return False
178
  else:
179
  print("No changes to commit")
180
+ return True
 
181
 
182
  except Exception as git_error:
183
  print(f"Git operation failed: {str(git_error)}")
184
  return False
185
 
186
  except Exception as e:
187
+ print(f"Save operation failed: {str(e)}")
188
  return False
189
 
190
  def add_to_qa_history(query: str, answer: str):
 
262
 
263
  def initialize_github_sync():
264
  """
265
+ Initialize GitHub repository connection with authentication handling.
 
266
  """
267
  try:
268
+ # Get and normalize repository path
269
+ repo_path = Path(os.getenv('GITHUB_REPO_PATH', '')).resolve()
270
+ print(f"Initializing repo at: {repo_path}")
271
 
272
+ if not repo_path.exists():
273
+ print(f"Repository directory not found: {repo_path}")
 
274
  return False
275
 
276
  try:
277
  # Initialize repository
278
+ repo = Repo(str(repo_path))
279
 
280
+ # Verify .git directory
281
+ if not (repo_path / '.git').exists():
282
+ print(".git directory not found")
283
  return False
284
 
285
+ # Check remote configuration
286
  try:
287
  origin = repo.remote('origin')
288
+ urls = list(origin.urls)
289
+ if not urls:
290
+ print("Remote 'origin' has no URL")
291
  return False
292
+
293
+ expected_url = "https://github.com/jirasaksaimekJijo/swu-chat-bot-project.git"
294
+ if expected_url not in urls:
295
+ print(f"Unexpected remote URL: {urls[0]}")
296
+ return False
297
+
298
+ print(f"Verified remote URL: {urls[0]}")
299
 
300
+ # Test Git operations
301
+ try:
302
+ repo.git.status()
303
+ print("Git status check passed")
304
+
305
+ # Try to create a test commit to verify write access
306
+ test_file = repo_path / '.git' / 'test_sync'
307
+ try:
308
+ test_file.touch()
309
+ repo.index.add([str(test_file)])
310
+ repo.index.commit("Test sync commit")
311
+ origin.push()
312
+ test_file.unlink() # Clean up test file
313
+ print("Push test successful")
314
+ except git.GitCommandError as push_error:
315
+ if "Authentication failed" in str(push_error):
316
+ print("Authentication failed - please check your Git credentials")
317
+ return False
318
+ print(f"Push test failed: {push_error}")
319
+ return False
320
+
321
+ return True
322
+
323
+ except Exception as git_error:
324
+ print(f"Git operations failed: {str(git_error)}")
325
+ return False
326
+
327
+ except ValueError:
328
+ print("Remote 'origin' not configured")
329
  return False
330
 
331
  except Exception as repo_error:
332
+ print(f"Repository validation failed: {str(repo_error)}")
333
  return False
334
 
335
  except Exception as e:
336
+ print(f"Initialization failed: {str(e)}")
337
  return False
338
 
339
  def main():