File size: 3,021 Bytes
9ab539a
 
bccaf50
 
 
 
 
9ab539a
bccaf50
 
 
9ab539a
bccaf50
 
 
9ab539a
 
bccaf50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ab539a
bccaf50
 
 
9ab539a
bccaf50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os
import re
import time
import uuid
from datetime import datetime
from pathlib import Path

import huggingface_hub
import requests
from huggingface_hub import HfApi

from src.display.utils import LibraryType, Language, AssessmentStatus
from src.envs import API, EVAL_REQUESTS_PATH, QUEUE_REPO, TOKEN
from src.submission.check_validity import is_repository_valid, get_library_info


def add_new_eval(
    library_name,
    library_version,
    repository_url,
    language,
    framework,
    library_type_str,
) -> str:
    """
    Adds a new library to the assessment queue.
    
    Args:
        library_name: Name of the library (org/repo format)
        library_version: Version of the library
        repository_url: URL to the repository
        language: Programming language
        framework: Related framework/ecosystem
        library_type_str: Type of AI 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, repository_url)
    
    if not is_valid:
        return f"⚠️ Invalid submission: {validity_message}"

    # Parse library type
    library_type = LibraryType.from_str(library_type_str)
    if library_type == LibraryType.Unknown:
        return "⚠️ Please select a valid library type."

    # Create a unique identifier for the submission
    uid = uuid.uuid4().hex[:6]
    timestamp = datetime.now().isoformat()
    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,
        "version": library_version,
        "repository_url": repository_url,
        "language": language,
        "framework": framework,
        "library_type": library_type.value.name,
        "license": license_name,
        "stars": stars,
        "status": "PENDING",
        "submitted_time": timestamp,
        "last_updated": timestamp,
        "assessment_id": uid
    }
    
    # Save the request 
    os.makedirs(EVAL_REQUESTS_PATH, exist_ok=True)
    with open(os.path.join(EVAL_REQUESTS_PATH, request_filename), "w") as f:
        json.dump(assessment_request, f, indent=2)
    
    try:
        # Push the file to the HF repo
        path = Path(os.path.join(EVAL_REQUESTS_PATH, request_filename))
        API.upload_file(
            path_or_fileobj=path,
            path_in_repo=request_filename,
            repo_id=QUEUE_REPO,
            repo_type="dataset",
        )
        
        return f"✅ Library '{library_name}' (version {library_version}) has been added to the assessment queue! Assessment ID: {uid}"
    
    except Exception as e:
        return f"Error uploading assessment request: {str(e)}"