JirasakJo commited on
Commit
6d23111
·
verified ·
1 Parent(s): 288bddc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -102
app.py CHANGED
@@ -165,119 +165,105 @@ def load_qa_history():
165
  def save_qa_history(history_entry):
166
  """Save QA history entry to local JSON file and push to GitHub"""
167
  try:
168
- history_file = Path("qa_history.json")
 
 
 
169
 
170
- # Initialize or load existing history
171
- if history_file.exists():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
172
  try:
173
- with open(history_file, "r", encoding="utf-8") as f:
174
- file_content = f.read()
175
-
176
 
177
- if not file_content.strip():
178
- st.warning("JSON file is empty, initializing new history")
179
- history_data = []
180
- else:
181
- try:
182
- history_data = json.loads(file_content)
183
- if not isinstance(history_data, list):
184
- st.error("JSON file does not contain a list, resetting history")
185
- history_data = []
186
- except json.JSONDecodeError as json_err:
187
- st.error(f"JSON parsing error: {str(json_err)}")
188
- # Try to salvage valid JSON if possible
189
- try:
190
- # Remove any trailing commas
191
- file_content = file_content.replace(",]", "]").replace(",}", "}")
192
- history_data = json.loads(file_content)
193
- except:
194
- st.error("Could not salvage JSON, initializing new history")
195
- history_data = []
196
- except Exception as file_err:
197
- st.error(f"File reading error: {str(file_err)}")
198
- history_data = []
199
  else:
200
- history_data = []
201
 
202
- # Append new entry
 
 
 
 
 
 
 
 
 
 
 
 
203
  history_data.append(history_entry)
204
 
205
- # Validate history data before saving
206
- if not isinstance(history_data, list):
207
- st.error("Invalid history data format, must be a list")
208
- history_data = []
209
-
210
- # Process and validate each entry
211
- processed_history = []
212
- for entry in history_data:
213
- if isinstance(entry, dict) and all(key in entry for key in ["timestamp", "query", "answer"]):
214
- # Process answer if it's a Document or dict
215
- if isinstance(entry["answer"], dict):
216
- entry["answer"] = entry["answer"].get('answer', str(entry["answer"]))
217
- elif hasattr(entry["answer"], 'content'):
218
- entry["answer"] = entry["answer"].content
219
- else:
220
- entry["answer"] = str(entry["answer"])
221
- processed_history.append(entry)
222
-
223
- history_data = processed_history
224
-
225
- # Save updated history locally
226
  try:
227
- json_content = json.dumps(history_data, ensure_ascii=False, indent=2)
228
- with open("qa_history.json", "w", encoding="utf-8") as f:
229
- f.write(json_content)
230
- except Exception as save_err:
231
- st.error(f"Error saving history locally: {str(save_err)}")
232
- return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233
 
234
- # Push to GitHub with error logging
235
- github_token = os.getenv('GITHUB_TOKEN') or st.secrets.get('GITHUB_TOKEN')
236
- if not github_token:
237
- st.error("GitHub token not found in environment or secrets!")
238
- return
239
-
240
- try:
241
- from github import Github
242
- g = Github(github_token)
243
-
244
- repo = g.get_repo("jirasaksaimekJijo/swu-chat-bot-project")
245
-
246
- try:
247
- # Try to get the file first
248
- contents = repo.get_contents("qa_history.json")
249
-
250
- # Ensure content is properly encoded
251
- content = json.dumps(history_data, ensure_ascii=False, indent=2)
252
-
253
- response = repo.update_file(
254
- path="qa_history.json",
255
- message="Update QA history",
256
- content=content,
257
- sha=contents.sha,
258
- branch="main" # Explicitly specify branch
259
- )
260
-
261
- except Exception as file_error:
262
- # File doesn't exist, create it
263
- content = json.dumps(history_data, ensure_ascii=False, indent=2)
264
- response = repo.create_file(
265
- path="qa_history.json",
266
- message="Create QA history",
267
- content=content,
268
- branch="main" # Explicitly specify branch
269
- )
270
- st.success("Successfully created qa_history.json on GitHub")
271
-
272
- except Exception as github_error:
273
- st.error(f"GitHub API error: {str(github_error)}")
274
- import traceback
275
- st.error(f"Full GitHub error trace: {traceback.format_exc()}")
276
-
277
  except Exception as e:
278
- st.error(f"General error in save_qa_history: {str(e)}")
279
  import traceback
