Update app.py
Browse files
app.py
CHANGED
|
@@ -17,7 +17,7 @@ demo_conversation = """[
|
|
| 17 |
]"""
|
| 18 |
|
| 19 |
description_text = """# Chat Template Viewer
|
| 20 |
-
### This space
|
| 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
|
| 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 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
update_button = gr.Button("Update Template List")
|
| 93 |
-
format_button = gr.Button("Format Conversation")
|
| 94 |
|
| 95 |
-
|
|
|
|
| 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 |
-
|
| 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 |
)
|