Spaces:
Runtime error
Runtime error
osanseviero
commited on
Commit
•
e878c76
1
Parent(s):
21346d9
Misc proposals
Browse files- Move INterface to Blocks
- Automatically open a PR rather than ask users to open a PR themselves
This PR requires setting a token; my suggestion would be to create a bot account and have its token as a secret as it's done for https://huggingface.co/model-sizer-bot https://huggingface.co/spaces/hf-accelerate/model-memory-usage
app.py
CHANGED
@@ -19,7 +19,6 @@ default_template = """{% for message in messages %}
|
|
19 |
{% endif %}"""
|
20 |
|
21 |
description_text = """### This space is a helper app for writing [Chat Templates](https://huggingface.co/docs/transformers/main/en/chat_templating).
|
22 |
-
|
23 |
### When you're happy with the outputs from your template, you can use the code block at the end to add it to a PR!"""
|
24 |
|
25 |
def apply_chat_template(template, test_conversation, add_generation_prompt, cleanup_whitespace):
|
@@ -36,20 +35,29 @@ def apply_chat_template(template, test_conversation, add_generation_prompt, clea
|
|
36 |
)
|
37 |
pr_snippet = "\n".join(pr_snippet)
|
38 |
formatted = tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=add_generation_prompt)
|
39 |
-
return formatted
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
)
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
{% endif %}"""
|
20 |
|
21 |
description_text = """### This space is a helper app for writing [Chat Templates](https://huggingface.co/docs/transformers/main/en/chat_templating).
|
|
|
22 |
### When you're happy with the outputs from your template, you can use the code block at the end to add it to a PR!"""
|
23 |
|
24 |
def apply_chat_template(template, test_conversation, add_generation_prompt, cleanup_whitespace):
|
|
|
35 |
)
|
36 |
pr_snippet = "\n".join(pr_snippet)
|
37 |
formatted = tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=add_generation_prompt)
|
38 |
+
return formatted #, pr_snippet
|
39 |
+
|
40 |
+
def open_pr(template, model_repo):
|
41 |
+
tokenizer = AutoTokenizer.from_pretrained(model_repo)
|
42 |
+
tokenizer.chat_template = template
|
43 |
+
tokenizer.push_to_hub(model_repo, create_pr=True)
|
44 |
+
|
45 |
+
|
46 |
+
with gr.Blocks() as demo:
|
47 |
+
gr.Markdown(description_text)
|
48 |
+
|
49 |
+
with gr.Row():
|
50 |
+
with gr.Column():
|
51 |
+
template = gr.TextArea(value=default_template, lines=10, max_lines=30, label="Chat Template")
|
52 |
+
example = gr.TextArea(value=demo_conversation, lines=6, label="Conversation")
|
53 |
+
generate_prompt = gr.Checkbox(value=False, label="Add generation prompt")
|
54 |
+
clean_whitespace = gr.Checkbox(value=True, label="Cleanup template whitespace")
|
55 |
+
btn = gr.Button("Submit")
|
56 |
+
with gr.Column():
|
57 |
+
output = gr.TextArea(label="Formatted conversation")
|
58 |
+
model_repo = gr.Textbox(label='Model repo to open a PR')
|
59 |
+
btn_pr = gr.Button("Open a PR with template update")
|
60 |
+
btn.click(fn=apply_chat_template, inputs=[template, example, generate_prompt, clean_whitespace], outputs=[output])
|
61 |
+
btn_pr.click(fn=open_pr, inputs=[template, model_repo])
|
62 |
+
|
63 |
+
demo.launch()
|