1#!/usr/bin/env python3
 2
 3from huggingface_hub import HfApi
 4import argparse
 5
 6# This script requires that the environment variable HF_TOKEN is set with your
 7# Hugging Face API token.
 8api = HfApi()
 9
10def load_template_and_substitute(template_path, **kwargs):
11    try:
12        with open(template_path, 'r', encoding='utf-8') as f:
13            template_content = f.read()
14
15        return template_content.format(**kwargs)
16    except FileNotFoundError:
17        print(f"Template file '{template_path}' not found!")
18        return None
19    except KeyError as e:
20        print(f"Missing template variable: {e}")
21        return None
22
23parser = argparse.ArgumentParser(description='Create a new Hugging Face model repository')
24parser.add_argument('--model-name', '-m', help='Name for the model', required=True)
25parser.add_argument('--namespace', '-ns', help='Namespace to add the model to', required=True)
26parser.add_argument('--org-base-model', '-b', help='Original Base model name', default="")
27parser.add_argument('--no-card', action='store_true', help='Skip creating model card')
28parser.add_argument('--private', '-p', action='store_true', help='Create private model')
29parser.add_argument('--embedding', '-e', action='store_true', help='Use embedding model card template')
30parser.add_argument('--dry-run', '-d', action='store_true', help='Print repository info and template without creating repository')
31
32args = parser.parse_args()
33
34repo_id = f"{args.namespace}/{args.model_name}-GGUF"
35print("Repository ID: ", repo_id)
36
37repo_url = None
38if not args.dry_run:
39    repo_url = api.create_repo(
40        repo_id=repo_id,
41        repo_type="model",
42        private=args.private,
43        exist_ok=False
44    )
45
46if not args.no_card:
47    if args.embedding:
48        template_path = "scripts/embedding/modelcard.template"
49    else:
50        template_path = "scripts/causal/modelcard.template"
51
52    print("Template path: ", template_path)
53
54    model_card_content = load_template_and_substitute(
55        template_path,
56        model_name=args.model_name,
57        namespace=args.namespace,
58        base_model=args.org_base_model,
59    )
60
61    if args.dry_run:
62        print("\nTemplate Content:\n")
63        print(model_card_content)
64    else:
65        if model_card_content:
66            api.upload_file(
67                path_or_fileobj=model_card_content.encode('utf-8'),
68                path_in_repo="README.md",
69                repo_id=repo_id
70            )
71            print("Model card created successfully.")
72        else:
73            print("Failed to create model card.")
74
75if not args.dry_run and repo_url:
76    print(f"Repository created: {repo_url}")
77
78