Spaces:
Sleeping
Sleeping
File size: 31,272 Bytes
b5259f8 c8cd7bf b5259f8 c8cd7bf b5259f8 c8cd7bf cac85d3 c8cd7bf cac85d3 c8cd7bf b5259f8 c8cd7bf b5259f8 c8cd7bf b5259f8 c8cd7bf 89c3ee0 b5259f8 c8cd7bf b5259f8 a0b0e01 b5259f8 cac85d3 c8cd7bf cac85d3 c8cd7bf a0b0e01 b5259f8 c8cd7bf b5259f8 a0b0e01 | 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 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 | import gradio as gr
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from transformers import PreTrainedTokenizerFast
import os
import json
import random
import hashlib
import re
# --------------------------------------
# LOAD TOKENIZER
# --------------------------------------
TOKENIZER_JSON = "tokenizer_hindi_bpe_8k_stream/tokenizer.json"
HF_DIR = "tokenizer_hindi_bpe_8k_stream/hf"
if os.path.exists(HF_DIR):
tokenizer = PreTrainedTokenizerFast.from_pretrained(HF_DIR)
elif os.path.exists(TOKENIZER_JSON):
tokenizer = PreTrainedTokenizerFast(tokenizer_file=TOKENIZER_JSON)
else:
raise ValueError("Tokenizer not found!")
print("Tokenizer loaded: vocab =", tokenizer.vocab_size)
# --------------------------------------
# ENCODE / DECODE FUNCTIONS
# --------------------------------------
def get_color_for_token(token_id, seed=None):
"""Generate a consistent color for a token ID."""
if seed is not None:
random.seed(seed)
# Generate a hash-based color
hash_obj = hashlib.md5(str(token_id).encode())
hash_int = int(hash_obj.hexdigest(), 16)
# Use HSL for better color distribution
hue = hash_int % 360
saturation = 60 + (hash_int % 30)
lightness = 75 + (hash_int % 15)
return f"hsl({hue}, {saturation}%, {lightness}%)"
def encode_text(text: str):
"""Basic encode: returns token IDs as CSV, token count, compression ratio, and color-coded HTML."""
enc = tokenizer(text, add_special_tokens=False, return_offsets_mapping=True)
token_ids = enc["input_ids"]
tokens = tokenizer.convert_ids_to_tokens(token_ids)
offsets = enc.get("offset_mapping", [])
token_count = len(token_ids)
csv_ids = ",".join(str(x) for x in token_ids)
# Calculate compression ratio (characters per token)
char_count = len(text)
compression_ratio = char_count / token_count if token_count > 0 else 0.0
# First, build token-to-word mapping using offsets
token_ranges = []
for idx, (start, end) in enumerate(offsets):
if start is not None and end is not None:
token_ranges.append((idx, start, end))
else:
token_ranges.append((idx, None, None))
# Get word positions for mapping
words_with_positions = []
for match in re.finditer(r'\S+', text):
word = match.group()
word_start = match.start()
word_end = match.end()
words_with_positions.append((word, word_start, word_end))
# Build token-to-word mapping
token_to_words_map = {}
for token_idx, token_start, token_end in token_ranges:
if token_start is not None and token_end is not None:
token_to_words_map[token_idx] = []
for word_idx, (word, word_start, word_end) in enumerate(words_with_positions):
if token_start < word_end and token_end > word_start:
token_to_words_map[token_idx].append(word_idx)
# Store token data for potential future use
token_data = []
for i, (token, token_id) in enumerate(zip(tokens, token_ids)):
token_data.append({
"idx": i,
"token": token,
"id": token_id
})
# Include JavaScript for highlighting in the HTML
highlight_script = """
<script>
if (typeof window.highlightFunctions === 'undefined') {
window.highlightFunctions = {};
window.currentHighlighted = null;
window.clearHighlights = function() {
if (window.currentHighlighted) {
window.currentHighlighted.forEach(el => {
el.classList.remove('highlighted');
el.style.borderColor = 'transparent';
el.style.boxShadow = 'none';
if (el.style.transform) el.style.transform = 'scale(1)';
});
}
window.currentHighlighted = null;
};
window.highlightInputWord = function(tokenIndicesStr) {
window.clearHighlights();
const tokenIndices = tokenIndicesStr.split(',');
const highlighted = [];
// Highlight input words
document.querySelectorAll(`.input-word-tag[data-word-tokens="${tokenIndicesStr}"]`).forEach(el => {
el.classList.add('highlighted');
el.style.borderColor = '#ff0000';
el.style.boxShadow = '0 0 8px rgba(255,0,0,0.5)';
highlighted.push(el);
});
// Highlight corresponding token IDs
tokenIndices.forEach(idx => {
document.querySelectorAll(`.token-id-tag[data-token-idx="${idx}"]`).forEach(el => {
el.classList.add('highlighted');
el.style.borderColor = '#ff0000';
el.style.boxShadow = '0 0 8px rgba(255,0,0,0.5)';
el.style.transform = 'scale(1.1)';
highlighted.push(el);
});
});
window.currentHighlighted = highlighted;
};
window.highlightTokenId = function(tokenIdx) {
window.clearHighlights();
const tokenIdEl = document.querySelector(`.token-id-tag[data-token-idx="${tokenIdx}"]`);
if (!tokenIdEl) return;
const highlighted = [tokenIdEl];
tokenIdEl.classList.add('highlighted');
tokenIdEl.style.borderColor = '#ff0000';
tokenIdEl.style.boxShadow = '0 0 8px rgba(255,0,0,0.5)';
tokenIdEl.style.transform = 'scale(1.1)';
// Find input words that contain this token
const tokenId = tokenIdEl.getAttribute('data-token-id');
document.querySelectorAll('.input-word-tag').forEach(wordEl => {
const tokenIndices = wordEl.getAttribute('data-word-tokens');
if (tokenIndices) {
const tokenList = tokenIndices.split(',');
if (tokenList.includes(tokenIdx.toString())) {
wordEl.classList.add('highlighted');
wordEl.style.borderColor = '#ff0000';
wordEl.style.boxShadow = '0 0 8px rgba(255,0,0,0.5)';
highlighted.push(wordEl);
}
}
});
window.currentHighlighted = highlighted;
};
}
</script>
"""
token_json = json.dumps(token_data)
# Create clickable HTML for input text (mirror of textbox) - uses same words_with_positions
input_word_html_parts = []
for word, word_start, word_end in words_with_positions:
word_escaped = word.replace("<", "<").replace(">", ">").replace("&", "&")
# Find tokens whose character ranges overlap with this word
word_token_indices = []
for token_idx, token_start, token_end in token_ranges:
if token_start is not None and token_end is not None:
if token_start < word_end and token_end > word_start:
word_token_indices.append(token_idx)
if word_token_indices:
token_id_for_word = token_ids[word_token_indices[0]]
color = get_color_for_token(token_id_for_word, seed=42)
token_indices_str = ",".join(map(str, word_token_indices))
input_word_html_parts.append(
f'<span class="input-word-tag" data-word-tokens="{token_indices_str}" '
f'style="background-color: {color}; padding: 2px 6px; margin: 2px; '
f'border-radius: 4px; cursor: pointer; display: inline-block; '
f'border: 2px solid transparent; transition: all 0.2s;" '
f'onclick="highlightInputWord(\'{token_indices_str}\')" '
f'onmouseover="this.style.borderColor=\'#333\'" '
f'onmouseout="if(!document.querySelector(\'.input-word-tag.highlighted\')) this.style.borderColor=\'transparent\'">{word_escaped}</span>'
)
else:
input_word_html_parts.append(f'<span style="padding: 2px 6px; margin: 2px;">{word_escaped}</span>')
input_html = '<div style="line-height: 2; padding: 10px; background: #ffffff; border: 2px solid #e0e0e0; border-radius: 8px; min-height: 60px;">' + " ".join(input_word_html_parts) + '</div>'
# Create token IDs display with labels for highlighting
token_ids_html_parts = []
for i, token_id in enumerate(token_ids):
color = get_color_for_token(token_id, seed=42)
# Find which words contain this token
word_indices = token_to_words_map.get(i, [])
word_labels = [words_with_positions[idx][0] for idx in word_indices]
word_label = ", ".join(word_labels[:2]) if word_labels else "" # Show first 2 words as label
token_ids_html_parts.append(
f'<div class="token-id-tag" data-token-idx="{i}" data-token-id="{token_id}" '
f'style="background-color: {color}; padding: 6px 10px; margin: 4px; '
f'border-radius: 6px; cursor: pointer; display: inline-block; vertical-align: top; '
f'border: 2px solid transparent; transition: all 0.2s; text-align: center; min-width: 60px;" '
f'onclick="highlightTokenId({i})" '
f'onmouseover="this.style.borderColor=\'#333\'" '
f'onmouseout="if(!document.querySelector(\'.token-id-tag[data-token-idx=\'{i}\'].highlighted\')) this.style.borderColor=\'transparent\'">'
f'<div style="font-family: monospace; font-weight: bold; font-size: 14px; color: #000;">{token_id}</div>'
f'<div style="font-size: 10px; color: #555; margin-top: 2px; word-break: break-word; max-width: 80px;">{word_label if word_label else " "}</div>'
f'</div>'
)
token_ids_html = '<div style="padding: 10px; background: #f8f9fa; border-radius: 8px; margin-top: 10px;">' + "".join(token_ids_html_parts) + '</div>'
return csv_ids, token_count, f"{compression_ratio:.2f}", token_ids_html, token_json, input_html
def decode_ids(ids: str):
"""Decode from comma-separated IDs to text with color-coded HTML."""
try:
arr = [int(x) for x in ids.split(",") if x.strip()]
decoded_text = tokenizer.decode(arr, skip_special_tokens=False)
# Re-encode with offsets to map tokens to words accurately
enc_with_offsets = tokenizer(decoded_text, add_special_tokens=False, return_offsets_mapping=True)
tokens = tokenizer.convert_ids_to_tokens(arr)
offsets = enc_with_offsets.get("offset_mapping", [])
# Build token-to-character-range mapping
token_ranges = []
for idx, (start, end) in enumerate(offsets):
if start is not None and end is not None:
token_ranges.append((idx, start, end))
else:
token_ranges.append((idx, None, None))
# Get word positions for mapping
words_with_positions = []
for match in re.finditer(r'\S+', decoded_text):
word = match.group()
word_start = match.start()
word_end = match.end()
words_with_positions.append((word, word_start, word_end))
# Create color-coded HTML for decoded text
word_html_parts = []
for word, word_start, word_end in words_with_positions:
word_escaped = word.replace("<", "<").replace(">", ">").replace("&", "&")
# Find tokens whose character ranges overlap with this word
word_token_indices = []
for token_idx, token_start, token_end in token_ranges:
if token_start is not None and token_end is not None:
# Check if token overlaps with word
if token_start < word_end and token_end > word_start:
word_token_indices.append(token_idx)
if word_token_indices and word_token_indices[0] < len(arr):
token_id_for_word = arr[word_token_indices[0]]
color = get_color_for_token(token_id_for_word, seed=42)
token_indices_str = ",".join(map(str, word_token_indices))
word_html_parts.append(
f'<span class="decode-word-tag" data-word-tokens="{token_indices_str}" '
f'style="background-color: {color}; padding: 2px 6px; margin: 2px; '
f'border-radius: 4px; cursor: pointer; display: inline-block; '
f'border: 2px solid transparent; transition: all 0.2s;" '
f'onclick="highlightDecodeWord(\'{token_indices_str}\')" '
f'onmouseover="this.style.borderColor=\'#333\'" '
f'onmouseout="if(!document.querySelector(\'.decode-word-tag.highlighted\')) this.style.borderColor=\'transparent\'">{word_escaped}</span>'
)
else:
word_html_parts.append(f'<span style="padding: 2px 6px; margin: 2px;">{word_escaped}</span>')
# Build token-to-word mapping for decode
token_to_words_map = {}
for token_idx, token_start, token_end in token_ranges:
if token_start is not None and token_end is not None:
token_to_words_map[token_idx] = []
for word_idx, (word, word_start, word_end) in enumerate(words_with_positions):
if token_start < word_end and token_end > word_start:
token_to_words_map[token_idx].append(word_idx)
decoded_html = '<div style="line-height: 2; padding: 10px; background: #ffffff; border: 2px solid #e0e0e0; border-radius: 8px; min-height: 60px;">' + " ".join(word_html_parts) + '</div>'
# Create token IDs display with labels for decode (similar to encode)
decode_token_ids_html_parts = []
for i, token_id in enumerate(arr):
color = get_color_for_token(token_id, seed=42)
# Find which words contain this token
word_indices = token_to_words_map.get(i, [])
word_labels = [words_with_positions[idx][0] for idx in word_indices if idx < len(words_with_positions)]
word_label = ", ".join(word_labels[:2]) if word_labels else "" # Show first 2 words as label
decode_token_ids_html_parts.append(
f'<div class="decode-token-id-tag" data-token-idx="{i}" data-token-id="{token_id}" '
f'style="background-color: {color}; padding: 6px 10px; margin: 4px; '
f'border-radius: 6px; cursor: pointer; display: inline-block; vertical-align: top; '
f'border: 2px solid transparent; transition: all 0.2s; text-align: center; min-width: 60px;" '
f'onclick="highlightDecodeTokenId({i})" '
f'onmouseover="this.style.borderColor=\'#333\'" '
f'onmouseout="if(!document.querySelector(\'.decode-token-id-tag[data-token-idx=\'{i}\'].highlighted\')) this.style.borderColor=\'transparent\'">'
f'<div style="font-family: monospace; font-weight: bold; font-size: 14px; color: #000;">{token_id}</div>'
f'<div style="font-size: 10px; color: #555; margin-top: 2px; word-break: break-word; max-width: 80px;">{word_label if word_label else " "}</div>'
f'</div>'
)
decode_token_ids_html = '<div style="padding: 10px; background: #f8f9fa; border-radius: 8px; margin-top: 10px;">' + "".join(decode_token_ids_html_parts) + '</div>'
return decoded_html, decode_token_ids_html, decoded_text
except Exception as e:
error_msg = f"❌ Invalid ID list: {str(e)}"
return f"<div style='padding: 10px; color: red;'>{error_msg}</div>", "", error_msg
# --------------------------------------
# FASTAPI REST BACKEND
# --------------------------------------
api = FastAPI(title="Hindi Tokenizer API")
api.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"]
)
@api.get("/")
def home():
return {
"message": "Hindi Tokenizer API",
"vocab_size": tokenizer.vocab_size
}
@api.get("/tokenize")
def tokenize_endpoint(text: str):
enc = tokenizer(text, add_special_tokens=False)
tokens = tokenizer.convert_ids_to_tokens(enc["input_ids"])
return {"tokens": tokens, "ids": enc["input_ids"]}
@api.get("/decode")
def decode_endpoint(ids: str):
try:
arr = [int(x) for x in ids.split(",") if x.strip()]
return {"text": tokenizer.decode(arr)}
except:
return {"error": "Invalid id list"}
# --------------------------------------
# GRADIO FRONTEND
# --------------------------------------
# JavaScript for interactive highlighting
highlight_js = """
<script>
let currentHighlighted = null;
function clearHighlights() {
if (currentHighlighted) {
currentHighlighted.forEach(el => {
el.classList.remove('highlighted');
el.style.borderColor = 'transparent';
el.style.boxShadow = 'none';
});
}
currentHighlighted = null;
}
function highlightToken(tokenIdx) {
clearHighlights();
const tokenEl = document.querySelector(`.token-tag[data-token-idx="${tokenIdx}"]`);
if (!tokenEl) return;
const tokenId = tokenEl.getAttribute('data-token-id');
const highlighted = [tokenEl];
// Highlight all tokens with same ID
document.querySelectorAll(`.token-tag[data-token-id="${tokenId}"]`).forEach(el => {
el.classList.add('highlighted');
el.style.borderColor = '#ff0000';
el.style.boxShadow = '0 0 8px rgba(255,0,0,0.5)';
highlighted.push(el);
});
// Highlight corresponding words
document.querySelectorAll('.word-tag').forEach(wordEl => {
const tokenIndices = wordEl.getAttribute('data-word-tokens').split(',');
if (tokenIndices.includes(tokenIdx.toString())) {
wordEl.classList.add('highlighted');
wordEl.style.borderColor = '#ff0000';
wordEl.style.boxShadow = '0 0 8px rgba(255,0,0,0.5)';
highlighted.push(wordEl);
}
});
currentHighlighted = highlighted;
}
function highlightWord(tokenIndicesStr) {
clearHighlights();
const tokenIndices = tokenIndicesStr.split(',');
const highlighted = [];
// Highlight words
document.querySelectorAll(`.word-tag[data-word-tokens="${tokenIndicesStr}"]`).forEach(el => {
el.classList.add('highlighted');
el.style.borderColor = '#ff0000';
el.style.boxShadow = '0 0 8px rgba(255,0,0,0.5)';
highlighted.push(el);
});
// Highlight corresponding tokens
tokenIndices.forEach(idx => {
document.querySelectorAll(`.token-tag[data-token-idx="${idx}"]`).forEach(el => {
el.classList.add('highlighted');
el.style.borderColor = '#ff0000';
el.style.boxShadow = '0 0 8px rgba(255,0,0,0.5)';
highlighted.push(el);
});
});
currentHighlighted = highlighted;
}
function highlightDecodeToken(tokenIdx) {
clearHighlights();
const tokenEl = document.querySelector(`.decode-token-tag[data-token-idx="${tokenIdx}"]`);
if (!tokenEl) return;
const highlighted = [tokenEl];
tokenEl.classList.add('highlighted');
tokenEl.style.borderColor = '#ff0000';
tokenEl.style.boxShadow = '0 0 8px rgba(255,0,0,0.5)';
// Highlight corresponding words in decoded text
document.querySelectorAll('.decode-word-tag').forEach(wordEl => {
const tokenIndices = wordEl.getAttribute('data-word-tokens').split(',');
if (tokenIndices.includes(tokenIdx.toString())) {
wordEl.classList.add('highlighted');
wordEl.style.borderColor = '#ff0000';
wordEl.style.boxShadow = '0 0 8px rgba(255,0,0,0.5)';
highlighted.push(wordEl);
}
});
currentHighlighted = highlighted;
}
function highlightInputWord(tokenIndicesStr) {
clearHighlights();
const tokenIndices = tokenIndicesStr.split(',');
const highlighted = [];
// Highlight input words
document.querySelectorAll(`.input-word-tag[data-word-tokens="${tokenIndicesStr}"]`).forEach(el => {
el.classList.add('highlighted');
el.style.borderColor = '#ff0000';
el.style.boxShadow = '0 0 8px rgba(255,0,0,0.5)';
highlighted.push(el);
});
// Highlight corresponding token IDs
tokenIndices.forEach(idx => {
document.querySelectorAll(`.token-id-tag[data-token-idx="${idx}"]`).forEach(el => {
el.classList.add('highlighted');
el.style.borderColor = '#ff0000';
el.style.boxShadow = '0 0 8px rgba(255,0,0,0.5)';
el.style.transform = 'scale(1.1)';
highlighted.push(el);
});
});
currentHighlighted = highlighted;
}
function highlightTokenId(tokenIdx) {
clearHighlights();
const tokenIdEl = document.querySelector(`.token-id-tag[data-token-idx="${tokenIdx}"]`);
if (!tokenIdEl) return;
const highlighted = [tokenIdEl];
tokenIdEl.classList.add('highlighted');
tokenIdEl.style.borderColor = '#ff0000';
tokenIdEl.style.boxShadow = '0 0 8px rgba(255,0,0,0.5)';
tokenIdEl.style.transform = 'scale(1.1)';
// Find input words that contain this token
document.querySelectorAll('.input-word-tag').forEach(wordEl => {
const tokenIndices = wordEl.getAttribute('data-word-tokens');
if (tokenIndices) {
const tokenList = tokenIndices.split(',');
if (tokenList.includes(tokenIdx.toString())) {
wordEl.classList.add('highlighted');
wordEl.style.borderColor = '#ff0000';
wordEl.style.boxShadow = '0 0 8px rgba(255,0,0,0.5)';
highlighted.push(wordEl);
}
}
});
currentHighlighted = highlighted;
}
function highlightDecodeWord(tokenIndicesStr) {
clearHighlights();
const tokenIndices = tokenIndicesStr.split(',');
const highlighted = [];
// Highlight words
document.querySelectorAll(`.decode-word-tag[data-word-tokens="${tokenIndicesStr}"]`).forEach(el => {
el.classList.add('highlighted');
el.style.borderColor = '#ff0000';
el.style.boxShadow = '0 0 8px rgba(255,0,0,0.5)';
highlighted.push(el);
});
// Highlight corresponding token IDs
tokenIndices.forEach(idx => {
document.querySelectorAll(`.decode-token-id-tag[data-token-idx="${idx}"]`).forEach(el => {
el.classList.add('highlighted');
el.style.borderColor = '#ff0000';
el.style.boxShadow = '0 0 8px rgba(255,0,0,0.5)';
el.style.transform = 'scale(1.1)';
highlighted.push(el);
});
});
currentHighlighted = highlighted;
}
function highlightDecodeTokenId(tokenIdx) {
clearHighlights();
const tokenIdEl = document.querySelector(`.decode-token-id-tag[data-token-idx="${tokenIdx}"]`);
if (!tokenIdEl) return;
const highlighted = [tokenIdEl];
tokenIdEl.classList.add('highlighted');
tokenIdEl.style.borderColor = '#ff0000';
tokenIdEl.style.boxShadow = '0 0 8px rgba(255,0,0,0.5)';
tokenIdEl.style.transform = 'scale(1.1)';
// Find decoded words that contain this token
document.querySelectorAll('.decode-word-tag').forEach(wordEl => {
const tokenIndices = wordEl.getAttribute('data-word-tokens');
if (tokenIndices) {
const tokenList = tokenIndices.split(',');
if (tokenList.includes(tokenIdx.toString())) {
wordEl.classList.add('highlighted');
wordEl.style.borderColor = '#ff0000';
wordEl.style.boxShadow = '0 0 8px rgba(255,0,0,0.5)';
highlighted.push(wordEl);
}
}
});
currentHighlighted = highlighted;
}
</script>
"""
with gr.Blocks(title="Hindi Tokenizer") as demo:
# Inject JavaScript for highlighting at the top of the page
gr.HTML(highlight_js)
gr.Markdown("## 🔡 Hindi BPE Tokenizer — Encode / Decode")
# Hidden component to store token data
token_data_store = gr.State(value="")
with gr.Tab("Encode"):
# Example texts
example_1 = "1,200 ईसा पूर्व संस्कृत भाषा संपूर्ण भारतीय उपमहाद्वीप में फैली हुए थी और तब तक यहां पर हिंदू धर्म का उद्धव हो चुका था और ऋग्वेद की रचना भी हो चुकी थी।[20] इसी समय बौद्ध एवं जैन धर्म उत्पन्न हो रहे होते थे।[21] प्रारंभिक राजनीतिक एकत्रीकरण ने गंगा बेसिन में स्थित मौर्य और गुप्त साम्राज्यों को जन्म दिया।[22] उनका समाज विस्तृत सृजनशीलता से भरा हुआ था। [23]"
example_2 = "भारत की सकल घरेलू उत्पाद (जीडीपी) की वृद्धि दर दूसरी तिमाही में 8.2 प्रतिशत बढ़ी। सरकारी आंकड़ों के अनुसार पिछले वित्त वर्ष की समान तिमाही में यह 5.6 प्रतिशत थी। सरकार की ओर से जारी आंकड़ों के अनुसार भारतीय अर्थव्यवस्था ने जुलाई-सितंबर तिमाही में 8.2 प्रतिशत की वृद्धि दर हासिल की। यह छह तिमाहियों का उच्चतम स्तर है। ऐसा इसलिए हुआ क्योंकि जीएसटी दर में कटौती से उपभोग बढ़ने की उम्मीद में कारखानों ने अधिक उत्पाद तैयार किए।"
example_3 = "मुंशी प्रेमचंद की एक लोकप्रिय कहानी 'पूस की रात' है, जो एक गरीब किसान, हल्कू की कहानी है। कहानी में दिखाया गया है कि कैसे हल्कू और उसकी पत्नी को कड़ाके की ठंड में अपने गरीब झोपड़ी में रहना पड़ता है और कैसे कर्ज चुकाने के लिए उन्हें अपनी फसल बेचनी पड़ती है। एक और प्रसिद्ध कहानी 'नमक का दारोगा' है, जो सरकारी नौकरी और ईमानदारी के महत्व को दर्शाती है।"
gr.Markdown("### 📚 Example Texts (Click to load and encode automatically)")
with gr.Row():
example_btn_1 = gr.Button("Example 1: Ancient India History", variant="secondary", size="sm")
example_btn_2 = gr.Button("Example 2: GDP Growth News", variant="secondary", size="sm")
example_btn_3 = gr.Button("Example 3: Premchand Stories", variant="secondary", size="sm")
text_in = gr.Textbox(label="Enter text", lines=3)
gr.Markdown("### 📝 Input Text (Click words to highlight token IDs)")
input_html_out = gr.HTML(label="Clickable Input Text", value="<div style='padding: 10px; color: #666; font-style: italic;'>Enter text above and click Encode to see clickable words</div>")
with gr.Row():
token_count_out = gr.Number(label="Token Count", precision=0)
compression_ratio_out = gr.Textbox(label="Compression Ratio (chars/token)", interactive=False)
gr.Markdown("### Token IDs (Click to highlight words)")
token_ids_html_out = gr.HTML(label="Token IDs with Labels")
ids_out = gr.Textbox(label="Token IDs (CSV)", lines=4, max_lines=10, interactive=False)
btn = gr.Button("Encode", variant="primary")
btn.click(encode_text, text_in, [ids_out, token_count_out, compression_ratio_out, token_ids_html_out, token_data_store, input_html_out])
# Function to load example and trigger encode
def load_and_encode_example_1():
text = example_1
encode_results = encode_text(text)
return text, *encode_results
def load_and_encode_example_2():
text = example_2
encode_results = encode_text(text)
return text, *encode_results
def load_and_encode_example_3():
text = example_3
encode_results = encode_text(text)
return text, *encode_results
example_btn_1.click(
fn=load_and_encode_example_1,
inputs=[],
outputs=[text_in, ids_out, token_count_out, compression_ratio_out, token_ids_html_out, token_data_store, input_html_out]
)
example_btn_2.click(
fn=load_and_encode_example_2,
inputs=[],
outputs=[text_in, ids_out, token_count_out, compression_ratio_out, token_ids_html_out, token_data_store, input_html_out]
)
example_btn_3.click(
fn=load_and_encode_example_3,
inputs=[],
outputs=[text_in, ids_out, token_count_out, compression_ratio_out, token_ids_html_out, token_data_store, input_html_out]
)
with gr.Tab("Decode"):
ids_in = gr.Textbox(label="Comma-separated token IDs", lines=4)
gr.Markdown("### 📝 Decoded Text (Click words to highlight token IDs)")
decoded_text_html_out = gr.HTML(label="Clickable Decoded Text", value="<div style='padding: 10px; color: #666; font-style: italic;'>Enter token IDs above and click Decode to see clickable words</div>")
gr.Markdown("### Token IDs (Click to highlight words)")
decode_token_ids_html_out = gr.HTML(label="Token IDs with Labels")
decoded_text_out = gr.Textbox(label="Decoded Text", lines=4, max_lines=10, interactive=False)
btn3 = gr.Button("Decode", variant="primary")
btn3.click(decode_ids, ids_in, [decoded_text_html_out, decode_token_ids_html_out, decoded_text_out])
# Mount FastAPI + Gradio
if "app" not in globals():
app = gr.mount_gradio_app(api, demo, path="/gradio")
if __name__ == "__main__":
demo.launch(server_port=7860, share=True) |