Spaces:
Running
Running
File size: 16,497 Bytes
2bae4ee 5bb95cf 2bae4ee 1a81b28 2bae4ee 5bb95cf 2bae4ee 1a81b28 2bae4ee 5bb95cf 2bae4ee 5bb95cf 2bae4ee e662215 5bb95cf 2bae4ee 5bb95cf 2bae4ee 5bb95cf 2bae4ee 1a81b28 2bae4ee |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chat Template Tester</title>
<script src="https://mozilla.github.io/nunjucks/files/nunjucks.min.js"></script>
<style>
/* CSS Reset and Base styles */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: system-ui, -apple-system, sans-serif;
background-color: #f5f5f5;
line-height: 1.6;
color: #333;
}
/* Container */
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
@media (max-width: 768px) {
.container {
padding: 12px;
}
}
/* Typography */
h1 {
font-size: clamp(1.5rem, 4vw, 2rem);
margin-bottom: 1.5rem;
color: #333;
}
h2 {
font-size: clamp(1.2rem, 3vw, 1.5rem);
margin-bottom: 1rem;
color: #444;
}
/* Card Layout */
.card {
background: white;
padding: clamp(16px, 3vw, 24px);
border-radius: 12px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
margin-bottom: 20px;
}
/* Form Elements */
.input-group {
display: flex;
gap: 8px;
margin-bottom: 12px;
}
@media (max-width: 600px) {
.input-group {
flex-direction: column;
}
.input-group select,
.input-group input,
.input-group button {
width: 100%;
}
}
textarea,
input[type="text"],
select {
padding: 12px;
border: 1.5px solid #ddd;
border-radius: 8px;
font-size: 14px;
background: #fff;
transition: border-color 0.2s;
}
textarea:focus,
input[type="text"]:focus,
select:focus {
outline: none;
border-color: #4CAF50;
}
select {
min-width: 120px;
}
@media (max-width: 600px) {
select {
min-width: 100%;
}
}
textarea {
width: 100%;
min-height: 120px;
resize: vertical;
font-family: inherit;
}
/* Buttons */
button {
background-color: #4CAF50;
color: white;
padding: 12px 20px;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 14px;
font-weight: 500;
transition: all 0.2s;
white-space: nowrap;
}
button:hover {
background-color: #45a049;
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
button.secondary {
background-color: #6c757d;
}
button.danger {
background-color: #dc3545;
}
button.small {
padding: 6px 12px;
font-size: 13px;
}
/* Messages Section */
.message {
background-color: #f8f9fa;
padding: 16px;
border-radius: 8px;
margin-bottom: 12px;
display: flex;
align-items: center;
gap: 12px;
flex-wrap: wrap;
}
@media (max-width: 600px) {
.message {
flex-direction: column;
align-items: stretch;
}
}
.message-content {
flex-grow: 1;
min-width: 200px;
word-break: break-word;
}
.message strong {
color: #495057;
margin-right: 8px;
display: inline-block;
}
.message-actions {
display: flex;
gap: 6px;
flex-wrap: wrap;
}
@media (max-width: 600px) {
.message-actions {
justify-content: flex-end;
margin-top: 8px;
}
}
/* Configuration section */
.config-group {
display: flex;
align-items: center;
gap: 16px;
margin-bottom: 16px;
flex-wrap: wrap;
}
@media (max-width: 600px) {
.config-group {
flex-direction: column;
align-items: stretch;
}
}
/* Output section */
#output {
white-space: pre-wrap;
background-color: #f8f9fa;
padding: 16px;
border-radius: 8px;
font-family: monospace;
border: 1.5px solid #ddd;
overflow-x: auto;
font-size: 14px;
line-height: 1.5;
}
#error {
white-space: pre-wrap;
color: #ffffff;
background-color: #f8090a;
padding: 16px;
border-radius: 8px;
font-family: monospace;
border: 1.5px solid #ddd;
overflow-x: auto;
font-size: 14px;
line-height: 1.5;
}
/* Checkbox styling */
.checkbox-wrapper {
display: flex;
align-items: center;
gap: 8px;
}
input[type="checkbox"] {
width: 16px;
height: 16px;
}
/* Loading state */
.loading {
opacity: 0.7;
pointer-events: none;
}
/* Focus styles for accessibility */
:focus-visible {
outline: 2px solid #4CAF50;
outline-offset: 2px;
}
</style>
</head>
<body>
<div class="container">
<h1>Chat Template Tester</h1>
<div class="card">
<h2>Messages</h2>
<div class="input-group">
<select id="role">
<option value="user">User</option>
<option value="assistant">Assistant</option>
<option value="system">System</option>
</select>
<input type="text" id="content" placeholder="Type your message here..." style="flex-grow: 1">
<button onclick="addMessage()">Add Message</button>
</div>
<div id="messages"></div>
</div>
<div class="card">
<h2>Configuration</h2>
<div class="config-group">
<div class="checkbox-wrapper">
<input type="checkbox" id="addGenerationPrompt">
<label for="addGenerationPrompt">Add Generation Prompt</label>
</div>
<div class="input-group" style="margin-bottom: 0; flex-grow: 1;">
<input type="text" id="bosToken" placeholder="BOS Token (e.g., <s>)" style="flex-grow: 1;">
<input type="text" id="eosToken" placeholder="EOS Token (e.g., </s>)" style="flex-grow: 1;">
</div>
</div>
<div class="input-group">
<input type="text" id="repoUrl" placeholder="Enter Hugging Face Repo URL" style="flex-grow: 1">
<button onclick="handleFetchConfig()">Fetch Config</button>
</div>
<h2>Template</h2>
<textarea id="template" placeholder="Enter your template here..."></textarea>
<button onclick="applyTemplate()" style="margin-top: 16px">Apply Template</button>
</div>
<div class="card">
<h2>Output</h2>
<pre id="output"></pre>
<pre id="error" style="display: none"></pre>
</div>
</div>
<script>
let messages = [];
let editingIndex = null;
function addMessage() {
const role = document.getElementById('role').value;
const content = document.getElementById('content').value;
if (content.trim()) {
messages.push({ role, content });
updateMessageDisplay();
document.getElementById('content').value = '';
document.getElementById('content').focus();
}
}
function updateMessageDisplay() {
const messagesDiv = document.getElementById('messages');
messagesDiv.innerHTML = messages.map((msg, index) => `
<div class="message">
<div class="message-content">
<strong>${msg.role}:</strong>
${editingIndex === index
? `<input type="text" id="editContent" value="${msg.content}" style="width: 100%;">`
: msg.content}
</div>
<div class="message-actions">
${editingIndex === index
? `<button class="small" onclick="saveEdit(${index})">Save</button>`
: `<button class="small secondary" onclick="editMessage(${index})">Edit</button>`}
<button class="small danger" onclick="removeMessage(${index})">Remove</button>
<button class="small" onclick="moveMessageUp(${index})" ${index === 0 ? 'disabled' : ''}>β</button>
<button class="small" onclick="moveMessageDown(${index})" ${index === messages.length - 1 ? 'disabled' : ''}>β</button>
</div>
</div>
`).join('');
}
function removeMessage(index) {
messages.splice(index, 1);
updateMessageDisplay();
}
function editMessage(index) {
editingIndex = index;
updateMessageDisplay();
setTimeout(() => {
const editInput = document.getElementById('editContent');
if (editInput) {
editInput.focus();
editInput.select();
}
}, 0);
}
function saveEdit(index) {
const newContent = document.getElementById('editContent').value;
if (newContent.trim()) {
messages[index].content = newContent;
editingIndex = null;
updateMessageDisplay();
}
}
function moveMessageUp(index) {
if (index > 0) {
[messages[index - 1], messages[index]] = [messages[index], messages[index - 1]];
updateMessageDisplay();
}
}
function moveMessageDown(index) {
if (index < messages.length - 1) {
[messages[index], messages[index + 1]] = [messages[index + 1], messages[index]];
updateMessageDisplay();
}
}
function raiseException(string) {
document.getElementById("output").style.display = "none";
document.getElementById("error").style.display = "block";
document.getElementById('error').textContent = `Error: ${string}`;
}
function applyTemplate() {
document.getElementById("output").style.display = "block";
document.getElementById("error").style.display = "none";
const template = document.getElementById('template').value;
const addGenerationPrompt = document.getElementById('addGenerationPrompt').checked;
const bosToken = document.getElementById('bosToken').value;
const eosToken = document.getElementById('eosToken').value;
const context = {
messages: messages,
add_generation_prompt: addGenerationPrompt,
bos_token: bosToken,
eos_token: eosToken,
raise_exception: raiseException,
};
try {
document.getElementById('output').parentElement.classList.add('loading');
const result = nunjucks.renderString(template, context);
const decodedResult = result.replace(/</g, '<').replace(/>/g, '>').replace(/&/g, '&');
document.getElementById('output').textContent = decodedResult;
} catch (error) {
document.getElementById('output').textContent = `Error: ${error.message}`;
} finally {
document.getElementById('output').parentElement.classList.remove('loading');
}
}
// Keyboard shortcuts and accessibility
document.addEventListener('keydown', function(e) {
// Add message on Enter in the content input
if (e.target.id === 'content' && e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
addMessage();
}
// Save edit on Enter in the edit input
if (e.target.id === 'editContent' && e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
saveEdit(editingIndex);
}
// Cancel edit on Escape
if (e.key === 'Escape' && editingIndex !== null) {
editingIndex = null;
updateMessageDisplay();
}
});
// Initialize
updateMessageDisplay();
// New Functions for fetching tokenizer config
function parseRepoUrl(url) {
const hfUrlRegex = /^(?:https?:\/\/)?(?:huggingface\.co|hf\.co)\/([^/]+)\/([^/]+)$/;
const shortRegex = /^([^/]+)\/([^/]+)$/;
let match = url.match(hfUrlRegex);
if (!match) {
match = url.match(shortRegex);
}
if (match) {
return { user: match[1], repo: match[2] };
}
return null;
}
async function fetchTokenizerConfig(user, repo) {
const apiUrl = `https://huggingface.co/${user}/${repo}/raw/main/tokenizer_config.json`;
try {
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error("Error fetching tokenizer config:", error);
displayError(`Failed to fetch tokenizer config: ${error.message}`);
return null;
}
}
function displayError(message) {
console.error(message);
}
function populateConfigFields(config) {
const bosTokenInput = document.getElementById('bosToken');
const eosTokenInput = document.getElementById('eosToken');
const templateTextarea = document.getElementById('template');
bosTokenInput.value = config?.bos_token ?? "";
eosTokenInput.value = config?.eos_token ?? "";
let chatTemplate = config?.chat_template ?? "";
// Decode HTML entities
if (chatTemplate) {
const tempElement = document.createElement('div');
tempElement.innerHTML = chatTemplate;
chatTemplate = tempElement.textContent;
}
templateTextarea.value = chatTemplate;
}
async function handleFetchConfig() {
const repoUrl = document.getElementById('repoUrl').value;
const repoInfo = parseRepoUrl(repoUrl);
if (!repoInfo) {
displayError("Invalid Hugging Face repository URL format.");
return;
}
document.getElementById('repoUrl').parentElement.classList.add('loading');
const { user, repo } = repoInfo;
const config = await fetchTokenizerConfig(user, repo);
document.getElementById('repoUrl').parentElement.classList.remove('loading');
if (config) {
console.log("Tokenizer Config:", JSON.stringify(config, null, 2));
populateConfigFields(config);
}
}
</script>
</body>
</html> |