rodrigomasini/data_only_hallucination_leaderboard
0
1import json2import os3import pprint4import re5from datetime import datetime, timezone6 7import click8from colorama import Fore9from huggingface_hub import HfApi, snapshot_download10 11EVAL_REQUESTS_PATH = "eval-queue"12QUEUE_REPO = "hallucinations-leaderboard/requests"13 14precisions = ("float16", "bfloat16", "8bit (LLM.int8)", "4bit (QLoRA / FP4)", "GPTQ")15model_types = ("pretrained", "fine-tuned", "RL-tuned", "instruction-tuned")16weight_types = ("Original", "Delta", "Adapter")17 18 19def get_model_size(model_info, precision: str):20 size_pattern = size_pattern = re.compile(r"(\d\.)?\d+(b|m)")21 try:22 model_size = round(model_info.safetensors["total"] / 1e9, 3)23 except (AttributeError, TypeError):24 try:25 size_match = re.search(size_pattern, model_info.modelId.lower())26 model_size = size_match.group(0)27 model_size = round(float(model_size[:-1]) if model_size[-1] == "b" else float(model_size[:-1]) / 1e3, 3)28 except AttributeError:29 return 0 # Unknown model sizes are indicated as 0, see NUMERIC_INTERVALS in app.py30 31 size_factor = 8 if (precision == "GPTQ" or "gptq" in model_info.modelId.lower()) else 132 model_size = size_factor * model_size33 return model_size34 35 36def main():37 api = HfApi()38 current_time = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")39 snapshot_download(repo_id=QUEUE_REPO, revision="main", local_dir=EVAL_REQUESTS_PATH, repo_type="dataset")40 41 model_name = click.prompt("Enter model name")42 revision = click.prompt("Enter revision", default="main")43 precision = click.prompt("Enter precision", default="float32", type=click.Choice(precisions))44 model_type = click.prompt("Enter model type", type=click.Choice(model_types))45 weight_type = click.prompt("Enter weight type", default="Original", type=click.Choice(weight_types))46 base_model = click.prompt("Enter base model", default="")47 status = click.prompt("Enter status", default="FINISHED")48 49 try:50 model_info = api.model_info(repo_id=model_name, revision=revision)51 except Exception as e:52 print(f"{Fore.RED}Could not find model info for {model_name} on the Hub\n{e}{Fore.RESET}")53 return 154 55 model_size = get_model_size(model_info=model_info, precision=precision)56 57 try:58 license = model_info.cardData["license"]59 except Exception:60 license = "?"61 62 eval_entry = {63 "model": model_name,64 "base_model": base_model,65 "revision": revision,66 "private": False,67 "precision": precision,68 "weight_type": weight_type,69 "status": status,70 "submitted_time": current_time,71 "model_type": model_type,72 "likes": model_info.likes,73 "params": model_size,74 "license": license,75 }76 77 user_name = ""78 model_path = model_name79 if "/" in model_name:80 user_name = model_name.split("/")[0]81 model_path = model_name.split("/")[1]82 83 pprint.pprint(eval_entry)84 85 if click.confirm("Do you want to continue? This request file will be pushed to the hub"):86 click.echo("continuing...")87 88 out_dir = f"{EVAL_REQUESTS_PATH}/{user_name}"89 os.makedirs(out_dir, exist_ok=True)90 out_path = f"{out_dir}/{model_path}_eval_request_{False}_{precision}_{weight_type}.json"91 92 with open(out_path, "w") as f:93 f.write(json.dumps(eval_entry))94 95 api.upload_file(96 path_or_fileobj=out_path,97 path_in_repo=out_path.split(f"{EVAL_REQUESTS_PATH}/")[1],98 repo_id=QUEUE_REPO,99 repo_type="dataset",100 commit_message=f"Add {model_name} to eval queue",101 )102 else:103 click.echo("aborting...")104 105 106if __name__ == "__main__":107 main()108 