Matt commited on
Commit
fcf6d89
1 Parent(s): 565c08f

Add cleanup whitespace

Browse files
Files changed (1) hide show
  1. app.py +6 -3
app.py CHANGED
@@ -15,15 +15,17 @@ demo_conversation2 = """[
15
  ]"""
16
 
17
  default_template = """{% for message in messages %}
18
- {{ "<|im_start|>" + message["role"] + "\\n" + message["content"] + "<|im_end|>\\n" }}
19
  {% endfor %}
20
  {% if add_generation_prompt %}
21
- {{ "<|im_start|>assistant\\n" }}
22
  {% endif %}"""
23
 
24
  conversations = [demo_conversation1, demo_conversation2]
25
 
26
- def apply_chat_template(template, test_conversation1, test_conversation2):
 
 
27
  tokenizer.chat_template = template
28
  outputs = []
29
  for i, conversation_str in enumerate((test_conversation1, test_conversation2)):
@@ -37,6 +39,7 @@ iface = gr.Interface(
37
  fn=apply_chat_template,
38
  inputs=[
39
  gr.TextArea(value=default_template, lines=10, max_lines=30, label="Chat Template"),
 
40
  gr.TextArea(value=str(demo_conversation1), lines=5, label="Conversation 1"),
41
  gr.TextArea(value=str(demo_conversation2), lines=5, label="Conversation 2")
42
  ],
 
15
  ]"""
16
 
17
  default_template = """{% for message in messages %}
18
+ {{ "<|im_start|>" + message["role"] + "\\n" + message["content"] + "<|im_end|>\\n" }}
19
  {% endfor %}
20
  {% if add_generation_prompt %}
21
+ {{ "<|im_start|>assistant\\n" }}
22
  {% endif %}"""
23
 
24
  conversations = [demo_conversation1, demo_conversation2]
25
 
26
+ def apply_chat_template(template, cleanup_whitespace, test_conversation1, test_conversation2):
27
+ if cleanup_whitespace:
28
+ template = "".join([line.strip() for line in template.split('\n')])
29
  tokenizer.chat_template = template
30
  outputs = []
31
  for i, conversation_str in enumerate((test_conversation1, test_conversation2)):
 
39
  fn=apply_chat_template,
40
  inputs=[
41
  gr.TextArea(value=default_template, lines=10, max_lines=30, label="Chat Template"),
42
+ gr.CheckBox(value=True, label="Cleanup template whitespace"),
43
  gr.TextArea(value=str(demo_conversation1), lines=5, label="Conversation 1"),
44
  gr.TextArea(value=str(demo_conversation2), lines=5, label="Conversation 2")
45
  ],