Spaces:
Runtime error
Runtime error
Matt
commited on
Commit
·
e300032
1
Parent(s):
c078aee
Add defaults
Browse files
app.py
CHANGED
|
@@ -11,13 +11,88 @@ demo_conversation = """[
|
|
| 11 |
{"role": "user", "content": "Can I ask a question?"}
|
| 12 |
]"""
|
| 13 |
|
| 14 |
-
|
|
|
|
| 15 |
{{ "<|im_start|>" + message["role"] + "\\n" + message["content"] + "<|im_end|>\\n" }}
|
| 16 |
{% endfor %}
|
| 17 |
{% if add_generation_prompt %}
|
| 18 |
{{ "<|im_start|>assistant\\n" }}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
{% endif %}"""
|
| 20 |
-
|
| 21 |
description_text = """# Chat Template Creator
|
| 22 |
|
| 23 |
### This space is a helper app for writing [Chat Templates](https://huggingface.co/docs/transformers/main/en/chat_templating).
|
|
@@ -40,6 +115,9 @@ def apply_chat_template(template, test_conversation, add_generation_prompt, clea
|
|
| 40 |
formatted = tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=add_generation_prompt)
|
| 41 |
return formatted, pr_snippet
|
| 42 |
|
|
|
|
|
|
|
|
|
|
| 43 |
with gr.Blocks() as demo:
|
| 44 |
|
| 45 |
gr.Markdown(description_text)
|
|
@@ -47,13 +125,13 @@ with gr.Blocks() as demo:
|
|
| 47 |
with gr.Row():
|
| 48 |
gr.Markdown("### Pick an existing template to start:")
|
| 49 |
with gr.Row():
|
| 50 |
-
gr.Button("ChatML")
|
| 51 |
-
gr.Button("Zephyr")
|
| 52 |
-
gr.Button("LLaMA")
|
| 53 |
with gr.Row():
|
| 54 |
-
gr.Button("Alpaca")
|
| 55 |
-
gr.Button("Vicuna")
|
| 56 |
-
gr.Button("
|
| 57 |
with gr.Row():
|
| 58 |
with gr.Column():
|
| 59 |
template_in = gr.TextArea(value=default_template, lines=10, max_lines=30, label="Chat Template")
|
|
@@ -64,10 +142,17 @@ with gr.Blocks() as demo:
|
|
| 64 |
with gr.Column():
|
| 65 |
formatted_out = gr.TextArea(label="Formatted conversation")
|
| 66 |
code_snippet_out = gr.TextArea(label="Code snippet to create PR", lines=3, show_label=True, show_copy_button=True)
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
demo.launch()
|
| 73 |
|
|
|
|
| 11 |
{"role": "user", "content": "Can I ask a question?"}
|
| 12 |
]"""
|
| 13 |
|
| 14 |
+
chat_templates = {
|
| 15 |
+
"chatml": """{% for message in messages %}
|
| 16 |
{{ "<|im_start|>" + message["role"] + "\\n" + message["content"] + "<|im_end|>\\n" }}
|
| 17 |
{% endfor %}
|
| 18 |
{% if add_generation_prompt %}
|
| 19 |
{{ "<|im_start|>assistant\\n" }}
|
| 20 |
+
{% endif %}""",
|
| 21 |
+
"zephyr": """{% for message in messages %}
|
| 22 |
+
{% if message['role'] == 'user' %}
|
| 23 |
+
{{ '<|user|>\n' + message['content'] + eos_token }}
|
| 24 |
+
{% elif message['role'] == 'system' %}
|
| 25 |
+
{{ '<|system|>\n' + message['content'] + eos_token }}
|
| 26 |
+
{% elif message['role'] == 'assistant' %}
|
| 27 |
+
{{ '<|assistant|>\n' + message['content'] + eos_token }}
|
| 28 |
+
{% endif %}
|
| 29 |
+
{% if loop.last and add_generation_prompt %}
|
| 30 |
+
{{ '<|assistant|>' }}
|
| 31 |
+
{% endif %}
|
| 32 |
+
{% endfor %}""",
|
| 33 |
+
"llama": """{% if messages[0]['role'] == 'system' %}
|
| 34 |
+
{% set loop_messages = messages[1:] %}
|
| 35 |
+
{% set system_message = messages[0]['content'] %}
|
| 36 |
+
{% else %}
|
| 37 |
+
{% set loop_messages = messages %}
|
| 38 |
+
{% set system_message = false %}
|
| 39 |
+
{% endif %}
|
| 40 |
+
{% for message in loop_messages %}
|
| 41 |
+
{% if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}
|
| 42 |
+
{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}
|
| 43 |
+
{% endif %}
|
| 44 |
+
{% if loop.index0 == 0 and system_message != false %}
|
| 45 |
+
{% set content = '<<SYS>>\\n' + system_message + '\\n<</SYS>>\\n\\n' + message['content'] %}
|
| 46 |
+
{% else %}
|
| 47 |
+
{% set content = message['content'] %}
|
| 48 |
+
{% endif %}
|
| 49 |
+
{% if message['role'] == 'user' %}
|
| 50 |
+
{{ bos_token + '[INST] ' + content.strip() + ' [/INST]' }}
|
| 51 |
+
{% elif message['role'] == 'assistant' %}
|
| 52 |
+
{{ ' ' + content.strip() + ' ' + eos_token }}
|
| 53 |
+
{% endif %}
|
| 54 |
+
{% endfor %}"""
|
| 55 |
+
"alpaca": """{% for message in messages %}
|
| 56 |
+
{% if message['role'] == 'system' %}
|
| 57 |
+
{{ message['content'] + '\n\n' }}
|
| 58 |
+
{% elif message['role'] == 'user' %}
|
| 59 |
+
{{ '### Instruction:\n' + message['content'] + '\n\n' }}
|
| 60 |
+
{% elif message['role'] == 'assistant' %}
|
| 61 |
+
{{ '### Response:\n' + message['content'] + '\n\n' }}
|
| 62 |
+
{% endif %}
|
| 63 |
+
{% if loop.last and add_generation_prompt %}
|
| 64 |
+
{{ '### Response:\n' }}
|
| 65 |
+
{% endif %}
|
| 66 |
+
{% endfor %}""",
|
| 67 |
+
"vicuna": """{% for message in messages %}
|
| 68 |
+
{% if message['role'] == 'system' %}
|
| 69 |
+
{{ message['content'] + '\n' }}
|
| 70 |
+
{% elif message['role'] == 'user' %}
|
| 71 |
+
{{ 'USER:\n' + message['content'] + '\n' }}
|
| 72 |
+
{% elif message['role'] == 'assistant' %}
|
| 73 |
+
{{ 'ASSISTANT:\n' + message['content'] + '\n' }}
|
| 74 |
+
{% endif %}
|
| 75 |
+
{% if loop.last and add_generation_prompt %}
|
| 76 |
+
{{ 'ASSISTANT:\n' }}
|
| 77 |
+
{% endif %}
|
| 78 |
+
{% endfor %}""",
|
| 79 |
+
"falcon": """{% for message in messages %}
|
| 80 |
+
{% if not loop.first %}
|
| 81 |
+
{{ '\n' }}
|
| 82 |
+
{% endif %}
|
| 83 |
+
{% if message['role'] == 'system' %}
|
| 84 |
+
{{ 'System: ' }}
|
| 85 |
+
{% elif message['role'] == 'user' %}
|
| 86 |
+
{{ 'User: ' }}
|
| 87 |
+
{% elif message['role'] == 'assistant' %}
|
| 88 |
+
{{ 'Falcon: ' }}
|
| 89 |
+
{% endif %}
|
| 90 |
+
{{ message['content'] }}
|
| 91 |
+
{% endfor %}
|
| 92 |
+
{% if add_generation_prompt %}
|
| 93 |
+
{{ '\n' + 'Falcon:' }}
|
| 94 |
{% endif %}"""
|
| 95 |
+
}
|
| 96 |
description_text = """# Chat Template Creator
|
| 97 |
|
| 98 |
### This space is a helper app for writing [Chat Templates](https://huggingface.co/docs/transformers/main/en/chat_templating).
|
|
|
|
| 115 |
formatted = tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=add_generation_prompt)
|
| 116 |
return formatted, pr_snippet
|
| 117 |
|
| 118 |
+
def load_template(template_name):
|
| 119 |
+
template_in.value = chat_templates[template_name]
|
| 120 |
+
|
| 121 |
with gr.Blocks() as demo:
|
| 122 |
|
| 123 |
gr.Markdown(description_text)
|
|
|
|
| 125 |
with gr.Row():
|
| 126 |
gr.Markdown("### Pick an existing template to start:")
|
| 127 |
with gr.Row():
|
| 128 |
+
load_chatml = gr.Button("ChatML")
|
| 129 |
+
load_zephyr = gr.Button("Zephyr")
|
| 130 |
+
load_llama = gr.Button("LLaMA")
|
| 131 |
with gr.Row():
|
| 132 |
+
load_alpaca = gr.Button("Alpaca")
|
| 133 |
+
load_vicuna = gr.Button("Vicuna")
|
| 134 |
+
load_falcon = gr.Button("Falcon")
|
| 135 |
with gr.Row():
|
| 136 |
with gr.Column():
|
| 137 |
template_in = gr.TextArea(value=default_template, lines=10, max_lines=30, label="Chat Template")
|
|
|
|
| 142 |
with gr.Column():
|
| 143 |
formatted_out = gr.TextArea(label="Formatted conversation")
|
| 144 |
code_snippet_out = gr.TextArea(label="Code snippet to create PR", lines=3, show_label=True, show_copy_button=True)
|
| 145 |
+
submit.click(fn=apply_chat_template,
|
| 146 |
+
inputs=[template_in, conversation_in, generation_prompt_check, cleanup_whitespace_check],
|
| 147 |
+
outputs=[formatted_out, code_snippet_out]
|
| 148 |
+
)
|
| 149 |
+
load_chatml.click(fn=load_template, "chatml")
|
| 150 |
+
load_zephyr.click(fn=load_template, "zephyr")
|
| 151 |
+
load_llama.click(fn=load_template, "llama")
|
| 152 |
+
load_alpaca.click(fn=load_template, "alpaca")
|
| 153 |
+
load_vicuna.click(fn=load_template, "vicuna")
|
| 154 |
+
load_falcon.click(fn=load_template, "falcon")
|
| 155 |
+
|
| 156 |
|
| 157 |
demo.launch()
|
| 158 |
|