Jofthomas HF staff commited on
Commit
7a435b7
·
verified ·
1 Parent(s): 9bf50e7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -38
app.py CHANGED
@@ -17,7 +17,7 @@ demo_conversation = """[
17
  ]"""
18
 
19
  description_text = """# Chat Template Viewer
20
- ### This space is a helper to learn more about [Chat Templates](https://huggingface.co/docs/transformers/main/en/chat_templating).
21
  """
22
 
23
  default_tools = [{"type": "function", "function": {"name": "get_current_weather",
@@ -33,23 +33,7 @@ default_tools = [{"type": "function", "function": {"name": "get_current_weather"
33
  }
34
  }}]
35
 
36
- def get_template_names(model_name):
37
- try:
38
- tokenizer = AutoTokenizer.from_pretrained(model_name)
39
- if isinstance(tokenizer.chat_template, dict):
40
- return list(tokenizer.chat_template.keys())
41
- else:
42
- return []
43
- except Exception as e:
44
- return ["Default"]
45
-
46
- def update_template_dropdown(model_name):
47
- template_names = get_template_names(model_name)
48
- if template_names:
49
- return gr.Dropdown.update(choices=template_names, value=template_names[0])
50
- return gr.Dropdown.update(choices=[], value=None)
51
-
52
- def apply_chat_template(model_name, test_conversation, add_generation_prompt, cleanup_whitespace, template_name, hf_token, kwargs):
53
  try:
54
  if hf_token:
55
  login(token=hf_token) # Ensure login is successful
@@ -60,15 +44,9 @@ def apply_chat_template(model_name, test_conversation, add_generation_prompt, cl
60
 
61
  try:
62
  conversation = json.loads(test_conversation)
63
-
64
- if template_name and tokenizer.chat_template:
65
- template = tokenizer.chat_template.get(template_name, None)
66
- else:
67
- template = None
68
 
69
  formatted = tokenizer.apply_chat_template(
70
  conversation,
71
- chat_template=template,
72
  tokenize=False,
73
  add_generation_prompt=add_generation_prompt,
74
  tools=default_tools
@@ -80,19 +58,17 @@ def apply_chat_template(model_name, test_conversation, add_generation_prompt, cl
80
  with gr.Blocks() as demo:
81
  gr.Markdown(description_text)
82
 
83
- model_name_input = gr.Textbox(label="Model Name", placeholder="Enter model name", value=default_model)
84
- template_dropdown = gr.Dropdown(label="Template Name", choices=[], interactive=True)
85
- conversation_input = gr.TextArea(value=demo_conversation, lines=6, label="Conversation")
86
- add_generation_prompt_checkbox = gr.Checkbox(value=False, label="Add generation prompt")
87
- cleanup_whitespace_checkbox = gr.Checkbox(value=True, label="Cleanup template whitespace")
88
- hf_token_input = gr.Textbox(label="Hugging Face Token (optional)", placeholder="Enter your HF token", type="password")
89
- kwargs_input = gr.JSON(label="Additional kwargs", value=default_tools, visible=False)
90
- output = gr.TextArea(label="Formatted conversation", interactive=False)
91
-
92
- update_button = gr.Button("Update Template List")
93
- format_button = gr.Button("Format Conversation")
94
 
95
- update_button.click(fn=update_template_dropdown, inputs=model_name_input, outputs=template_dropdown)
 
96
 
97
  format_button.click(
98
  fn=apply_chat_template,
@@ -101,9 +77,8 @@ with gr.Blocks() as demo:
101
  conversation_input,
102
  add_generation_prompt_checkbox,
103
  cleanup_whitespace_checkbox,
104
- template_dropdown,
105
  hf_token_input,
106
- kwargs_input
107
  ],
108
  outputs=output
109
  )
 
17
  ]"""
18
 
19
  description_text = """# Chat Template Viewer
20
+ ### This space helps visualize chat formatting using Hugging Face models.
21
  """
22
 
23
  default_tools = [{"type": "function", "function": {"name": "get_current_weather",
 
33
  }
34
  }}]
35
 
36
+ def apply_chat_template(model_name, test_conversation, add_generation_prompt, cleanup_whitespace, hf_token, kwargs):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  try:
38
  if hf_token:
39
  login(token=hf_token) # Ensure login is successful
 
44
 
45
  try:
46
  conversation = json.loads(test_conversation)
 
 
 
 
 
47
 
48
  formatted = tokenizer.apply_chat_template(
49
  conversation,
 
50
  tokenize=False,
51
  add_generation_prompt=add_generation_prompt,
52
  tools=default_tools
 
58
  with gr.Blocks() as demo:
59
  gr.Markdown(description_text)
60
 
61
+ with gr.Row():
62
+ with gr.Column():
63
+ model_name_input = gr.Textbox(label="Model Name", placeholder="Enter model name", value=default_model)
64
+ hf_token_input = gr.Textbox(label="Hugging Face Token (optional)", placeholder="Enter your HF token", type="password")
65
+ conversation_input = gr.TextArea(value=demo_conversation, lines=8, label="Conversation")
66
+ add_generation_prompt_checkbox = gr.Checkbox(value=False, label="Add generation prompt")
67
+ cleanup_whitespace_checkbox = gr.Checkbox(value=True, label="Cleanup template whitespace")
68
+ format_button = gr.Button("Format Conversation")
 
 
 
69
 
70
+ with gr.Column():
71
+ output = gr.TextArea(label="Formatted Conversation", interactive=False, lines=12)
72
 
73
  format_button.click(
74
  fn=apply_chat_template,
 
77
  conversation_input,
78
  add_generation_prompt_checkbox,
79
  cleanup_whitespace_checkbox,
 
80
  hf_token_input,
81
+ default_tools # Default tools parameter
82
  ],
83
  outputs=output
84
  )