File size: 2,608 Bytes
0ae90b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import json
import os
from pathlib import Path
import argparse

def process_submissions(input_file, output_dir='submissions'):
    """
    Process a JSON file containing multiple model submissions and split it into
    individual JSON files in the submissions directory.
    
    Args:
        input_file (str): Path to the input JSON file
        output_dir (str): Directory where individual submission files will be stored
    """
    # Create submissions directory if it doesn't exist
    Path(output_dir).mkdir(exist_ok=True)
    
    # Read the input file
    try:
        with open(input_file, 'r') as f:
            submissions = json.load(f)
        
        if not isinstance(submissions, list):
            print(f"Error: Input file {input_file} must contain a JSON array of submissions")
            return
        
        # Process each submission
        for submission in submissions:
            if 'model_name' not in submission:
                print(f"Warning: Skipping submission without model_name field")
                continue
                
            model_name = submission['model_name']
            # Create a safe filename from the model name
            safe_filename = f"{model_name.replace('/', '_')}.json"
            output_path = os.path.join(output_dir, safe_filename)
            
            # Write individual submission file
            with open(output_path, 'w') as f:
                json.dump(submission, f, indent=4)
            
            print(f"Created submission file: {output_path}")
        
        print(f"\nProcessed {len(submissions)} submissions successfully!")
        
    except FileNotFoundError:
        print(f"Error: Input file '{input_file}' not found")
    except json.JSONDecodeError:
        print(f"Error: Input file '{input_file}' is not valid JSON")
    except Exception as e:
        print(f"Error processing submissions: {str(e)}")

def main():
    # Set up argument parser
    parser = argparse.ArgumentParser(
        description='Process a JSON file containing model submissions and split into individual files.'
    )
    parser.add_argument(
        'input_file',
        help='Path to the input JSON file containing model submissions'
    )
    parser.add_argument(
        '--output-dir',
        '-o',
        default='submissions',
        help='Directory where individual submission files will be stored (default: submissions)'
    )
    
    # Parse arguments
    args = parser.parse_args()
    
    # Process submissions
    process_submissions(args.input_file, args.output_dir)

if __name__ == "__main__":
    main()