File size: 5,605 Bytes
4438381
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import json
import os
from datetime import datetime
import glob
from app import SubmissionForum
from utils.hub_storage import HubStorage



def read_json_file(file_path: str) -> dict:
    """Read and parse local JSON file."""
    try:
        with open(file_path, 'r') as f:
            return json.load(f)
    except Exception as e:
        print(f"Error reading {file_path}: {str(e)}")
        return None

def update_json_file(file_path: str, content: dict, method_name: str = None, new_status: str = None) -> bool:
    """Update local JSON file and add forum post if status changed"""
    try:
        with open(file_path, 'w') as f:
            json.dump(content, f, indent=4)
            
        # Add forum post if this is a status update
        if method_name and new_status:
            forum.add_status_update(method_name, new_status)
            
        return True
    except Exception as e:
        print(f"Error updating {file_path}: {str(e)}")
        return False

def review_pending_submissions(submissions_dir: str = "submissions"):
    """Review all pending submissions and allow admin to approve/reject them."""
    try:
        REPO_ID = "snap-stanford/stark-leaderboard" 
        hub_storage = HubStorage(REPO_ID)
        forum = SubmissionForum(hub_storage=hub_storage)
        # Find all latest.json files
        latest_files = glob.glob(os.path.join(submissions_dir, "**", "latest.json"), recursive=True)
        
        if not latest_files:
            print("No submissions found.")
            return
        
        changes_made = False
        
        # Process each submission
        for latest_file in latest_files:
            folder_path = os.path.dirname(latest_file)
            latest_info = read_json_file(latest_file)
            
            if not latest_info:
                print(f"Could not read {latest_file}, skipping...")
                continue
                
            if latest_info.get('status') != 'pending_review':
                continue
                
            # Find corresponding metadata file
            timestamp = latest_info.get('latest_submission')
            if not timestamp:
                print(f"No timestamp found in {latest_file}, skipping...")
                continue
                
            metadata_file = os.path.join(folder_path, f"metadata_{timestamp}.json")
            metadata = read_json_file(metadata_file)
            
            if not metadata:
                print(f"Could not read metadata for {latest_file}, skipping...")
                continue
            
            # Display submission details
            print("\n" + "="*80)
            print(f"Submission Details:")
            print(f"Method Name: {metadata.get('Method Name')}")
            print(f"Team Name: {metadata.get('Team Name')}")
            print(f"Dataset: {metadata.get('Dataset')}")
            print(f"Split: {metadata.get('Split')}")
            print(f"Contact Email(s): {metadata.get('Contact Email(s)')}")
            print(f"Code Repository: {metadata.get('Code Repository')}")
            print("\nModel Description:")
            print(metadata.get('Model Description', 'No description provided'))
            print("\nResults:")
            if 'results' in metadata:
                for metric, value in metadata['results'].items():
                    print(f"{metric}: {value}")
            print("\nSubmission Date:", metadata.get('submission_date', 'Unknown'))
            print("="*80)
            
            # Get admin decision
            while True:
                decision = input("\nApprove this submission? (y/n/skip/quit): ").lower()
                if decision in ['y', 'n', 'skip', 'quit']:
                    break
                print("Invalid input. Please enter 'y', 'n', 'skip', or 'quit'")
            
            if decision == 'quit':
                print("Exiting review process...")
                break
                
            if decision == 'skip':
                continue
                
            # Update status
            new_status = 'approved' if decision == 'y' else 'rejected'
            review_timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            
            # Update latest.json
            latest_info['status'] = new_status
            latest_info['reviewed_at'] = review_timestamp
            if not update_json_file(
                latest_file, 
                latest_info, 
                method_name=metadata.get('Method Name'),
                new_status=new_status
            ):
                print("Failed to update latest.json")
                continue
            
            # Update metadata
            metadata['status'] = new_status
            metadata['reviewed_at'] = review_timestamp
            if not update_json_file(metadata_file, metadata):
                print("Failed to update metadata file")
                continue
            
            forum.add_status_update(metadata.get('Method Name'), new_status)
            print(f"Submission {new_status}")
            changes_made = True
            
        if changes_made:
            print("\nChanges have been made to the following files:")
            print("Please push these changes to HuggingFace manually.")
            
    except Exception as e:
        print(f"Error during review process: {str(e)}")

if __name__ == "__main__":
    print("Starting submission review process...")
    # You can specify a different submissions directory if needed
    review_pending_submissions("submissions")
    print("\nReview process completed.")