-
Notifications
You must be signed in to change notification settings - Fork 3
224 lines (195 loc) · 7.84 KB
/
create-model-yml.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
name: Create HuggingFace Model Repository
on:
workflow_dispatch:
inputs:
model_name:
description: "Name of the model to create (will be used in repo name and files)"
required: true
type: string
prompt_template:
description: "Prompt template for the model"
required: true
type: string
default: "<|im_start|>system\n{system_message}<|im_end|>\n<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant"
stop_tokens:
description: "Stop tokens for the model (comma-separated, e.g., <|im_end|>,</s>)"
required: true
type: string
default: "<|im_end|>"
engine:
description: "Engine to run the model (e.g., llama-cpp)"
required: true
type: string
default: "llama-cpp"
env:
USER_NAME: cortexso
MODEL_NAME: ${{ inputs.model_name }}
PROMPT_TEMPLATE: ${{ inputs.prompt_template }}
STOP_TOKENS: ${{ inputs.stop_tokens }}
ENGINE: ${{ inputs.engine }}
jobs:
create-repo:
runs-on: ubuntu-20-04-gguf
timeout-minutes: 7200
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Cache Python packages
uses: actions/cache@v4
with:
path: |
~/.cache/pip
~/.local/share/pip
.venv
key: ${{ runner.os }}-pip-${{ github.sha }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
pip install huggingface_hub PyYAML
git lfs install
- name: Create YAML files
run: |
python3 - << EOF
import yaml
import os
from yaml.representer import SafeRepresenter
class CustomDumper(yaml.SafeDumper):
pass
def custom_str_representer(dumper, data):
if isinstance(data, str):
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='')
return SafeRepresenter.represent_str(dumper, data)
CustomDumper.add_representer(str, custom_str_representer)
def dict_representer(dumper, data):
return dumper.represent_dict(data.items())
CustomDumper.add_representer(dict, dict_representer)
# Process stop tokens
stop_tokens = os.environ['STOP_TOKENS'].split(',')
stop_tokens = [token.strip() for token in stop_tokens if token.strip()]
# Create model.yml content with sections
def write_section(file, content, section_name='', subsection=''):
if section_name:
file.write(f"# BEGIN {section_name}\n")
if subsection:
file.write(f"# BEGIN {subsection}\n")
# Special handling for prompt_template to ensure it's a single line with preserved \n
if 'prompt_template' in content:
# Create a copy of the content to modify
modified_content = content.copy()
# Convert the prompt template to a literal string that preserves \n
prompt_template = modified_content['prompt_template']
# Remove the prompt_template from the content to handle it separately
del modified_content['prompt_template']
# Dump the rest of the content
yaml_content = yaml.dump(modified_content, Dumper=CustomDumper, default_flow_style=False, sort_keys=False)
file.write(yaml_content)
# Write the prompt_template separately with proper escaping
file.write(f'prompt_template: "{prompt_template}"\n')
else:
yaml_content = yaml.dump(content, Dumper=CustomDumper, default_flow_style=False, sort_keys=False)
file.write(yaml_content)
if subsection:
file.write(f"# END {subsection}\n")
if section_name:
file.write(f"# END {section_name}\n")
with open('model.yml', 'w') as f:
# General metadata section
general_metadata = {
'id': os.environ['MODEL_NAME'],
'model': os.environ['MODEL_NAME'],
'name': os.environ['MODEL_NAME'],
'version': 1
}
write_section(f, general_metadata, 'GENERAL GGUF METADATA')
f.write('\n')
# Inference parameters section
f.write("# BEGIN INFERENCE PARAMETERS\n")
# Required subsection
required_inference = {'stop': stop_tokens}
write_section(f, required_inference, '', 'REQUIRED')
f.write('\n')
# Optional subsection
optional_inference = {
'stream': True,
'top_p': 0.9,
'temperature': 0.7,
'frequency_penalty': 0,
'presence_penalty': 0,
'max_tokens': 4096,
'seed': -1,
'dynatemp_range': 0,
'dynatemp_exponent': 1,
'top_k': 40,
'min_p': 0.05,
'tfs_z': 1,
'typ_p': 1,
'repeat_last_n': 64,
'repeat_penalty': 1,
'mirostat': False,
'mirostat_tau': 5,
'mirostat_eta': 0.100000001,
'penalize_nl': False,
'ignore_eos': False,
'n_probs': 0,
'min_keep': 0
}
write_section(f, optional_inference, '', 'OPTIONAL')
f.write("# END INFERENCE PARAMETERS\n\n")
# Model load parameters section
f.write("# BEGIN MODEL LOAD PARAMETERS\n")
required_load = {
'engine': os.environ['ENGINE'],
'prompt_template': os.environ['PROMPT_TEMPLATE'],
'ctx_len': 4096,
'ngl': 34
}
write_section(f, required_load, '', 'REQUIRED')
f.write("# END MODEL LOAD PARAMETERS\n")
# Create metadata.yml
metadata_content = {
'version': 1,
'name': os.environ['MODEL_NAME'],
'default': '8b-gguf-q4-km'
}
with open('metadata.yml', 'w') as f:
write_section(f, metadata_content)
EOF
- name: Create HuggingFace Repository and Upload Files
env:
HF_TOKEN: ${{ secrets.HUGGINGFACE_TOKEN_WRITE }}
run: |
python3 - << EOF
from huggingface_hub import HfApi, create_repo
import os
# Initialize the Hugging Face API
api = HfApi(token=os.environ['HF_TOKEN'])
# Create the repository
repo_id = f"${{ env.USER_NAME }}/${{ env.MODEL_NAME }}"
try:
create_repo(repo_id, private=False, token=os.environ['HF_TOKEN'])
print(f"Created repository: {repo_id}")
except Exception as e:
print(f"Repository might already exist or error occurred: {e}")
# Upload the files
api.upload_file(
path_or_fileobj="model.yml",
path_in_repo="model.yml",
repo_id=repo_id,
token=os.environ['HF_TOKEN']
)
api.upload_file(
path_or_fileobj="metadata.yml",
path_in_repo="metadata.yml",
repo_id=repo_id,
token=os.environ['HF_TOKEN']
)
print("Files uploaded successfully")
EOF
- name: Cleanup
run: |
rm -f model.yml metadata.yml