rodrigomasini/data_only_hallucination_leaderboard
0
1#!/usr/bin/env python2 3import json4import os5import time6 7from datetime import datetime, timezone8 9from src.envs import API, EVAL_REQUESTS_PATH, H4_TOKEN, QUEUE_REPO10from src.submission.check_validity import already_submitted_models, get_model_size, is_model_on_hub11 12from huggingface_hub import snapshot_download13from src.backend.envs import EVAL_REQUESTS_PATH_BACKEND14from src.backend.manage_requests import get_eval_requests15from src.backend.manage_requests import EvalRequest16 17 18def add_new_eval(model: str, base_model: str, revision: str, precision: str, private: bool, weight_type: str, model_type: str):19 REQUESTED_MODELS, USERS_TO_SUBMISSION_DATES = already_submitted_models(EVAL_REQUESTS_PATH)20 21 user_name = ""22 model_path = model23 if "/" in model:24 tokens = model.split("/")25 user_name = tokens[0]26 model_path = tokens[1]27 28 precision = precision.split(" ")[0]29 current_time = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")30 31 if model_type is None or model_type == "":32 return print("Please select a model type.")33 34 # Does the model actually exist?35 if revision == "":36 revision = "main"37 38 # Is the model on the hub?39 if weight_type in ["Delta", "Adapter"]:40 base_model_on_hub, error, _ = is_model_on_hub(model_name=base_model, revision=revision, token=H4_TOKEN, test_tokenizer=True)41 if not base_model_on_hub:42 print(f'Base model "{base_model}" {error}')43 return44 45 if not weight_type == "Adapter":46 model_on_hub, error, _ = is_model_on_hub(model_name=model, revision=revision, test_tokenizer=True)47 if not model_on_hub:48 print(f'Model "{model}" {error}')49 return50 51 # Is the model info correctly filled?52 try:53 model_info = API.model_info(repo_id=model, revision=revision)54 except Exception:55 print("Could not get your model information. Please fill it up properly.")56 return57 58 model_size = get_model_size(model_info=model_info, precision=precision)59 60 license = 'none'61 try:62 license = model_info.cardData["license"]63 except Exception:64 print("Please select a license for your model")65 # return66 67 # modelcard_OK, error_msg = check_model_card(model)68 # if not modelcard_OK:69 # print(error_msg)70 # return71 72 # Seems good, creating the eval73 print("Adding new eval")74 75 eval_entry = {76 "model": model,77 "base_model": base_model,78 "revision": revision,79 "private": private,80 "precision": precision,81 "weight_type": weight_type,82 "status": "PENDING",83 "submitted_time": current_time,84 "model_type": model_type,85 "likes": model_info.likes,86 "params": model_size,87 "license": license,88 }89 90 # Check for duplicate submission91 if f"{model}_{revision}_{precision}" in REQUESTED_MODELS:92 print("This model has been already submitted.")93 return94 95 print("Creating eval file")96 OUT_DIR = f"{EVAL_REQUESTS_PATH}/{user_name}"97 os.makedirs(OUT_DIR, exist_ok=True)98 out_path = f"{OUT_DIR}/{model_path}_eval_request_{private}_{precision}_{weight_type}.json"99 100 with open(out_path, "w") as f:101 f.write(json.dumps(eval_entry))102 103 print("Uploading eval file")104 API.upload_file(path_or_fileobj=out_path, path_in_repo=out_path.split("eval-queue/")[1],105 repo_id=QUEUE_REPO, repo_type="dataset", commit_message=f"Add {model} to eval queue")106 107 # Remove the local file108 os.remove(out_path)109 110 print("Your request has been submitted to the evaluation queue!\nPlease wait for up to an hour for the model to show in the PENDING list.")111 return112 113 114def main():115 from huggingface_hub import HfApi116 117 api = HfApi()118 model_lst = api.list_models()119 120 model_lst = [m for m in model_lst]121 122 def custom_filter(m) -> bool:123 # res = m.pipeline_tag in {'text-generation'} and 'en' in m.tags and m.private is False124 # res = m.pipeline_tag in {'text-generation'} and 'en' in m.tags and m.private is False and 'mistralai/' in m.id125 res = 'mistralai/' in m.id126 return res127 128 filtered_model_lst = sorted([m for m in model_lst if custom_filter(m)], key=lambda m: m.downloads, reverse=True)129 130 snapshot_download(repo_id=QUEUE_REPO, revision="main", local_dir=EVAL_REQUESTS_PATH_BACKEND, repo_type="dataset", max_workers=60)131 132 PENDING_STATUS = "PENDING"133 RUNNING_STATUS = "RUNNING"134 FINISHED_STATUS = "FINISHED"135 FAILED_STATUS = "FAILED"136 137 status = [PENDING_STATUS, RUNNING_STATUS, FINISHED_STATUS, FAILED_STATUS]138 139 # Get all eval requests140 eval_requests: list[EvalRequest] = get_eval_requests(job_status=status, hf_repo=QUEUE_REPO, local_dir=EVAL_REQUESTS_PATH_BACKEND)141 142 requested_model_names = {e.model for e in eval_requests}143 144 # breakpoint()145 146 for i in range(min(200, len(filtered_model_lst))):147 model = filtered_model_lst[i]148 149 print(f'Considering {model.id} ..')150 151 is_finetuned = any(tag.startswith('base_model:') for tag in model.tags)152 153 model_type = 'pretrained'154 if is_finetuned:155 model_type = "fine-tuned"156 157 is_instruction_tuned = 'nstruct' in model.id158 if is_instruction_tuned:159 model_type = "instruction-tuned"160 161 if model.id not in requested_model_names:162 163 if 'mage' not in model.id:164 add_new_eval(model=model.id, base_model='', revision='main', precision='float32', private=False, weight_type='Original', model_type=model_type)165 time.sleep(10)166 else:167 print(f'Model {model.id} already added, not adding it to the queue again.')168 169 170if __name__ == "__main__":171 main()172 