JirasakJo commited on
Commit
36b4705
·
verified ·
1 Parent(s): 5e756bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -7
app.py CHANGED
@@ -127,21 +127,60 @@ def save_qa_history(history_entry):
127
 
128
  # Initialize or load existing history
129
  if history_file.exists():
130
- with open(history_file, "r", encoding="utf-8") as f:
131
- try:
132
- history_data = json.load(f)
133
- except json.JSONDecodeError:
134
- st.error("Error parsing existing JSON file")
 
 
 
 
135
  history_data = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
  else:
 
137
  history_data = []
138
 
139
  # Append new entry
140
  history_data.append(history_entry)
141
 
 
 
 
 
 
 
 
 
 
142
  # Save updated history locally
143
- with open("qa_history.json", "w", encoding="utf-8") as f:
144
- json.dump(history_data, f, ensure_ascii=False, indent=2)
 
 
 
 
 
 
145
 
146
  # Push to GitHub with error logging
147
  github_token = os.getenv('GITHUB_TOKEN') or st.secrets.get('GITHUB_TOKEN')
 
127
 
128
  # Initialize or load existing history
129
  if history_file.exists():
130
+ try:
131
+ with open(history_file, "r", encoding="utf-8") as f:
132
+ file_content = f.read()
133
+
134
+ # Debug: Log file content
135
+ st.info(f"File content (first 500 chars): {file_content[:500]}")
136
+
137
+ if not file_content.strip():
138
+ st.warning("JSON file is empty, initializing new history")
139
  history_data = []
140
+ else:
141
+ try:
142
+ history_data = json.loads(file_content)
143
+ if not isinstance(history_data, list):
144
+ st.error("JSON file does not contain a list, resetting history")
145
+ history_data = []
146
+ except json.JSONDecodeError as json_err:
147
+ st.error(f"JSON parsing error: {str(json_err)}")
148
+ # Try to salvage valid JSON if possible
149
+ try:
150
+ # Remove any trailing commas
151
+ file_content = file_content.replace(",]", "]").replace(",}", "}")
152
+ history_data = json.loads(file_content)
153
+ except:
154
+ st.error("Could not salvage JSON, initializing new history")
155
+ history_data = []
156
+ except Exception as file_err:
157
+ st.error(f"File reading error: {str(file_err)}")
158
+ history_data = []
159
  else:
160
+ st.info("No existing history file found, creating new one")
161
  history_data = []
162
 
163
  # Append new entry
164
  history_data.append(history_entry)
165
 
166
+ # Validate history data before saving
167
+ if not isinstance(history_data, list):
168
+ st.error("Invalid history data format, must be a list")
169
+ history_data = []
170
+
171
+ # Remove any None or invalid entries
172
+ history_data = [entry for entry in history_data if isinstance(entry, dict) and
173
+ all(key in entry for key in ["timestamp", "query", "answer"])]
174
+
175
  # Save updated history locally
176
+ try:
177
+ json_content = json.dumps(history_data, ensure_ascii=False, indent=2)
178
+ with open("qa_history.json", "w", encoding="utf-8") as f:
179
+ f.write(json_content)
180
+ st.success("Successfully saved history locally")
181
+ except Exception as save_err:
182
+ st.error(f"Error saving history locally: {str(save_err)}")
183
+ return
184
 
185
  # Push to GitHub with error logging
186
  github_token = os.getenv('GITHUB_TOKEN') or st.secrets.get('GITHUB_TOKEN')