jinruiyang Claude Opus 4.5 commited on
Commit
d14d8e4
·
1 Parent(s): 8f481e9

Add Google Sheets integration to backend API

Browse files

- Integrate sheets_storage with /api/entity-feedback endpoint
- Integrate sheets_storage with /api/rating endpoint
- Keep local file backup alongside cloud storage

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

Files changed (1) hide show
  1. backend/api.py +54 -6
backend/api.py CHANGED
@@ -23,6 +23,16 @@ except ImportError:
23
 
24
  from .services import get_knowledge_base, get_retriever, search_knowledge_base, get_stats
25
 
 
 
 
 
 
 
 
 
 
 
26
  # DeepL API configuration
27
  DEEPL_API_KEY = os.environ.get("DEEPL_API_KEY", "")
28
  DEEPL_API_URL = "https://api-free.deepl.com/v2/translate" # Use api.deepl.com for paid plans
@@ -289,7 +299,26 @@ async def api_rating(request: RatingRequest, req: Request):
289
  "rating_value": request.rating_value
290
  }
291
 
292
- # Load existing ratings
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293
  if rating_file.exists():
294
  with open(rating_file, "r", encoding="utf-8") as f:
295
  all_ratings = json.load(f)
@@ -298,11 +327,10 @@ async def api_rating(request: RatingRequest, req: Request):
298
 
299
  all_ratings.append(rating)
300
 
301
- # Save ratings
302
  with open(rating_file, "w", encoding="utf-8") as f:
303
  json.dump(all_ratings, f, ensure_ascii=False, indent=2)
304
 
305
- return {"success": True, "total": len(all_ratings)}
306
 
307
  except Exception as e:
308
  return {"success": False, "error": str(e)}
@@ -336,7 +364,28 @@ async def api_entity_feedback(request: EntityFeedbackRequest, req: Request):
336
  "submitted_at": request.submitted_at
337
  }
338
 
339
- # Load existing feedbacks
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
340
  if feedback_file.exists():
341
  with open(feedback_file, "r", encoding="utf-8") as f:
342
  all_feedbacks = json.load(f)
@@ -345,11 +394,10 @@ async def api_entity_feedback(request: EntityFeedbackRequest, req: Request):
345
 
346
  all_feedbacks.append(feedback)
347
 
348
- # Save feedbacks
349
  with open(feedback_file, "w", encoding="utf-8") as f:
350
  json.dump(all_feedbacks, f, ensure_ascii=False, indent=2)
351
 
352
- return {"success": True, "total": len(all_feedbacks)}
353
 
354
  except Exception as e:
355
  return {"success": False, "error": str(e)}
 
23
 
24
  from .services import get_knowledge_base, get_retriever, search_knowledge_base, get_stats
25
 
26
+ # Google Sheets storage for persistent feedback
27
+ import sys
28
+ sys.path.insert(0, str(Path(__file__).parent.parent / "ir"))
29
+ try:
30
+ import sheets_storage
31
+ SHEETS_ENABLED = sheets_storage.is_sheets_enabled()
32
+ except ImportError:
33
+ SHEETS_ENABLED = False
34
+ sheets_storage = None
35
+
36
  # DeepL API configuration
37
  DEEPL_API_KEY = os.environ.get("DEEPL_API_KEY", "")
38
  DEEPL_API_URL = "https://api-free.deepl.com/v2/translate" # Use api.deepl.com for paid plans
 
299
  "rating_value": request.rating_value
300
  }
301
 
302
+ # Try Google Sheets first (persistent cloud storage)
303
+ sheets_saved = False
304
+ if SHEETS_ENABLED and sheets_storage:
305
+ try:
306
+ success = sheets_storage.save_rating_to_sheets(
307
+ query=request.query,
308
+ category=request.category or "Not selected",
309
+ entity_id=request.entity_id,
310
+ entity_name=request.entity_id, # Use ID as name fallback
311
+ rank=request.entity_index + 1,
312
+ score=0,
313
+ rating=f"{request.rating_type}:{request.rating_value}",
314
+ page=1,
315
+ client_ip=client_ip
316
+ )
317
+ sheets_saved = success
318
+ except Exception as e:
319
+ print(f"Google Sheets save failed: {e}")
320
+
321
+ # Also save to local file as backup
322
  if rating_file.exists():
323
  with open(rating_file, "r", encoding="utf-8") as f:
324
  all_ratings = json.load(f)
 
327
 
328
  all_ratings.append(rating)
329
 
 
330
  with open(rating_file, "w", encoding="utf-8") as f:
331
  json.dump(all_ratings, f, ensure_ascii=False, indent=2)
332
 
333
+ return {"success": True, "total": len(all_ratings), "sheets_saved": sheets_saved}
334
 
335
  except Exception as e:
336
  return {"success": False, "error": str(e)}
 
364
  "submitted_at": request.submitted_at
365
  }
366
 
367
+ # Try Google Sheets first (persistent cloud storage)
368
+ sheets_saved = False
369
+ if SHEETS_ENABLED and sheets_storage:
370
+ try:
371
+ # Convert ratings dict to string for sheet
372
+ ratings_str = json.dumps(request.ratings) if request.ratings else ""
373
+ success = sheets_storage.save_rating_to_sheets(
374
+ query=request.query,
375
+ category=str(request.rank_position), # Use rank as category placeholder
376
+ entity_id=request.entity_id,
377
+ entity_name=request.entity_name,
378
+ rank=request.rank_position,
379
+ score=request.rank_score,
380
+ rating=ratings_str,
381
+ page=1,
382
+ client_ip=client_ip
383
+ )
384
+ sheets_saved = success
385
+ except Exception as e:
386
+ print(f"Google Sheets save failed: {e}")
387
+
388
+ # Also save to local file as backup
389
  if feedback_file.exists():
390
  with open(feedback_file, "r", encoding="utf-8") as f:
391
  all_feedbacks = json.load(f)
 
394
 
395
  all_feedbacks.append(feedback)
396
 
 
397
  with open(feedback_file, "w", encoding="utf-8") as f:
398
  json.dump(all_feedbacks, f, ensure_ascii=False, indent=2)
399
 
400
+ return {"success": True, "total": len(all_feedbacks), "sheets_saved": sheets_saved}
401
 
402
  except Exception as e:
403
  return {"success": False, "error": str(e)}