Spaces:
Sleeping
Sleeping
Download model_card.py from salihfurkaan/model-merger: direct link, hf CLI and curl.
- Browser
- Download file 2.41 kB
-
https://huggingface.co/spaces/salihfurkaan/model-merger/resolve/main/model_card.py
- Command line
-
hf download hf://spaces/salihfurkaan/model-merger/model_card.py
-
curl -L -o model_card.py https://huggingface.co/spaces/salihfurkaan/model-merger/resolve/main/model_card.py
2.41 kB
| """Generate Hugging Face model cards for merged models.""" | |
| import yaml | |
| from jinja2 import Template | |
| MODEL_CARD_TEMPLATE = """ | |
| --- | |
| license: {{ license }} | |
| base_model: | |
| {%- for model in models %} | |
| - {{ model }} | |
| {%- endfor %} | |
| tags: | |
| - merge | |
| - mergekit | |
| - lazymergekit | |
| {%- for model in models %} | |
| - {{ model }} | |
| {%- endfor %} | |
| --- | |
| # {{ model_name }} | |
| {{ model_name }} is a merge of the following models using [Model Merger](https://huggingface.co/spaces/salihfurkaan/model-merger): | |
| {%- for model in models %} | |
| * [{{ model }}](https://huggingface.co/{{ model }}) | |
| {%- endfor %} | |
| ## Configuration | |
| ```yaml | |
| {{ yaml_config }} | |
| ``` | |
| ## Usage | |
| ```python | |
| pip install -U transformers accelerate | |
| from transformers import AutoTokenizer | |
| import transformers | |
| import torch | |
| model = "{{ username }}/{{ model_name }}" | |
| messages = [{"role": "user", "content": "What is a large language model?"}] | |
| tokenizer = AutoTokenizer.from_pretrained(model) | |
| prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) | |
| pipeline = transformers.pipeline( | |
| "text-generation", | |
| model=model, | |
| torch_dtype=torch.float16, | |
| device_map="auto", | |
| ) | |
| outputs = pipeline(prompt, max_new_tokens=256, do_sample=True, temperature=0.7, top_k=50, top_p=0.95) | |
| print(outputs[0]["generated_text"]) | |
| ``` | |
| """.strip() | |
| def extract_models(yaml_config: str) -> list[str]: | |
| data = yaml.safe_load(yaml_config) | |
| if "models" in data: | |
| return [ | |
| data["models"][i]["model"] | |
| for i in range(len(data["models"])) | |
| if "model" in data["models"][i] | |
| ] | |
| if "slices" in data and data["slices"]: | |
| sources = data["slices"][0].get("sources", []) | |
| if sources: | |
| return [s["model"] for s in sources if "model" in s] | |
| return [data["slices"][i]["sources"][0]["model"] for i in range(len(data["slices"]))] | |
| if "experts" in data: | |
| return [expert["source_model"] for expert in data["experts"]] | |
| raise ValueError("No models or slices found in yaml config") | |
| def generate_model_card( | |
| model_name: str, | |
| yaml_config: str, | |
| username: str, | |
| license: str = "apache-2.0", | |
| ) -> str: | |
| models = extract_models(yaml_config) | |
| template = Template(MODEL_CARD_TEMPLATE) | |
| return template.render( | |
| model_name=model_name, | |
| models=models, | |
| yaml_config=yaml_config.strip(), | |
| username=username, | |
| license=license, | |
| ) | |