hf-create-model.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python3
  2. from huggingface_hub import HfApi
  3. import argparse
  4. # This script requires that the environment variable HF_TOKEN is set with your
  5. # Hugging Face API token.
  6. api = HfApi()
  7. def load_template_and_substitute(template_path, **kwargs):
  8. try:
  9. with open(template_path, 'r', encoding='utf-8') as f:
  10. template_content = f.read()
  11. return template_content.format(**kwargs)
  12. except FileNotFoundError:
  13. print(f"Template file '{template_path}' not found!")
  14. return None
  15. except KeyError as e:
  16. print(f"Missing template variable: {e}")
  17. return None
  18. parser = argparse.ArgumentParser(description='Create a new Hugging Face model repository')
  19. parser.add_argument('--model-name', '-m', help='Name for the model', required=True)
  20. parser.add_argument('--namespace', '-ns', help='Namespace to add the model to', required=True)
  21. parser.add_argument('--org-base-model', '-b', help='Original Base model name', default="")
  22. parser.add_argument('--no-card', action='store_true', help='Skip creating model card')
  23. parser.add_argument('--private', '-p', action='store_true', help='Create private model')
  24. args = parser.parse_args()
  25. repo_id = f"{args.namespace}/{args.model_name}-GGUF"
  26. print("Repository ID: ", repo_id)
  27. repo_url = api.create_repo(
  28. repo_id=repo_id,
  29. repo_type="model",
  30. private=args.private,
  31. exist_ok=False
  32. )
  33. if not args.no_card:
  34. template_path = "scripts/readme.md.template"
  35. model_card_content = load_template_and_substitute(
  36. template_path,
  37. model_name=args.model_name,
  38. namespace=args.namespace,
  39. base_model=args.org_base_model,
  40. )
  41. if model_card_content:
  42. api.upload_file(
  43. path_or_fileobj=model_card_content.encode('utf-8'),
  44. path_in_repo="README.md",
  45. repo_id=repo_id
  46. )
  47. print("Model card created successfully.")
  48. else:
  49. print("Failed to create model card.")
  50. print(f"Repository created: {repo_url}")