seonglae-holistic's picture
fix: submission id too long changed until sec
68bce6d
import json
import os
import uuid
from datetime import datetime
from pathlib import Path
from src.display.formatting import styled_error, styled_warning, styled_message
from src.envs import API, EVAL_REQUESTS_PATH, QUEUE_REPO, LOCAL_MODE
from src.submission.check_validity import is_repository_valid
def add_new_eval(
library_name,
) -> str:
"""
Adds a new library to the assessment queue.
Args:
library_name: Name of the library
Returns:
A message indicating the status of the submission
"""
# Check if valid repository
is_valid, validity_message, library_info = is_repository_valid(library_name)
if not is_valid:
return styled_error(f"Invalid submission: {validity_message}")
# Create a unique identifier for the submission
uid = uuid.uuid4().hex[:6]
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
request_filename = f"{library_name.replace('/', '_')}_eval_request_{timestamp}_{uid}.json"
# Stars count and license info from library_info if available
stars = library_info.get("stars", 0)
license_name = library_info.get("license", "unknown")
# Create the assessment request JSON
assessment_request = {
"library": library_name,
"license": license_name,
"stars": stars,
"status": "PENDING",
"submitted_time": timestamp,
"last_updated": timestamp,
"assessment_id": uid
}
# Ensure directory exists
os.makedirs(EVAL_REQUESTS_PATH, exist_ok=True)
# Save the request locally
request_file_path = os.path.join(EVAL_REQUESTS_PATH, request_filename)
with open(request_file_path, "w") as f:
json.dump(assessment_request, f, indent=2)
# If in local mode, don't try to upload to HF
if LOCAL_MODE:
return styled_message(f"Library '{library_name}' has been added to the local assessment queue! Assessment ID: {uid}")
# Try to upload to HF if not in local mode
try:
# Push the file to the HF repo
path = Path(request_file_path)
API.upload_file(
path_or_fileobj=path,
path_in_repo=f"assessment-queue/{request_filename}",
repo_id=QUEUE_REPO,
repo_type="space",
)
return styled_message(f"Library '{library_name}' has been added to the assessment queue! Assessment ID: {uid}")
except Exception as e:
return styled_warning(f"Saved locally but failed to upload to Hugging Face: {str(e)}")