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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -36
app.py CHANGED
@@ -135,9 +135,15 @@ def save_qa_history(history_entry):
135
  and configurable paths.
136
  """
137
  try:
138
- # Save to local JSON file first
139
- history_file = Path("qa_history.json")
 
 
140
 
 
 
 
 
141
  # Initialize or load existing history
142
  history_data = []
143
  if history_file.exists():
@@ -145,41 +151,46 @@ def save_qa_history(history_entry):
145
  with open(history_file, "r", encoding="utf-8") as f:
146
  history_data = json.load(f)
147
  except json.JSONDecodeError:
 
148
  pass
149
 
150
- # Append new entry if it's a list of entries
151
  if isinstance(history_entry, list):
152
  history_data.extend(history_entry)
153
  else:
154
  history_data.append(history_entry)
155
 
156
- # Save updated history locally
157
  with open(history_file, "w", encoding="utf-8") as f:
158
  json.dump(history_data, f, ensure_ascii=False, indent=2)
159
 
160
- # Try to push to GitHub if configured
 
 
 
 
 
 
161
  try:
162
- repo_path = os.getenv('GITHUB_REPO_PATH') or st.secrets.get('GITHUB_REPO_PATH')
 
163
 
164
- if repo_path and Path(repo_path).exists():
165
- repo = Repo(repo_path)
166
- repo.git.add(str(history_file.absolute()))
167
- if repo.is_dirty():
168
- repo.index.commit("Update QA history")
169
- origin = repo.remote(name="origin")
170
- origin.push()
171
- else:
172
- print("No changes to commit.")
173
  else:
174
- print("GitHub sync skipped - repository path not configured or doesn't exist")
 
 
175
 
176
  except Exception as git_error:
177
- print(f"GitHub sync failed (continuing without GitHub push): {str(git_error)}")
178
-
179
- return True
180
 
181
  except Exception as e:
182
- print(f"Error saving QA history: {str(e)}")
183
  return False
184
 
185
  def add_to_qa_history(query: str, answer: str):
@@ -261,30 +272,49 @@ def initialize_github_sync():
261
  Returns True if GitHub sync is properly configured, False otherwise.
262
  """
263
  try:
264
- repo_path = os.getenv('GITHUB_REPO_PATH') or st.secrets.get('GITHUB_REPO_PATH')
 
265
 
266
- if not repo_path:
267
- print("GitHub repository path not configured")
268
- return False
269
-
270
- repo_dir = Path(repo_path)
271
- if not repo_dir.exists():
272
  print(f"Repository directory does not exist: {repo_path}")
273
  return False
274
 
275
- # Validate Git repository
276
  try:
277
- repo = Repo(repo_dir)
278
- # Check if remote 'origin' exists
279
- if 'origin' not in [remote.name for remote in repo.remotes]:
280
- print("Remote 'origin' not configured in repository")
 
 
281
  return False
282
- except Exception as e:
283
- print(f"Error validating Git repository: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
284
  return False
285
 
286
- return True
287
-
288
  except Exception as e:
289
  print(f"Error initializing GitHub sync: {str(e)}")
290
  return False
 
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():
 
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):
 
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