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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -4
app.py CHANGED
@@ -168,9 +168,20 @@ def save_qa_history(history_entry):
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:
@@ -244,11 +255,22 @@ def add_to_qa_history(query: str, answer: str):
244
  st.warning("Empty query or answer detected, skipping history update")
245
  return None
246
 
 
 
 
 
 
 
 
 
 
 
 
247
  # Create history entry with proper timestamp
248
  history_entry = {
249
  "timestamp": (datetime.now() + timedelta(hours=5)).strftime("%Y-%m-%dT%H:%M:%S"),
250
  "query": query,
251
- "answer": answer
252
  }
253
 
254
  # Save entry
 
168
  st.error("Invalid history data format, must be a list")
169
  history_data = []
170
 
171
+ # Process and validate each entry
172
+ processed_history = []
173
+ for entry in history_data:
174
+ if isinstance(entry, dict) and all(key in entry for key in ["timestamp", "query", "answer"]):
175
+ # Process answer if it's a Document or dict
176
+ if isinstance(entry["answer"], dict):
177
+ entry["answer"] = entry["answer"].get('answer', str(entry["answer"]))
178
+ elif hasattr(entry["answer"], 'content'):
179
+ entry["answer"] = entry["answer"].content
180
+ else:
181
+ entry["answer"] = str(entry["answer"])
182
+ processed_history.append(entry)
183
+
184
+ history_data = processed_history
185
 
186
  # Save updated history locally
187
  try:
 
255
  st.warning("Empty query or answer detected, skipping history update")
256
  return None
257
 
258
+ # Handle different answer types
259
+ if isinstance(answer, dict):
260
+ # If answer is a dict with 'answer' key, extract it
261
+ processed_answer = answer.get('answer', str(answer))
262
+ elif hasattr(answer, 'content'):
263
+ # If answer is a Document-like object with content attribute
264
+ processed_answer = answer.content
265
+ else:
266
+ # Convert answer to string for any other type
267
+ processed_answer = str(answer)
268
+
269
  # Create history entry with proper timestamp
270
  history_entry = {
271
  "timestamp": (datetime.now() + timedelta(hours=5)).strftime("%Y-%m-%dT%H:%M:%S"),
272
  "query": query,
273
+ "answer": processed_answer
274
  }
275
 
276
  # Save entry