davanstrien HF Staff commited on
Commit
acf6917
·
1 Parent(s): 7e8dc67

Add early authentication check with whoami validation

Browse files

- Validate token at script start using HfApi.whoami()
- Fail fast with clear error if authentication fails
- Show authenticated username for confirmation

Files changed (1) hide show
  1. classify-dataset.py +25 -14
classify-dataset.py CHANGED
@@ -218,6 +218,30 @@ def prepare_prompts(
218
  def main():
219
  args = parse_args()
220
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
  # Check CUDA availability
222
  if not torch.cuda.is_available():
223
  logger.error("CUDA is not available. This script requires a GPU.")
@@ -341,20 +365,7 @@ def main():
341
  success_rate = (valid_texts / total_texts * 100) if total_texts > 0 else 0
342
  logger.info(f"Classification success rate: {success_rate:.1f}%")
343
 
344
- # Get token for authentication
345
- token = args.hf_token
346
- if not token:
347
- # Try to get token from environment or huggingface-cli login
348
- token = os.environ.get("HF_TOKEN") or get_token()
349
-
350
- if not token:
351
- logger.error("No authentication token found. Please either:")
352
- logger.error("1. Run 'huggingface-cli login'")
353
- logger.error("2. Set HF_TOKEN environment variable")
354
- logger.error("3. Pass --hf-token argument")
355
- sys.exit(1)
356
-
357
- # Save to Hub
358
  logger.info(f"Pushing dataset to Hub: {args.output_dataset}")
359
  try:
360
  dataset.push_to_hub(
 
218
  def main():
219
  args = parse_args()
220
 
221
+ # Check authentication early
222
+ logger.info("Checking authentication...")
223
+ token = args.hf_token
224
+ if not token:
225
+ # Try to get token from environment or huggingface-cli login
226
+ token = os.environ.get("HF_TOKEN") or get_token()
227
+
228
+ if not token:
229
+ logger.error("No authentication token found. Please either:")
230
+ logger.error("1. Run 'huggingface-cli login'")
231
+ logger.error("2. Set HF_TOKEN environment variable")
232
+ logger.error("3. Pass --hf-token argument")
233
+ sys.exit(1)
234
+
235
+ # Validate token by checking who we are
236
+ try:
237
+ api = HfApi(token=token)
238
+ user_info = api.whoami()
239
+ logger.info(f"Authenticated as: {user_info['name']}")
240
+ except Exception as e:
241
+ logger.error(f"Authentication failed: {e}")
242
+ logger.error("Please check your token is valid")
243
+ sys.exit(1)
244
+
245
  # Check CUDA availability
246
  if not torch.cuda.is_available():
247
  logger.error("CUDA is not available. This script requires a GPU.")
 
365
  success_rate = (valid_texts / total_texts * 100) if total_texts > 0 else 0
366
  logger.info(f"Classification success rate: {success_rate:.1f}%")
367
 
368
+ # Save to Hub (token already validated at start)
 
 
 
 
 
 
 
 
 
 
 
 
 
369
  logger.info(f"Pushing dataset to Hub: {args.output_dataset}")
370
  try:
371
  dataset.push_to_hub(