Shiyu Zhao commited on
Commit
7e2b9c0
·
1 Parent(s): 1a12918

Update space

Browse files
Files changed (1) hide show
  1. app.py +57 -28
app.py CHANGED
@@ -14,6 +14,7 @@ from email.mime.text import MIMEText
14
  from huggingface_hub import HfApi
15
  from tempfile import NamedTemporaryFile
16
  import shutil
 
17
 
18
  from stark_qa import load_qa
19
  from stark_qa.evaluator import Evaluator
@@ -484,6 +485,7 @@ def process_submission(
484
  code_repo, csv_file, model_description, hardware, paper_link
485
  ):
486
  """Process and validate submission"""
 
487
  try:
488
  # Input validation
489
  if not all([method_name, team_name, dataset, split, contact_email, code_repo, csv_file]):
@@ -513,42 +515,45 @@ def process_submission(
513
  }
514
 
515
  # Save and process files
516
- api = HfApi()
517
  REPO_ID = "snap-stanford/stark-leaderboard" # Replace with your space name
518
  HF_TOKEN = os.getenv("HF_TOKEN")
519
 
520
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
521
  folder_name = f"{sanitize_name(method_name)}_{sanitize_name(team_name)}"
522
 
523
- # Handle CSV file upload
524
- csv_filename = f"predictions_{timestamp}.csv"
525
- csv_path_in_repo = f"submissions/{folder_name}/{csv_filename}"
526
-
527
- # Create a temporary file to store the CSV content
528
- with NamedTemporaryFile(mode='wb', delete=False) as tmp_file:
529
- # Handle different types of file objects that Gradio might provide
 
 
 
 
530
  if hasattr(csv_file, 'name'):
531
- # If it's a path string or has a name attribute
532
- with open(csv_file if isinstance(csv_file, str) else csv_file.name, 'rb') as source:
533
- shutil.copyfileobj(source, tmp_file)
534
  else:
535
  # If it's a file-like object
536
- csv_file.seek(0)
537
- shutil.copyfileobj(csv_file, tmp_file)
538
-
539
- try:
540
- hub_storage.save_to_hub(
541
- file_content=tmp_file.name,
542
- path_in_repo=csv_path_in_repo,
543
- commit_message=f"Add submission: {method_name} by {team_name}"
544
- )
545
- finally:
546
- os.unlink(tmp_file.name)
 
547
 
548
  # Process evaluation
549
  # Use the temporary file path for evaluation
