Spaces:
Sleeping
Sleeping
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.") |