280
- st.error(f"Full error trace: {traceback.format_exc()}")
 
 
281
 
282
  def add_to_qa_history(query: str, answer: str):
283
  """Add new QA pair to history with validation"""
 
165
  def save_qa_history(history_entry):
166
  """Save QA history entry to local JSON file and push to GitHub"""
167
  try:
168
+ import requests
169
+ import base64
170
+ import json
171
+ from pathlib import Path
172
 
173
+ # GitHub API configuration
174
+ REPO_OWNER = "jirasaksaimekJijo"
175
+ REPO_NAME = "swu-chat-bot-project"
176
+ FILE_PATH = "qa_history.json"
177
+ GITHUB_TOKEN = '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'
178
+
179
+ # First, load existing data from GitHub
180
+ api_url = f"https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/contents/{FILE_PATH}"
181
+ headers = {
182
+ "Accept": "application/vnd.github.v3+json",
183
+ "Authorization": f"token {GITHUB_TOKEN}"
184
+ }
185
+
186
+ # Try to get existing file first
187
+ response = requests.get(api_url, headers=headers)
188
+
189
+ # Initialize empty history data
190
+ history_data = []
191
+ sha = None
192
+
193
+ if response.status_code == 200:
194
+ # File exists, get its content and SHA
195
+ content_data = response.json()
196
+ sha = content_data["sha"]
197
+
198
  try:
199
+ # Decode and parse existing content
200
+ file_content = base64.b64decode(content_data["content"]).decode("utf-8")
 
201
 
202
+ if file_content.strip(): # Make sure content is not empty
203
+ history_data = json.loads(file_content)
204
+
205
+ # Ensure history_data is a list
206
+ if not isinstance(history_data, list):
207
+ st.warning("Existing history data is not a list. Initializing new list.")
208
+ history_data = []
209
+ except Exception as e:
210
+ st.warning(f"Error parsing existing history: {e}. Initializing new list.")
211
+ elif response.status_code == 404:
212
+ # File doesn't exist yet
213
+ st.info("Creating new QA history file.")
 
 
 
 
 
 
 
 
 
 
214
  else:
215
+ st.error(f"Failed to check existing history: {response.status_code} - {response.reason}")
216
 
217
+ # Process history entry before appending
218
+ if isinstance(history_entry, dict) and all(key in history_entry for key in ["timestamp", "query", "answer"]):
219
+ # Process answer if it's a dict
220
+ if isinstance(history_entry["answer"], dict):
221
+ history_entry["answer"] = history_entry["answer"].get('answer', str(history_entry["answer"]))
222
+ # Process answer if it's a Document-like object
223
+ elif hasattr(history_entry["answer"], 'content'):
224
+ history_entry["answer"] = history_entry["answer"].content
225
+ # Convert to string for any other type
226
+ else:
227
+ history_entry["answer"] = str(history_entry["answer"])
228
+
229
+ # Append new entry to history data
230
  history_data.append(history_entry)
231
 
232
+ # Also save locally for backup
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233
  try:
234
+ local_path = Path("qa_history.json")
235
+ with open(local_path, "w", encoding="utf-8") as f:
236
+ json.dump(history_data, f, ensure_ascii=False, indent=2)
237
+ except Exception as local_err:
238
+ st.warning(f"Failed to save local backup: {local_err}")
239
+
240
+ # Prepare content for GitHub
241
+ updated_content = json.dumps(history_data, ensure_ascii=False, indent=2)
242
+ encoded_content = base64.b64encode(updated_content.encode('utf-8')).decode('utf-8')
243
+
244
+ # Prepare the update/create payload
245
+ data = {
246
+ "message": "Update QA history",
247
+ "content": encoded_content,
248
+ }
249
+
250
+ if sha: # If file exists, include its SHA
251
+ data["sha"] = sha
252
+
253
+ # Update or create the file
254
+ update_response = requests.put(api_url, headers=headers, json=data)
255
+
256
+ if update_response.status_code in [200, 201]:
257
+ return True
258
+ else:
259
+ st.error(f"Failed to update QA history: {update_response.status_code} - {update_response.text}")
260
+ return False
261
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
262
  except Exception as e:
 
263
  import traceback
264
+ st.error(f"Error in save_qa_history: {str(e)}")
265
+ st.error(f"Traceback: {traceback.format_exc()}")
266
+ return False
267
 
268
  def add_to_qa_history(query: str, answer: str):
269
  """Add new QA pair to history with validation"""