File size: 4,192 Bytes
03696ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os
import re
from datetime import datetime, timezone

from src.envs import API, EVAL_REQUESTS_PATH, QUEUE_REPO, TOKEN
from src.submission.check_validity import (
    already_submitted_models,
    check_model_card,
    get_model_size,
    is_model_on_hub,
)

REQUESTED_MODELS = None
USERS_TO_SUBMISSION_DATES = None
MODEL_ID_PATTERN = re.compile(r"^[\w.-]+/[\w.-]+$")


def response(status: str, message: str) -> dict[str, str]:
    return {"status": status, "message": message}


def add_new_eval(
    model: str,
    base_model: str,
    revision: str,
    precision: str,
    weight_type: str,
    model_type: str,
):
    global REQUESTED_MODELS
    global USERS_TO_SUBMISSION_DATES
    if REQUESTED_MODELS is None:
        REQUESTED_MODELS, USERS_TO_SUBMISSION_DATES = already_submitted_models(EVAL_REQUESTS_PATH)

    model = model.strip()
    base_model = base_model.strip()
    revision = revision.strip() or "main"
    if not MODEL_ID_PATTERN.fullmatch(model):
        return response("error", "Enter a model ID in the form organization/model-name.")

    user_name, model_path = model.split("/", 1)
    precision = precision.split(" ")[0]
    current_time = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")

    if not model_type:
        return response("error", "Please select a model type.")

    if weight_type in ["Delta", "Adapter"]:
        if not MODEL_ID_PATTERN.fullmatch(base_model):
            return response("error", "Enter a base model ID in the form organization/model-name.")
        base_model_on_hub, error, _ = is_model_on_hub(
            model_name=base_model,
            revision=revision,
            token=TOKEN,
            test_tokenizer=True,
        )
        if not base_model_on_hub:
            return response("error", f'Base model "{base_model}" {error}')

    if weight_type != "Adapter":
        model_on_hub, error, _ = is_model_on_hub(
            model_name=model,
            revision=revision,
            token=TOKEN,
            test_tokenizer=True,
        )
        if not model_on_hub:
            return response("error", f'Model "{model}" {error}')

    try:
        model_info = API.model_info(repo_id=model, revision=revision)
    except Exception:
        return response("error", "Could not read this model. Check its ID and revision, then try again.")

    model_size = get_model_size(model_info=model_info, precision=precision)
    try:
        license_name = model_info.cardData["license"]
    except Exception:
        return response("error", "Please select a license in your model card.")

    modelcard_ok, error_msg = check_model_card(model)
    if not modelcard_ok:
        return response("error", error_msg)

    eval_entry = {
        "model": model,
        "base_model": base_model,
        "revision": revision,
        "precision": precision,
        "weight_type": weight_type,
        "status": "PENDING",
        "submitted_time": current_time,
        "model_type": model_type,
        "likes": model_info.likes,
        "params": model_size,
        "license": license_name,
        "private": False,
    }

    request_key = f"{model}_{revision}_{precision}"
    if request_key in REQUESTED_MODELS:
        return response("warning", "This model has already been submitted.")

    out_dir = os.path.join(EVAL_REQUESTS_PATH, user_name)
    os.makedirs(out_dir, exist_ok=True)
    out_path = os.path.join(out_dir, f"{model_path}_eval_request_False_{precision}_{weight_type}.json")
    with open(out_path, "w") as file:
        json.dump(eval_entry, file)

    try:
        API.upload_file(
            path_or_fileobj=out_path,
            path_in_repo=os.path.relpath(out_path, EVAL_REQUESTS_PATH),
            repo_id=QUEUE_REPO,
            repo_type="dataset",
            commit_message=f"Add {model} to eval queue",
        )
    except Exception:
        os.remove(out_path)
        return response("error", "The model passed validation, but the queue could not be updated. Please try again.")

    REQUESTED_MODELS.add(request_key)
    return response(
        "success",
        "Your model is in the evaluation queue. It should appear in the pending list shortly.",
    )