|
import json |
|
import os |
|
from typing import Any |
|
|
|
import huggingface_hub |
|
from huggingface_hub import ModelCard |
|
from huggingface_hub.hf_api import ModelInfo |
|
from transformers import AutoConfig |
|
from transformers.models.auto.tokenization_auto import AutoTokenizer |
|
|
|
|
|
def check_model_card(repo_id: str) -> tuple[bool, str]: |
|
"""Checks if the model card and license exist and have been filled""" |
|
try: |
|
card = ModelCard.load(repo_id) |
|
except huggingface_hub.utils.EntryNotFoundError: |
|
return False, "Please add a model card to your model to explain how you trained/fine-tuned it." |
|
|
|
|
|
if card.data.license is None: |
|
if not ("license_name" in card.data and "license_link" in card.data): |
|
return False, ( |
|
"License not found. Please add a license to your model card using the `license` metadata or a" |
|
" `license_name`/`license_link` pair." |
|
) |
|
|
|
|
|
if len(card.text) < 200: |
|
return False, "Please add a description to your model card, it is too short." |
|
|
|
return True, "" |
|
|
|
def is_model_on_hub(model_name: str, model_args: dict = None, token: str = None, test_tokenizer=False) -> tuple[bool, str, Any]: |
|
"""Checks if the model model_name is on the hub, and whether it (and its tokenizer) can be loaded with AutoClasses.""" |
|
model_args = model_args or {} |
|
try: |
|
config = AutoConfig.from_pretrained(model_name, token=token, **model_args) |
|
if test_tokenizer: |
|
try: |
|
tk = AutoTokenizer.from_pretrained(model_name, token=token, **model_args) |
|
except ValueError as e: |
|
return ( |
|
False, |
|
f"uses a tokenizer which is not in a transformers release: {e}", |
|
None |
|
) |
|
except Exception as e: |
|
return (False, "'s tokenizer cannot be loaded. Is your tokenizer class in a stable transformers release, and correctly configured?", None) |
|
return True, None, config |
|
|
|
except ValueError: |
|
return ( |
|
False, |
|
"needs to be launched with `trust_remote_code=True`. For safety reason, we do not allow these models to be automatically submitted to the leaderboard.", |
|
None |
|
) |
|
|
|
except Exception as e: |
|
return False, "was not found on hub!", None |
|
|
|
|
|
def get_model_size(model_info: ModelInfo, precision: str): |
|
"""Gets the model size from the configuration, or the model name if the configuration does not contain the information.""" |
|
try: |
|
model_size = model_info.safetensors["total"] |
|
except (AttributeError, TypeError): |
|
return 0 |
|
|
|
size_factor = 8 if (precision == "GPTQ" or "gptq" in model_info.modelId.lower()) else 1 |
|
model_size = size_factor * model_size |
|
return model_size |
|
|
|
def get_model_arch(model_info: ModelInfo): |
|
"""Gets the model architecture from the configuration""" |
|
return model_info.config.get("architectures", "Unknown") |
|
|
|
def get_model_properties(configuration: dict) -> tuple[str, str, str, int, str, int]: |
|
model_name = configuration["model_name_sanitized"] |
|
revision = configuration["config"]["model_revision"] |
|
precision = configuration["config"]["model_dtype"].split(".")[-1] |
|
seed = configuration["config"]["random_seed"] |
|
n_shot = list(configuration["n-shot"].values())[0] |
|
prompt_version = list(configuration["versions"].values())[0] |
|
return model_name, revision, precision, seed, prompt_version, n_shot |
|
|
|
def already_submitted_models(requested_models_dir: str) -> set[str]: |
|
"""Gather a list of already submitted models to avoid duplicates""" |
|
depth = 1 |
|
run_names = [] |
|
|
|
for root, _, files in os.walk(requested_models_dir): |
|
current_depth = root.count(os.sep) - requested_models_dir.count(os.sep) |
|
if current_depth == depth: |
|
for file in files: |
|
if not file.endswith(".json"): |
|
continue |
|
with open(os.path.join(root, file), "r") as f: |
|
info = json.load(f) |
|
|
|
properties = get_model_properties(info) |
|
run_names.append("_".join([str(property) for property in properties])) |
|
|
|
return set(run_names) |
|
|