550
  results = compute_metrics(
551
- tmp_file.name, # Use the temporary file path
552
  dataset=dataset.lower(),
553
  split=split,
554
  num_workers=4
@@ -557,6 +562,18 @@ def process_submission(
557
  if isinstance(results, str):
558
  send_error_notification(meta_data, results)
559
  return f"Evaluation error: {results}"
 
 
 
 
 
 
 
 
 
 
 
 
560
 
561
  # Process results (multiply by 100)
562
  processed_results = {
@@ -574,19 +591,23 @@ def process_submission(
574
  "submission_date": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
575
  "csv_path": csv_path_in_repo
576
  }
 
 
 
 
 
 
577
 
578
  metadata_path = f"submissions/{folder_name}/metadata_{timestamp}.json"
579
- with NamedTemporaryFile(mode='w', delete=False) as tmp_file:
580
- json.dump(submission_data, tmp_file, indent=4)
581
 
582
  try:
583
  hub_storage.save_to_hub(
584
- file_content=tmp_file.name,
585
  path_in_repo=metadata_path,
586
  commit_message=f"Add metadata: {method_name} by {team_name}"
587
  )
588
- finally:
589
- os.unlink(tmp_file.name)
590
 
591
  # Send confirmation email and update leaderboard
592
  send_submission_confirmation(meta_data, processed_results)
@@ -613,6 +634,14 @@ def process_submission(
613
  # meta_data will always be defined here since we create it at the beginning
614
  send_error_notification(meta_data, error_message)
615
  return error_message
 
 
 
 
 
 
 
 
616
 
617
  def filter_by_model_type(df, selected_types):
618
  if not selected_types:
 
14
  from huggingface_hub import HfApi
15
  from tempfile import NamedTemporaryFile
16
  import shutil
17
+ import tempfile
18
 
19
  from stark_qa import load_qa
20
  from stark_qa.evaluator import Evaluator
 
485
  code_repo, csv_file, model_description, hardware, paper_link
486
  ):
487
  """Process and validate submission"""
488
+ temp_files = []
489
  try:
490
  # Input validation
491
  if not all([method_name, team_name, dataset, split, contact_email, code_repo, csv_file]):
 
515
  }
516
 
517
  # Save and process files
 
518
  REPO_ID = "snap-stanford/stark-leaderboard" # Replace with your space name
519
  HF_TOKEN = os.getenv("HF_TOKEN")
520
 
521
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
522
  folder_name = f"{sanitize_name(method_name)}_{sanitize_name(team_name)}"
523
 
524
+ temp_csv_path = None
525
+ if isinstance(csv_file, str):
526
+ # If it's already a file path, use it directly
527
+ temp_csv_path = csv_file
528
+ else:
529
+ # Create a temporary file with a .csv extension
530
+ temp_fd, temp_csv_path = tempfile.mkstemp(suffix='.csv')
531
+ temp_files.append(temp_csv_path)
532
+ os.close(temp_fd)
533
+
534
+ # Write the content to the temporary file
535
  if hasattr(csv_file, 'name'):
536
+ # If it's a file object with a name attribute
537
+ shutil.copy2(csv_file.name, temp_csv_path)
 
538
  else:
539
  # If it's a file-like object
540
+ with open(temp_csv_path, 'wb') as temp_file:
541
+ if hasattr(csv_file, 'seek'):
542
+ csv_file.seek(0)
543
+ if hasattr(csv_file, 'read'):
544
+ shutil.copyfileobj(csv_file, temp_file)
545
+ else:
546
+ temp_file.write(csv_file)
547
+
548
+ # Verify the CSV file exists and is readable
549
+ if not os.path.exists(temp_csv_path):
550
+ raise FileNotFoundError(f"Failed to create temporary CSV file at {temp_csv_path}")
551
+
552
 
553
  # Process evaluation
554
  # Use the temporary file path for evaluation
555
  results = compute_metrics(
556
+ csv_path=temp_csv_path, # Use the temporary file path
557
  dataset=dataset.lower(),
558
  split=split,
559
  num_workers=4
 
562
  if isinstance(results, str):
563
  send_error_notification(meta_data, results)
564
  return f"Evaluation error: {results}"
565
+
566
+ csv_filename = f"predictions_{timestamp}.csv"
567
+ csv_path_in_repo = f"submissions/{folder_name}/{csv_filename}"
568
+
569
+ try:
570
+ hub_storage.save_to_hub(
571
+ file_content=temp_csv_path,
572
+ path_in_repo=csv_path_in_repo,
573
+ commit_message=f"Add submission: {method_name} by {team_name}"
574
+ )
575
+ except Exception as e:
576
+ raise RuntimeError(f"Failed to save CSV to HuggingFace Hub: {str(e)}")
577
 
578
  # Process results (multiply by 100)
579
  processed_results = {
 
591
  "submission_date": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
592
  "csv_path": csv_path_in_repo
593
  }
594
+ metadata_fd, temp_metadata_path = tempfile.mkstemp(suffix='.json')
595
+ temp_files.append(temp_metadata_path)
596
+ os.close(metadata_fd)
597
+
598
+ with open(temp_metadata_path, 'w') as f:
599
+ json.dump(submission_data, f, indent=4)
600
 
601
  metadata_path = f"submissions/{folder_name}/metadata_{timestamp}.json"
 
 
602
 
603
  try:
604
  hub_storage.save_to_hub(
605
+ file_content=temp_metadata_path,
606
  path_in_repo=metadata_path,
607
  commit_message=f"Add metadata: {method_name} by {team_name}"
608
  )
609
+ except Exception as e:
610
+ raise RuntimeError(f"Failed to save metadata to HuggingFace Hub: {str(e)}")
611
 
612
  # Send confirmation email and update leaderboard
613
  send_submission_confirmation(meta_data, processed_results)
 
634
  # meta_data will always be defined here since we create it at the beginning
635
  send_error_notification(meta_data, error_message)
636
  return error_message
637
+ finally:
638
+ # Clean up temporary files
639
+ for temp_file in temp_files:
640
+ try:
641
+ if os.path.exists(temp_file):
642
+ os.unlink(temp_file)
643
+ except Exception as e:
644
+ print(f"Warning: Failed to delete temporary file {temp_file}: {str(e)}")
645
 
646
  def filter_by_model_type(df, selected_types):
647
  if not selected_types: