gionuibk commited on
Commit
55ff9e2
·
verified ·
1 Parent(s): d733330

Upload strategies/data_recorder.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. strategies/data_recorder.py +26 -16
strategies/data_recorder.py CHANGED
@@ -210,25 +210,35 @@ class DataRecorderStrategy(Strategy):
210
  def _test_connectivity(self):
211
  """
212
  Attempts to upload a small hello_world file to verify HF token permissions.
 
213
  """
214
- try:
215
- self.log.info("Testing HuggingFace Dataset connectivity...", LogColor.YELLOW)
216
- test_file = Path("connectivity_test.txt")
217
- test_file.write_text(f"Connected at {datetime.utcnow()}")
218
-
219
- self.api.upload_file(
220
- path_or_fileobj=str(test_file),
221
- path_in_repo="connectivity_test.txt",
222
- repo_id=self.repo_id,
223
- repo_type="dataset",
224
- )
225
- self.log.info("✅ Connectivity Test PASSED! Token is valid.", LogColor.GREEN)
226
- test_file.unlink() # Cleanup
227
- except Exception as e:
228
- self.log.error(f"❌ Connectivity Test FAILED: {e}", LogColor.RED)
229
- self.log.error("Please check HF_TOKEN in Spaces Secrets.", LogColor.RED)
 
 
 
 
 
 
 
 
230
  if self.catalog_path.exists():
231
  shutil.rmtree(self.catalog_path)
 
232
  self.catalog_path.mkdir(parents=True, exist_ok=True)
233
 
234
  # -- EVENT HANDLERS ----------------------------------------------------------------
 
210
  def _test_connectivity(self):
211
  """
212
  Attempts to upload a small hello_world file to verify HF token permissions.
213
+ Retries 3 times to handle startup network lag.
214
  """
215
+ max_retries = 3
216
+ for i in range(max_retries):
217
+ try:
218
+ self.log.info(f"Testing HuggingFace Dataset connectivity (Attempt {i+1}/{max_retries})...", LogColor.YELLOW)
219
+ test_file = Path("connectivity_test.txt")
220
+ test_file.write_text(f"Connected at {datetime.utcnow()}")
221
+
222
+ self.api.upload_file(
223
+ path_or_fileobj=str(test_file),
224
+ path_in_repo="connectivity_test.txt",
225
+ repo_id=self.repo_id,
226
+ repo_type="dataset",
227
+ )
228
+ self.log.info("✅ Connectivity Test PASSED! Token is valid.", LogColor.GREEN)
229
+ test_file.unlink() # Cleanup
230
+ break
231
+ except Exception as e:
232
+ self.log.error(f"⚠️ Connectivity Attempt {i+1} Failed: {e}", LogColor.YELLOW)
233
+ if i < max_retries - 1:
234
+ time.sleep(5)
235
+ else:
236
+ self.log.error(f"❌ Connectivity Test FAILED after {max_retries} attempts.", LogColor.RED)
237
+ self.log.error("Please check HF_TOKEN in Spaces Secrets.", LogColor.RED)
238
+
239
  if self.catalog_path.exists():
240
  shutil.rmtree(self.catalog_path)
241
+
242
  self.catalog_path.mkdir(parents=True, exist_ok=True)
243
 
244
  # -- EVENT HANDLERS ----------------------------------------------------------------