thomasbphoto commited on
Commit
d334a2b
·
1 Parent(s): ffd31a1

Reduce pushes + add logging to find issue

Browse files
Files changed (2) hide show
  1. app.py +12 -4
  2. db.py +8 -2
app.py CHANGED
@@ -514,6 +514,7 @@ def dump_database_to_json():
514
  return
515
 
516
  votes = get_all_votes()
 
517
  json_data = [
518
  {
519
  "id": vote.id,
@@ -528,10 +529,17 @@ def dump_database_to_json():
528
  ]
529
 
530
  json_file_path = JSON_DATASET_DIR / "votes.json"
531
- # Upload to Hugging Face
 
 
532
  with commit_scheduler.lock:
533
- with json_file_path.open("w") as f:
534
- json.dump(json_data, f, indent=4)
 
 
 
 
 
535
 
536
  logging.info("Database dumped to JSON")
537
 
@@ -556,7 +564,7 @@ def schedule_dump_database(interval=60):
556
 
557
 
558
  if __name__ == "__main__":
559
- schedule_dump_database() # Start the periodic database dump
560
  demo = gradio_interface()
561
 
562
  demo.launch()
 
514
  return
515
 
516
  votes = get_all_votes()
517
+ logging.info("Preparing to dump %d votes to JSON", len(votes))
518
  json_data = [
519
  {
520
  "id": vote.id,
 
529
  ]
530
 
531
  json_file_path = JSON_DATASET_DIR / "votes.json"
532
+ json_temp_path = JSON_DATASET_DIR / "votes.json.tmp"
533
+
534
+ # Upload to Hugging Face with atomic writes
535
  with commit_scheduler.lock:
536
+ # Write to temporary file first
537
+ with json_temp_path.open("w") as f:
538
+ json.dump(json_data, f)
539
+ # Atomically replace the target file
540
+ os.replace(json_temp_path, json_file_path)
541
+ file_size = json_file_path.stat().st_size
542
+ logging.info("JSON dump succeeded; new file size is %d bytes", file_size)
543
 
544
  logging.info("Database dumped to JSON")
545
 
 
564
 
565
 
566
  if __name__ == "__main__":
567
+ schedule_dump_database(3600) # Start the periodic database dump
568
  demo = gradio_interface()
569
 
570
  demo.launch()
db.py CHANGED
@@ -48,7 +48,13 @@ def fill_database_once(dataset_name="bgsys/votes_datasets_test2"):
48
  with SessionLocal() as db:
49
  # Check if the database is already filled
50
  if db.query(Vote).first() is None:
51
- dataset = load_dataset(dataset_name)
 
 
 
 
 
 
52
  for record in dataset['train']:
53
  # Ensure the timestamp is a string
54
  timestamp_str = record.get("timestamp", datetime.utcnow().isoformat())
@@ -68,7 +74,7 @@ def fill_database_once(dataset_name="bgsys/votes_datasets_test2"):
68
  db_vote = Vote(**vote_data)
69
  db.add(db_vote)
70
  db.commit()
71
- logging.info("Database filled with data from Hugging Face dataset: %s", dataset_name)
72
  else:
73
  logging.info("Database already filled, skipping dataset loading.")
74
 
 
48
  with SessionLocal() as db:
49
  # Check if the database is already filled
50
  if db.query(Vote).first() is None:
51
+ try:
52
+ dataset = load_dataset(dataset_name)
53
+ logging.info("Successfully loaded Hub dataset %s with %d records.", dataset_name, len(dataset['train']))
54
+ except Exception:
55
+ logging.exception("Failed to load Hub dataset %s. Starting with empty local database.", dataset_name)
56
+ return
57
+
58
  for record in dataset['train']:
59
  # Ensure the timestamp is a string
60
  timestamp_str = record.get("timestamp", datetime.utcnow().isoformat())
 
74
  db_vote = Vote(**vote_data)
75
  db.add(db_vote)
76
  db.commit()
77
+ logging.info("Database filled with %d records from Hugging Face dataset: %s", len(dataset['train']), dataset_name)
78
  else:
79
  logging.info("Database already filled, skipping dataset loading.")
80