Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
import json | |
import os | |
from datetime import datetime, timezone | |
from src.display.formatting import styled_error, styled_message, styled_warning | |
from src.envs import API, EVAL_REQUESTS_PATH, H4_TOKEN, QUEUE_REPO, RATE_LIMIT_PERIOD, RATE_LIMIT_QUOTA | |
from src.leaderboard.filter_models import DO_NOT_SUBMIT_MODELS | |
from src.submission.check_validity import ( | |
already_submitted_models, | |
check_model_card, | |
get_model_size, | |
is_model_on_hub, | |
user_submission_permission, | |
) | |
## it just uploads request file. where does the evaluation actually happen? | |
REQUESTED_MODELS = None | |
USERS_TO_SUBMISSION_DATES = None | |
def add_new_eval( | |
model: str, | |
requested_tasks: list, # write better type hints. this is list of class Task. | |
base_model: str, | |
revision: str, | |
precision: str, | |
private: bool, | |
weight_type: str, | |
model_type: str, | |
): | |
global REQUESTED_MODELS | |
global USERS_TO_SUBMISSION_DATES | |
if not REQUESTED_MODELS: | |
REQUESTED_MODELS, USERS_TO_SUBMISSION_DATES = already_submitted_models(EVAL_REQUESTS_PATH) | |
# REQUESTED_MODELS is set(file_names), where file_names.append(f"{info['model']}_{info['revision']}_{info['precision']}") | |
user_name = "" | |
model_path = model | |
if "/" in model: | |
user_name = model.split("/")[0] | |
model_path = model.split("/")[1] | |
precision = precision.split(" ")[0] | |
current_time = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") | |
if model_type is None or model_type == "": | |
return styled_error("Please select a model type.") | |
# Is the user rate limited? | |
if user_name != "": | |
user_can_submit, error_msg = user_submission_permission( | |
user_name, USERS_TO_SUBMISSION_DATES, RATE_LIMIT_PERIOD, RATE_LIMIT_QUOTA | |
) | |
if not user_can_submit: | |
return styled_error(error_msg) | |
# Did the model authors forbid its submission to the leaderboard? | |
if model in DO_NOT_SUBMIT_MODELS or base_model in DO_NOT_SUBMIT_MODELS: | |
return styled_warning("Model authors have requested that their model be not submitted on the leaderboard.") | |
# Does the model actually exist? | |
if revision == "": | |
revision = "main" | |
# Is the model on the hub? | |
if weight_type in ["Delta", "Adapter"]: | |
base_model_on_hub, error, _ = is_model_on_hub(model_name=base_model, revision=revision, token=H4_TOKEN, test_tokenizer=True) | |
if not base_model_on_hub: | |
return styled_error(f'Base model "{base_model}" {error}') | |
if not weight_type == "Adapter": | |
model_on_hub, error, _ = is_model_on_hub(model_name=model, revision=revision, test_tokenizer=True) | |
if not model_on_hub: | |
return styled_error(f'Model "{model}" {error}') | |
# Is the model info correctly filled? | |
try: | |
model_info = API.model_info(repo_id=model, revision=revision) | |
except Exception: | |
return styled_error("Could not get your model information. Please fill it up properly.") | |
model_size = get_model_size(model_info=model_info, precision=precision) | |
# Were the model card and license filled? | |
try: | |
license = model_info.cardData["license"] | |
except Exception: | |
return styled_error("Please select a license for your model") | |
modelcard_OK, error_msg = check_model_card(model) | |
if not modelcard_OK: | |
return styled_error(error_msg) | |
# Seems good, creating the eval | |
print("Adding new eval") | |
print() | |
print(f"requested_tasks: {requested_tasks}") | |
print(f"type(requested_tasks): {type(requested_tasks)}") | |
print() | |
# requested_tasks: [{'benchmark': 'hellaswag', 'metric': 'acc_norm', 'col_name': 'HellaSwag'}, {'benchmark': 'pubmedqa', 'metric': 'acc', 'col_name': 'PubMedQA'}] | |
# type(requested_tasks): <class 'list'> | |
requested_task_names = [task_dic['benchmark'] for task_dic in requested_tasks] | |
print() | |
print(f"requested_task_names: {requested_task_names}") | |
print(f"type(requested_task_names): {type(requested_task_names)}") | |
print() | |
already_submitted_tasks = [] | |
for requested_task_name in requested_task_names: | |
if f"{model}_{requested_task_name}_{revision}_{precision}" in REQUESTED_MODELS: | |
# return styled_warning("This model has been already submitted.") | |
already_submitted_tasks.append(requested_task_name) | |
task_names_for_eval = set(requested_task_names) - set(already_submitted_tasks) | |
task_names_for_eval = list(task_names_for_eval) | |
return_msg = "Your request has been submitted to the evaluation queue! Please wait for up to an hour for the model to show in the PENDING list." | |
if len(already_submitted_tasks) > 0: | |
return_msg = f"This model has been already submitted for task(s) {already_submitted_tasks}. Evaluation will proceed for tasks {task_names_for_eval}. Please wait for up to an hour for the model to show in the PENDING list." | |
if len(task_names_for_eval)==0: | |
return styled_warning(f"This model has been already submitted for task(s) {already_submitted_tasks}.") | |
tasks_for_eval = [dct for dct in requested_tasks if dct['benchmark'] in task_names_for_eval] | |
print() | |
print(f"tasks_for_eval: {tasks_for_eval}") | |
# print(f"type(requested_task_names): {type(requested_task_names)}") | |
print() | |
eval_entry = { | |
"model": model, | |
"requested_tasks": tasks_for_eval, # this is a list of tasks. would eval file be written correctly for each tasks? YES. run_evaluation() takes list of tasks. might have to specify | |
"base_model": base_model, | |
"revision": revision, | |
"private": private, | |
"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, | |
} | |
####---- ####---- ####---- ####---- ####---- ####---- ####---- ####---- ####---- ####---- ####---- ####---- ####---- ####---- | |
print("Creating eval file") | |
OUT_DIR = f"{EVAL_REQUESTS_PATH}/{user_name}" # local path | |
os.makedirs(OUT_DIR, exist_ok=True) | |
out_path = f"{OUT_DIR}/{model_path}_{'_'.join([f'{task}' for task in task_names_for_eval])}_eval_request_{private}_{precision}_{weight_type}.json" | |
print(f"out_path = {out_path}") | |
with open(out_path, "w") as f: | |
f.write(json.dumps(eval_entry)) # local path used! for saving request file. | |
print("Uploading eval file (QUEUE_REPO)") | |
print() | |
print(f"path_or_fileobj={out_path}, path_in_repo={out_path.split('eval-queue/')[1]}, repo_id={QUEUE_REPO}, repo_type=dataset,") | |
API.upload_file( | |
path_or_fileobj=out_path, | |
path_in_repo=out_path.split("eval-queue/")[1], | |
repo_id=QUEUE_REPO, | |
repo_type="dataset", | |
commit_message=f"Add {model} to eval queue", | |
) | |
print(f"is os.remove(out_path) the problem?") | |
# Remove the local file | |
os.remove(out_path) | |
return styled_message( | |
return_msg | |
) | |