evanzyfan commited on
Commit
07d06f9
·
1 Parent(s): b1cb912

fix saving bug

Browse files
Files changed (1) hide show
  1. app.py +30 -9
app.py CHANGED
@@ -7,6 +7,7 @@ side-by-side with shuffled anonymous labels, and collects preference ordering.
7
  """
8
 
9
  import json
 
10
  import os
11
  import random
12
  import threading
@@ -17,6 +18,12 @@ from typing import Any, Dict, List, Optional, Tuple
17
  import gradio as gr
18
  from huggingface_hub import CommitScheduler, snapshot_download
19
 
 
 
 
 
 
 
20
  # ============================================================================
21
  # Configuration
22
  # ============================================================================
@@ -157,11 +164,6 @@ def get_reference_portraits(story_id: str) -> List[Tuple[str, str]]:
157
  _save_lock = threading.Lock()
158
 
159
 
160
- def _get_lock():
161
- """Return the scheduler lock if available, otherwise use the local lock."""
162
- return scheduler.lock if scheduler is not None else _save_lock
163
-
164
-
165
  # ============================================================================
166
  # Group Management
167
  # ============================================================================
@@ -247,13 +249,25 @@ def create_group_config(group_id: str) -> Dict[str, Any]:
247
  "method_display_map": method_display_map,
248
  }
249
 
250
- with _get_lock():
251
  with open(group_dir / "mapping.json", "w", encoding="utf-8") as f:
252
  json.dump(config, f, indent=2, ensure_ascii=False)
253
 
254
  return config
255
 
256
 
 
 
 
 
 
 
 
 
 
 
 
 
257
  def save_ranking_result(
258
  group_id: str,
259
  story_id: str,
@@ -263,7 +277,6 @@ def save_ranking_result(
263
  comment: str,
264
  ) -> str:
265
  """Save a preference ranking result to JSON. Returns a status message."""
266
- print(f"Saving result to {group_id}, {story_id}, {evaluator_id}, {method_display_map}, {ranking}, {comment}")
267
  group_dir = Path(OUTPUT_DIR) / f"group_{group_id}"
268
  story_dir = group_dir / story_id
269
  filename = f"{story_id}_{evaluator_id}.json"
@@ -279,12 +292,20 @@ def save_ranking_result(
279
  }
280
 
281
  filepath = story_dir / filename
282
- with _get_lock():
283
  story_dir.mkdir(parents=True, exist_ok=True)
284
  with open(filepath, "w", encoding="utf-8") as f:
285
- print(f"Saving result to {filepath}. Result data: {result_data}")
286
  json.dump(result_data, f, indent=4, ensure_ascii=False)
287
 
 
 
 
 
 
 
 
 
 
288
  return f"Saved to {filepath}"
289
 
290
 
 
7
  """
8
 
9
  import json
10
+ import logging
11
  import os
12
  import random
13
  import threading
 
18
  import gradio as gr
19
  from huggingface_hub import CommitScheduler, snapshot_download
20
 
21
+ logging.getLogger("huggingface_hub._commit_scheduler").setLevel(logging.DEBUG)
22
+ logging.basicConfig(
23
+ format="%(asctime)s [%(name)s] %(levelname)s: %(message)s",
24
+ level=logging.INFO,
25
+ )
26
+
27
  # ============================================================================
28
  # Configuration
29
  # ============================================================================
 
164
  _save_lock = threading.Lock()
165
 
166
 
 
 
 
 
 
167
  # ============================================================================
168
  # Group Management
169
  # ============================================================================
 
249
  "method_display_map": method_display_map,
250
  }
251
 
252
+ with _save_lock:
253
  with open(group_dir / "mapping.json", "w", encoding="utf-8") as f:
254
  json.dump(config, f, indent=2, ensure_ascii=False)
255
 
256
  return config
257
 
258
 
259
+ def _on_push_done(future):
260
+ """Callback to surface push results/errors from the background thread."""
261
+ try:
262
+ result = future.result()
263
+ if result:
264
+ print(f"[CommitScheduler] Push succeeded: {result.commit_url}")
265
+ else:
266
+ print("[CommitScheduler] Push skipped: no changed files detected")
267
+ except Exception as e:
268
+ print(f"[CommitScheduler] Push FAILED: {e}")
269
+
270
+
271
  def save_ranking_result(
272
  group_id: str,
273
  story_id: str,
 
277
  comment: str,
278
  ) -> str:
279
  """Save a preference ranking result to JSON. Returns a status message."""
 
280
  group_dir = Path(OUTPUT_DIR) / f"group_{group_id}"
281
  story_dir = group_dir / story_id
282
  filename = f"{story_id}_{evaluator_id}.json"
 
292
  }
293
 
294
  filepath = story_dir / filename
295
+ with _save_lock:
296
  story_dir.mkdir(parents=True, exist_ok=True)
297
  with open(filepath, "w", encoding="utf-8") as f:
 
298
  json.dump(result_data, f, indent=4, ensure_ascii=False)
299
 
300
+ print(f"[Save] Written {filepath}")
301
+
302
+ if scheduler is not None:
303
+ print("[CommitScheduler] Triggering immediate push after save ...")
304
+ future = scheduler.trigger()
305
+ future.add_done_callback(_on_push_done)
306
+ else:
307
+ print("[CommitScheduler] WARNING: scheduler is None — RESULTS_REPO_ID not set?")
308
+
309
  return f"Saved to {filepath}"
310
 
311