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