SolsticeAI commited on
Commit
f6d8b49
·
verified ·
1 Parent(s): 89ebd10

Delete apply_template.py

Browse files
Files changed (1) hide show
  1. apply_template.py +0 -103
apply_template.py DELETED
@@ -1,103 +0,0 @@
1
- #!/usr/bin/env python3
2
- """
3
- Protium-1.0 Chat Template Applier
4
- Usage:
5
- python apply_template.py --template universal --local-dir ./path/to/model
6
- python apply_template.py --template deepseek --model-id Solstice-AI/my-model --push
7
- """
8
-
9
- import argparse
10
- import json
11
- import os
12
- import sys
13
- from huggingface_hub import HfApi, hf_hub_download
14
-
15
- TEMPLATES = {
16
- "universal": "templates/universal.jinja",
17
- "deepseek": "templates/deepseek.jinja",
18
- "qwen": "templates/qwen.jinja",
19
- "glm": "templates/glm.jinja",
20
- "llama3": "templates/llama3.jinja",
21
- "llama": "templates/llama3.jinja",
22
- "mistral": "templates/mistral.jinja",
23
- "gemma": "templates/gemma.jinja",
24
- }
25
-
26
- def load_template(name):
27
- base_dir = os.path.dirname(os.path.abspath(__file__))
28
- rel_path = TEMPLATES.get(name.lower())
29
- if not rel_path:
30
- raise ValueError(f"Unknown template: {name}. Available: {list(TEMPLATES.keys())}")
31
- full_path = os.path.join(base_dir, rel_path)
32
- with open(full_path, "r", encoding="utf-8") as f:
33
- return f.read()
34
-
35
- def apply_local(local_dir, template_name):
36
- tc_path = os.path.join(local_dir, "tokenizer_config.json")
37
- if not os.path.exists(tc_path):
38
- print(f"Error: {tc_path} not found.")
39
- sys.exit(1)
40
-
41
- template_code = load_template(template_name)
42
- with open(tc_path, "r", encoding="utf-8") as f:
43
- tc = json.load(f)
44
-
45
- tc["chat_template"] = template_code
46
- with open(tc_path, "w", encoding="utf-8") as f:
47
- json.dump(tc, f, indent=2, ensure_ascii=False)
48
- f.write("\n")
49
-
50
- # Also save chat_template.jinja standalone
51
- jinja_path = os.path.join(local_dir, "chat_template.jinja")
52
- with open(jinja_path, "w", encoding="utf-8") as f:
53
- f.write(template_code)
54
-
55
- print(f" Successfully applied {template_name} template to {local_dir}")
56
-
57
- def apply_hub(model_id, template_name, push=False, token=None):
58
- api = HfApi(token=token)
59
- print(f"Downloading tokenizer_config.json from {model_id}...")
60
- tc_path = hf_hub_download(repo_id=model_id, filename="tokenizer_config.json", token=token)
61
- with open(tc_path, "r", encoding="utf-8") as f:
62
- tc = json.load(f)
63
-
64
- template_code = load_template(template_name)
65
- tc["chat_template"] = template_code
66
-
67
- if push:
68
- print(f"Pushing updated tokenizer_config.json and chat_template.jinja to {model_id}...")
69
- api.upload_file(
70
- path_or_fileobj=json.dumps(tc, indent=2, ensure_ascii=False).encode("utf-8"),
71
- path_in_repo="tokenizer_config.json",
72
- repo_id=model_id,
73
- commit_message=f"feat: apply Protium-1.0 {template_name} chat template"
74
- )
75
- api.upload_file(
76
- path_or_fileobj=template_code.encode("utf-8"),
77
- path_in_repo="chat_template.jinja",
78
- repo_id=model_id,
79
- commit_message=f"feat: add Protium-1.0 {template_name} chat_template.jinja"
80
- )
81
- print(f" Successfully pushed to {model_id}!")
82
- else:
83
- print("Dry run completed. Pass --push to commit changes to Hugging Face Hub.")
84
-
85
- def main():
86
- parser = argparse.ArgumentParser(description="Apply Protium-1.0 Chat Templates")
87
- parser.add_argument("--template", "-t", required=True, choices=list(TEMPLATES.keys()), help="Template family to apply")
88
- parser.add_argument("--local-dir", "-l", help="Path to local model directory")
89
- parser.add_argument("--model-id", "-m", help="Hugging Face Model ID (e.g. org/model)")
90
- parser.add_argument("--push", action="store_true", help="Push to Hugging Face Hub")
91
- parser.add_argument("--token", help="Hugging Face User Access Token")
92
-
93
- args = parser.parse_args()
94
- if args.local_dir:
95
- apply_local(args.local_dir, args.template)
96
- elif args.model_id:
97
- apply_hub(args.model_id, args.template, push=args.push, token=args.token)
98
- else:
99
- print("Please provide either --local-dir or --model-id.")
100
- sys.exit(1)
101
-
102
- if __name__ == "__main__":
103
- main()