File size: 36,083 Bytes
c363258 ebbe7e8 c363258 ebbe7e8 c363258 ebbe7e8 c363258 ebbe7e8 c363258 ebbe7e8 c363258 ebbe7e8 c363258 ebbe7e8 c363258 ebbe7e8 c363258 daecf7c c363258 | 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 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 | <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Unlimited OCR β Baidu</title>
<meta name="description" content="Unlimited OCR: One-shot long-horizon document parsing powered by Baidu's vision-language model." />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@300;400;500;600&family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet" />
<style>
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
--bg-deep: #0a0c10;
--bg-panel: #0f1117;
--bg-surface: #161b22;
--bg-card: #1c2230;
--bg-hover: #21283a;
--border: #2a3347;
--border-soft: #1e2635;
--accent: #ff6b35;
--accent-glow: rgba(255,107,53,0.25);
--teal: #00d4aa;
--teal-dim: #008f73;
--teal-glow: rgba(0,212,170,0.2);
--blue: #4a9eff;
--red: #ef4444;
--text-primary: #e8eaf0;
--text-secondary:#8892a4;
--text-muted: #4a5568;
--font-mono: 'JetBrains Mono', monospace;
--font-ui: 'Inter', sans-serif;
--radius: 8px;
--radius-lg: 12px;
}
html, body { height: 100%; background: var(--bg-deep); color: var(--text-primary); font-family: var(--font-ui); overflow: hidden; }
.app { display: flex; flex-direction: column; height: 100vh; }
/* ββ Header ββ */
header {
background: var(--bg-panel);
border-bottom: 1px solid var(--border);
padding: 0 20px;
height: 52px;
display: flex;
align-items: center;
gap: 16px;
flex-shrink: 0;
}
.logo-area { display: flex; align-items: center; gap: 10px; }
.logo-icon {
width: 28px; height: 28px;
background: linear-gradient(135deg, var(--accent) 0%, #ff9f5a 100%);
border-radius: 6px;
display: flex; align-items: center; justify-content: center;
font-size: 14px;
box-shadow: 0 0 12px var(--accent-glow);
}
.logo-text { font-size: 14px; font-weight: 600; letter-spacing: 0.01em; }
.logo-text span { color: var(--accent); }
.header-sep { width: 1px; height: 24px; background: var(--border); }
.mode-group {
display: flex; gap: 4px;
background: var(--bg-deep);
border: 1px solid var(--border);
border-radius: var(--radius);
padding: 3px;
}
.mode-btn {
padding: 4px 14px;
border-radius: 5px;
font-family: var(--font-mono);
font-size: 11px; font-weight: 500;
cursor: pointer; border: none;
transition: all 0.18s ease;
color: var(--text-secondary);
background: transparent;
letter-spacing: 0.04em;
}
.mode-btn.active { background: var(--bg-surface); color: var(--text-primary); box-shadow: 0 1px 6px rgba(0,0,0,0.4); }
.mode-btn:hover:not(.active) { color: var(--text-primary); }
.ngram-group { display: flex; align-items: center; gap: 8px; }
.ngram-label { font-family: var(--font-mono); font-size: 10px; color: var(--text-muted); letter-spacing: 0.06em; }
.toggle-wrap { position: relative; width: 36px; height: 20px; }
.toggle-wrap input { display: none; }
.toggle-track { position: absolute; inset: 0; background: var(--border); border-radius: 10px; cursor: pointer; transition: background 0.2s; }
.toggle-track::after { content: ''; position: absolute; top: 3px; left: 3px; width: 14px; height: 14px; background: var(--text-secondary); border-radius: 50%; transition: transform 0.2s, background 0.2s; }
.toggle-wrap input:checked + .toggle-track { background: var(--teal-dim); }
.toggle-wrap input:checked + .toggle-track::after { transform: translateX(16px); background: var(--teal); }
.header-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.btn { display: flex; align-items: center; gap: 6px; padding: 6px 16px; border-radius: var(--radius); font-family: var(--font-mono); font-size: 11px; font-weight: 600; letter-spacing: 0.06em; cursor: pointer; border: none; transition: all 0.18s ease; }
.btn-start { background: var(--teal); color: #000; box-shadow: 0 0 14px var(--teal-glow); }
.btn-start:hover:not(:disabled) { background: #00eebc; box-shadow: 0 0 22px var(--teal-glow); transform: translateY(-1px); }
.btn-start:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }
.btn-stop { background: var(--red); color: #fff; box-shadow: 0 0 12px rgba(239,68,68,0.3); }
.btn-stop:hover:not(:disabled) { background: #f87171; transform: translateY(-1px); }
.btn-stop:disabled { opacity: 0.35; cursor: not-allowed; transform: none; }
.btn-clear { background: var(--bg-surface); color: var(--text-secondary); border: 1px solid var(--border); }
.btn-clear:hover { color: var(--text-primary); border-color: var(--text-muted); }
.btn-copy { background: var(--bg-surface); color: var(--text-secondary); border: 1px solid var(--border); }
.btn-copy:hover { color: var(--blue); border-color: var(--blue); }
/* ββ Status bar ββ */
.status-bar {
background: var(--bg-panel);
border-bottom: 1px solid var(--border-soft);
padding: 0 20px; height: 28px;
display: flex; align-items: center; gap: 12px;
flex-shrink: 0;
}
.status-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--text-muted); transition: background 0.3s; }
.status-dot.idle { background: var(--text-muted); }
.status-dot.running { background: var(--teal); animation: pulse 1.2s infinite; }
.status-dot.done { background: var(--teal); }
.status-dot.error { background: var(--red); }
@keyframes pulse { 0%,100% { opacity:1; } 50% { opacity:0.5; } }
.status-text { font-family: var(--font-mono); font-size: 10px; color: var(--text-muted); letter-spacing: 0.05em; }
.status-meta { margin-left: auto; display: flex; gap: 16px; }
.meta-chip { font-family: var(--font-mono); font-size: 10px; color: var(--text-muted); }
.meta-chip span { color: var(--text-secondary); }
/* ββ Progress bar ββ */
.progress-bar-wrap { height: 2px; background: var(--bg-deep); flex-shrink: 0; overflow: hidden; }
.progress-bar { height: 100%; background: linear-gradient(90deg, var(--teal) 0%, var(--blue) 100%); width: 0%; transition: width 0.3s ease; }
.progress-bar.indeterminate { width: 40%; animation: slide 1.4s ease-in-out infinite; }
@keyframes slide { 0% { transform: translateX(-100%); } 100% { transform: translateX(350%); } }
/* ββ Panels ββ */
.panels { flex: 1; display: grid; grid-template-columns: 1fr 1fr; overflow: hidden; }
.panel { display: flex; flex-direction: column; overflow: hidden; }
.panel-left { border-right: 1px solid var(--border); }
.panel-header {
background: var(--bg-surface);
border-bottom: 1px solid var(--border-soft);
padding: 8px 16px;
display: flex; align-items: center; gap: 10px;
flex-shrink: 0;
}
.panel-label { font-family: var(--font-mono); font-size: 10px; font-weight: 600; letter-spacing: 0.08em; color: var(--text-muted); }
.panel-tag { font-family: var(--font-mono); font-size: 9px; padding: 2px 7px; border-radius: 3px; background: var(--bg-deep); border: 1px solid var(--border); color: var(--text-muted); }
.panel-tag.active { border-color: var(--accent); color: var(--accent); background: rgba(255,107,53,0.08); }
.panel-actions { margin-left: auto; display: flex; gap: 6px; }
.icon-btn { width: 24px; height: 24px; display: flex; align-items: center; justify-content: center; border-radius: 4px; background: transparent; border: 1px solid transparent; cursor: pointer; color: var(--text-muted); font-size: 12px; transition: all 0.15s; }
.icon-btn:hover { background: var(--bg-hover); color: var(--text-primary); border-color: var(--border); }
/* ββ Drop zone ββ */
.drop-zone {
flex: 1;
display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px;
padding: 32px;
border: 2px dashed var(--border);
margin: 16px; border-radius: var(--radius-lg);
cursor: pointer; transition: all 0.2s; text-align: center;
}
.drop-zone.dragging { border-color: var(--accent); background: rgba(255,107,53,0.04); }
.drop-icon { font-size: 40px; opacity: 0.3; filter: grayscale(1); }
.drop-title { font-size: 14px; font-weight: 500; color: var(--text-secondary); }
.drop-hint { font-size: 11px; color: var(--text-muted); font-family: var(--font-mono); }
.drop-formats { display: flex; gap: 6px; }
.format-chip { font-family: var(--font-mono); font-size: 9px; padding: 2px 8px; border-radius: 3px; background: var(--bg-deep); border: 1px solid var(--border); color: var(--text-muted); }
#image-preview { width: 100%; height: 100%; object-fit: contain; background: #000; display: none; }
#image-preview.visible { display: block; }
/* PDF.js canvas viewer */
#pdf-canvas-wrap {
display: none;
flex-direction: column;
align-items: center;
gap: 10px;
overflow-y: auto;
padding: 12px;
height: 100%;
background: #525659;
}
#pdf-canvas-wrap.visible { display: flex; }
#pdf-canvas-wrap canvas {
display: block;
width: 100%;
max-width: 680px;
box-shadow: 0 2px 10px rgba(0,0,0,0.6);
border-radius: 2px;
}
/* ββ Output ββ */
.output-area {
flex: 1; overflow-y: auto;
padding: 16px 20px;
font-family: var(--font-mono); font-size: 12px; line-height: 1.7;
color: var(--text-primary); position: relative;
}
.output-area::-webkit-scrollbar { width: 6px; }
.output-area::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
.output-placeholder { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; color: var(--text-muted); }
.output-placeholder svg { opacity: 0.2; }
.output-placeholder p { font-size: 12px; font-family: var(--font-mono); }
#ocr-text { white-space: pre-wrap; word-break: break-word; }
#artifact-gallery { margin-top: 18px; display: flex; flex-direction: column; gap: 18px; }
.artifact-section-title { font-family: var(--font-mono); font-size: 10px; color: var(--teal); letter-spacing: 0.08em; margin-bottom: 8px; }
.artifact-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 10px; }
.artifact-card { display: flex; flex-direction: column; gap: 6px; color: var(--text-secondary); text-decoration: none; }
.artifact-card img { width: 100%; aspect-ratio: 1 / 1; object-fit: contain; background: #000; border: 1px solid var(--border); border-radius: var(--radius); }
.artifact-card span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 9px; }
.cursor { display: inline-block; width: 8px; height: 14px; background: var(--teal); vertical-align: middle; margin-left: 2px; border-radius: 1px; animation: blink 0.8s step-end infinite; }
@keyframes blink { 0%,100% { opacity:1; } 50% { opacity:0; } }
/* ββ Prompt bar ββ */
.prompt-bar { background: var(--bg-surface); border-top: 1px solid var(--border-soft); padding: 8px 16px; display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.prompt-label { font-family: var(--font-mono); font-size: 10px; color: var(--text-muted); white-space: nowrap; }
.prompt-input { flex: 1; background: var(--bg-deep); border: 1px solid var(--border); border-radius: 5px; padding: 4px 10px; font-family: var(--font-mono); font-size: 11px; color: var(--text-primary); outline: none; transition: border-color 0.2s; }
.prompt-input:focus { border-color: var(--accent); }
.token-counter { font-family: var(--font-mono); font-size: 10px; color: var(--teal); margin-left: auto; }
/* ββ Toast ββ */
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(60px); background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius); padding: 8px 20px; font-family: var(--font-mono); font-size: 11px; color: var(--text-primary); opacity: 0; transition: all 0.3s ease; z-index: 100; white-space: nowrap; }
.toast.show { transform: translateX(-50%) translateY(0); opacity: 1; }
* { scrollbar-width: thin; scrollbar-color: var(--border) transparent; }
</style>
</head>
<body>
<div class="app">
<!-- Header -->
<header>
<div class="logo-area">
<div class="logo-icon">π</div>
<span class="logo-text">Unlimited<span>-OCR</span></span>
</div>
<div class="header-sep"></div>
<div class="mode-group" role="group" aria-label="Inference mode">
<button class="mode-btn active" id="btn-long" onclick="setMode('long')">Long</button>
<button class="mode-btn" id="btn-base" onclick="setMode('base')">Base</button>
</div>
<div class="header-sep"></div>
<div class="ngram-group">
<span class="ngram-label">NGRAM</span>
<label class="toggle-wrap">
<input type="checkbox" id="ngram-toggle" checked />
<div class="toggle-track"></div>
</label>
</div>
<div class="header-actions">
<button class="btn btn-clear" onclick="clearAll()">
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="1 4 1 10 7 10"/><path d="M3.51 15a9 9 0 1 0 .49-3.51"/></svg>
RESET
</button>
<button class="btn btn-copy" id="copy-btn" onclick="copyText()" disabled>
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="9" y="9" width="13" height="13" rx="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg>
COPY
</button>
<button class="btn btn-start" id="start-btn" onclick="startOCR()" disabled>
<svg width="12" height="12" viewBox="0 0 24 24" fill="currentColor"><polygon points="5 3 19 12 5 21 5 3"/></svg>
START
</button>
<button class="btn btn-stop" id="stop-btn" onclick="stopOCR()" disabled>
<svg width="12" height="12" viewBox="0 0 24 24" fill="currentColor"><rect x="6" y="6" width="12" height="12"/></svg>
STOP
</button>
</div>
</header>
<!-- Status bar -->
<div class="status-bar">
<div class="status-dot idle" id="status-dot"></div>
<span class="status-text" id="status-text">IDLE Β· drop or click to load a document</span>
<div class="status-meta">
<span class="meta-chip">MODEL <span>baidu/Unlimited-OCR</span></span>
<span class="meta-chip">MODE <span id="mode-display">LONG</span></span>
<span class="meta-chip" id="page-info" style="display:none">PAGES <span id="page-count">1</span></span>
<span class="meta-chip token-counter" id="token-display" style="display:none"><span id="char-count">0</span> chars</span>
</div>
</div>
<!-- Progress bar -->
<div class="progress-bar-wrap">
<div class="progress-bar" id="progress-bar"></div>
</div>
<!-- Panels -->
<div class="panels">
<!-- Left: input -->
<div class="panel panel-left">
<div class="panel-header">
<span class="panel-label">INPUT</span>
<span class="panel-tag active" id="input-type-tag">EMPTY</span>
<div class="panel-actions">
<button class="icon-btn" title="Upload file" onclick="document.getElementById('file-input').click()">
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" y1="3" x2="12" y2="15"/></svg>
</button>
<button class="icon-btn" title="Toggle zoom" id="zoom-btn" onclick="toggleZoom()" style="display:none">
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="15 3 21 3 21 9"/><polyline points="9 21 3 21 3 15"/><line x1="21" y1="3" x2="14" y2="10"/><line x1="3" y1="21" x2="10" y2="14"/></svg>
</button>
</div>
</div>
<div class="drop-zone" id="drop-zone"
onclick="document.getElementById('file-input').click()"
ondragover="onDragOver(event)"
ondragleave="onDragLeave(event)"
ondrop="onDrop(event)">
<div class="drop-icon">π</div>
<div class="drop-title">Drop your document here</div>
<p class="drop-hint">or click anywhere to browse</p>
<div class="drop-formats">
<span class="format-chip">PNG</span>
<span class="format-chip">JPG</span>
<span class="format-chip">WEBP</span>
<span class="format-chip">PDF</span>
<span class="format-chip">TIFF</span>
</div>
</div>
<img id="image-preview" alt="Document preview" />
<div id="pdf-canvas-wrap"></div>
<div class="prompt-bar">
<span class="prompt-label">PROMPT</span>
<input class="prompt-input" id="prompt-input" value="document parsing." placeholder="e.g. document parsing." />
</div>
</div>
<!-- Right: output -->
<div class="panel">
<div class="panel-header">
<span class="panel-label">OCR OUTPUT</span>
<span class="panel-tag" id="output-tag">WAITING</span>
<div class="panel-actions" style="margin-left:auto">
<button class="icon-btn" title="Scroll to top" onclick="scrollOutputTop()">
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="17 11 12 6 7 11"/><polyline points="17 18 12 13 7 18"/></svg>
</button>
<button class="icon-btn" title="Scroll to bottom" onclick="scrollOutputBottom()">
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="7 13 12 18 17 13"/><polyline points="7 6 12 11 17 6"/></svg>
</button>
</div>
</div>
<div class="output-area" id="output-area">
<div class="output-placeholder" id="output-placeholder">
<svg width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1"><rect x="3" y="3" width="18" height="18" rx="2"/><line x1="3" y1="9" x2="21" y2="9"/><line x1="3" y1="15" x2="21" y2="15"/><line x1="9" y1="9" x2="9" y2="21"/></svg>
<p>OCR output will appear here</p>
<p style="color:var(--border);font-size:10px">Load a document and press START</p>
</div>
<div id="ocr-text"></div>
<div id="artifact-gallery"></div>
</div>
</div>
</div>
</div>
<input type="file" id="file-input" style="display:none" accept="image/*,.pdf" onchange="handleFileInput(event)" />
<div class="toast" id="toast"></div>
<script type="module">
import { Client, handle_file } from "https://cdn.jsdelivr.net/npm/@gradio/client/dist/index.min.js";
import * as pdfjsLib from "https://cdn.jsdelivr.net/npm/pdfjs-dist@4.4.168/build/pdf.min.mjs";
pdfjsLib.GlobalWorkerOptions.workerSrc =
"https://cdn.jsdelivr.net/npm/pdfjs-dist@4.4.168/build/pdf.worker.min.mjs";
// ββ State βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
let client = null;
let currentMode = 'long';
let currentFile = null;
let isPDF = false;
let isRunning = false;
let stopRequested = false;
let charCount = 0;
let zoomedOut = false;
// ββ Boot ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
async function boot() {
try {
client = await Client.connect(window.location.origin);
setStatus('idle', 'READY Β· model loaded');
} catch (e) {
setStatus('error', 'CONNECTION ERROR Β· ' + e.message);
}
}
boot();
// ββ Global handlers (called from onclick="β¦") βββββββββββββββββββββββββββββββββ
window.setMode = (m) => {
currentMode = m;
document.getElementById('btn-long').classList.toggle('active', m === 'long');
document.getElementById('btn-base').classList.toggle('active', m === 'base');
document.getElementById('mode-display').textContent = m.toUpperCase();
};
window.handleFileInput = (e) => { if (e.target.files[0]) loadFile(e.target.files[0]); };
window.onDragOver = (e) => {
e.preventDefault();
document.getElementById('drop-zone').classList.add('dragging');
};
window.onDragLeave = () => {
document.getElementById('drop-zone').classList.remove('dragging');
};
window.onDrop = (e) => {
e.preventDefault();
document.getElementById('drop-zone').classList.remove('dragging');
if (e.dataTransfer.files[0]) loadFile(e.dataTransfer.files[0]);
};
window.startOCR = startOCR;
window.stopOCR = stopOCR;
window.clearAll = clearAll;
window.copyText = copyText;
window.toggleZoom = () => {
zoomedOut = !zoomedOut;
document.getElementById('image-preview').style.objectFit = zoomedOut ? 'cover' : 'contain';
};
window.scrollOutputTop = () => { document.getElementById('output-area').scrollTop = 0; };
window.scrollOutputBottom = () => {
const a = document.getElementById('output-area');
a.scrollTop = a.scrollHeight;
};
// ββ File loading ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
async function loadFile(file) {
currentFile = file;
isPDF = file.type === 'application/pdf' || file.name.toLowerCase().endsWith('.pdf');
// Hide drop zone, reset previews
document.getElementById('drop-zone').style.display = 'none';
document.getElementById('image-preview').classList.remove('visible');
document.getElementById('pdf-canvas-wrap').classList.remove('visible');
document.getElementById('zoom-btn').style.display = 'flex';
document.getElementById('input-type-tag').textContent =
isPDF ? 'PDF' : file.name.split('.').pop().toUpperCase();
if (isPDF) {
// Render PDF pages with PDF.js into canvases (blob: URLs are blocked in iframes on HTTPS)
const wrap = document.getElementById('pdf-canvas-wrap');
wrap.innerHTML = '<div style="color:var(--teal);font-family:var(--font-mono);font-size:11px;padding:20px">Rendering PDFβ¦</div>';
wrap.classList.add('visible');
document.getElementById('page-info').style.display = 'flex';
const buffer = await file.arrayBuffer();
const pdf = await pdfjsLib.getDocument({ data: buffer }).promise;
const total = pdf.numPages;
document.getElementById('page-count').textContent = total;
wrap.innerHTML = '';
for (let i = 1; i <= total; i++) {
const page = await pdf.getPage(i);
const viewport = page.getViewport({ scale: 2.0 });
const canvas = document.createElement('canvas');
canvas.width = viewport.width;
canvas.height = viewport.height;
await page.render({ canvasContext: canvas.getContext('2d'), viewport }).promise;
wrap.appendChild(canvas);
}
} else {
const preview = document.getElementById('image-preview');
preview.src = URL.createObjectURL(file);
preview.classList.add('visible');
}
setStatus('idle', `READY Β· ${file.name}`);
document.getElementById('start-btn').disabled = false;
clearOutput();
}
// ββ OCR entry βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
async function startOCR() {
if (!currentFile || isRunning) return;
if (!client) { showToast('Client not ready yet'); return; }
isRunning = true;
stopRequested = false;
charCount = 0;
clearOutput();
document.getElementById('output-placeholder').style.display = 'none';
document.getElementById('output-tag').textContent = 'RUNNING';
document.getElementById('start-btn').disabled = true;
document.getElementById('stop-btn').disabled = false;
setProgressIndeterminate(true);
const prompt = document.getElementById('prompt-input').value || 'document parsing.';
// Long mode β gundam (640px crop, fast, ZeroGPU-friendly)
// Base mode β base (1024px, more accurate)
const mode = currentMode === 'long' ? 'gundam' : 'base';
const ocrDiv = document.getElementById('ocr-text');
try {
if (isPDF) {
await runPDF(ocrDiv, prompt, mode);
} else {
await runSingleImage(ocrDiv, prompt, mode);
}
} finally {
isRunning = false;
setProgressIndeterminate(false);
document.getElementById('start-btn').disabled = false;
document.getElementById('stop-btn').disabled = true;
}
}
// ββ Single image β streaming via client.submit() ββββββββββββββββββββββββββββ
async function runSingleImage(ocrDiv, prompt, mode) {
ocrDiv.innerHTML = '<span class="cursor"></span>';
setStatus('running', 'IN GPU QUEUEβ¦');
const submission = client.submit('/run_ocr', {
image_path: handle_file(currentFile),
mode,
prompt,
});
let finalText = '';
try {
for await (const msg of submission) {
if (stopRequested) { submission.cancel(); return finishStop(ocrDiv); }
if (msg.type === 'data') {
const payload = msg.data?.[0] ?? {};
const chunk = payload?.text ?? payload ?? '';
finalText = chunk;
ocrDiv.textContent = chunk;
if (payload?.artifacts) renderArtifacts(payload.artifacts, 'Extracted images', false);
window.scrollOutputBottom();
}
if (msg.type === 'status' && msg.stage === 'complete') break;
}
finishDone(finalText);
} catch (err) {
if (stopRequested) return finishStop(ocrDiv);
showError(ocrDiv, err.message);
}
}
// ββ PDF: CPU explode β one 60-second GPU slot per page βββββββββββββββββββββββ
//
// ZeroGPU design pattern:
// β’ Short bursts = higher queue priority
// β’ One page/call = multiple users can be served concurrently
// β’ PDF extraction is CPU-only (no GPU wasted on I/O)
//
async function runPDF(ocrDiv, prompt, mode) {
setStatus('running', 'EXTRACTING PDF PAGES (CPU)β¦');
ocrDiv.innerHTML = '<span class="cursor"></span>';
let pages;
try {
const res = await client.predict('/explode_pdf', {
pdf_file: handle_file(currentFile),
});
pages = res?.data?.[0]?.pages ?? [];
} catch (err) {
if (stopRequested) return finishStop(ocrDiv);
return showError(ocrDiv, 'PDF extraction failed: ' + err.message);
}
if (!pages.length) return showError(ocrDiv, 'No pages found in PDF.');
const total = pages.length;
document.getElementById('page-count').textContent = total;
document.getElementById('page-info').style.display = 'flex';
ocrDiv.textContent = '';
let fullText = '';
for (let i = 0; i < total; i++) {
if (stopRequested) return finishStop(ocrDiv);
setStatus('running', `GPU QUEUE Β· PAGE ${i + 1} / ${total}`);
setProgressValue(Math.round((i / total) * 100));
// Section header
const sep = document.createElement('div');
sep.style.cssText =
'margin:10px 0 5px;font-size:9px;letter-spacing:.1em;' +
'color:var(--text-muted);border-bottom:1px solid var(--border-soft);padding-bottom:3px;';
sep.textContent = `ββ PAGE ${i + 1} / ${total} ββ`;
ocrDiv.appendChild(sep);
// Waiting-for-GPU indicator
const slot = document.createElement('div');
slot.style.cssText = 'color:var(--text-muted);font-size:11px;padding:3px 0;';
slot.innerHTML = '<span class="cursor"></span> waiting for GPU slotβ¦';
ocrDiv.appendChild(slot);
window.scrollOutputBottom();
// Stream each page token-by-token using client.submit()
const pageSub = client.submit('/run_ocr', {
image_path: { path: pages[i].path, orig_name: pages[i].orig_name },
mode,
prompt,
});
try {
let pageText = '';
for await (const msg of pageSub) {
if (stopRequested) { pageSub.cancel(); slot.remove(); return finishStop(ocrDiv); }
if (msg.type === 'data') {
const payload = msg.data?.[0] ?? {};
pageText = payload?.text ?? payload ?? '';
slot.style.color = 'var(--text-primary)';
slot.innerHTML = '';
slot.textContent = pageText;
if (payload?.artifacts) renderArtifacts(payload.artifacts, `Page ${i + 1} images`, true);
window.scrollOutputBottom();
}
if (msg.type === 'status' && msg.stage === 'complete') break;
}
fullText += (fullText ? '\n\n' : '') + pageText;
charCount = fullText.length;
document.getElementById('char-count').textContent = charCount.toLocaleString();
document.getElementById('token-display').style.display = 'flex';
if (!pageText) slot.textContent = '(no text detected)';
} catch (err) {
if (stopRequested) return finishStop(ocrDiv);
slot.innerHTML = `<span style="color:var(--red)">Page ${i + 1} error: ${err.message}</span>`;
}
}
finishDone(fullText);
}
// ββ Stop ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function stopOCR() { stopRequested = true; }
function finishStop(ocrDiv) {
ocrDiv.querySelectorAll('.cursor').forEach(c => c.remove());
setStatus('idle', 'STOPPED by user');
document.getElementById('output-tag').textContent = 'STOPPED';
setProgressIndeterminate(false);
isRunning = false;
document.getElementById('start-btn').disabled = false;
document.getElementById('stop-btn').disabled = true;
}
// ββ Done / error ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function finishDone(text) {
charCount = text.length;
setProgressValue(100);
setStatus('done', `DONE Β· ${charCount.toLocaleString()} chars`);
document.getElementById('output-tag').textContent = 'DONE';
document.getElementById('token-display').style.display = 'flex';
document.getElementById('char-count').textContent = charCount.toLocaleString();
document.getElementById('copy-btn').disabled = false;
showToast('OCR complete');
}
function showError(ocrDiv, msg) {
ocrDiv.innerHTML = `<span style="color:var(--red)">ERROR: ${msg}</span>`;
setStatus('error', 'ERROR Β· ' + msg);
document.getElementById('output-tag').textContent = 'ERROR';
}
// ββ Utilities βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function renderArtifacts(artifacts, title = 'Extracted images', append = false) {
const gallery = document.getElementById('artifact-gallery');
if (!append) gallery.innerHTML = '';
const annotated = artifacts?.annotated ?? [];
const extracted = artifacts?.extracted ?? [];
const groups = [
{ title: `${title} Β· annotated`, items: annotated },
{ title: `${title} Β· cropped`, items: extracted },
];
for (const group of groups) {
if (!group.items.length) continue;
const section = document.createElement('section');
const heading = document.createElement('div');
heading.className = 'artifact-section-title';
heading.textContent = group.title.toUpperCase();
const grid = document.createElement('div');
grid.className = 'artifact-grid';
for (const item of group.items) {
const link = document.createElement('a');
link.className = 'artifact-card';
link.href = item.data_url;
link.download = item.name;
link.target = '_blank';
link.rel = 'noopener';
const img = document.createElement('img');
img.src = item.data_url;
img.alt = item.name;
img.loading = 'lazy';
const label = document.createElement('span');
label.textContent = item.name;
link.appendChild(img);
link.appendChild(label);
grid.appendChild(link);
}
section.appendChild(heading);
section.appendChild(grid);
gallery.appendChild(section);
}
}
function clearOutput() {
document.getElementById('ocr-text').innerHTML = '';
document.getElementById('artifact-gallery').innerHTML = '';
charCount = 0;
document.getElementById('output-placeholder').style.display = 'flex';
document.getElementById('output-tag').textContent = 'WAITING';
document.getElementById('token-display').style.display = 'none';
document.getElementById('copy-btn').disabled = true;
setProgressValue(0);
}
function clearAll() {
if (isRunning) stopOCR();
currentFile = null; isPDF = false;
document.getElementById('drop-zone').style.display = '';
const p = document.getElementById('image-preview');
p.classList.remove('visible'); p.src = '';
const v = document.getElementById('pdf-canvas-wrap');
v.classList.remove('visible'); v.innerHTML = '';
document.getElementById('zoom-btn').style.display = 'none';
document.getElementById('input-type-tag').textContent = 'EMPTY';
document.getElementById('start-btn').disabled = true;
document.getElementById('page-info').style.display = 'none';
document.getElementById('file-input').value = '';
clearOutput();
setStatus('idle', 'IDLE Β· drop or click to load a document');
}
async function copyText() {
const text = document.getElementById('ocr-text').innerText;
if (!text.trim()) {
showToast('Nothing to copy');
return;
}
try {
if (navigator.clipboard?.writeText && window.isSecureContext) {
await navigator.clipboard.writeText(text);
showToast('Copied to clipboard');
return;
}
throw new Error('Clipboard API unavailable');
} catch (_) {
const textarea = document.createElement('textarea');
textarea.value = text;
textarea.setAttribute('readonly', '');
textarea.style.position = 'fixed';
textarea.style.top = '-9999px';
textarea.style.left = '-9999px';
document.body.appendChild(textarea);
textarea.focus();
textarea.select();
try {
const copied = document.execCommand('copy');
showToast(copied ? 'Copied to clipboard' : 'Copy failed β select text manually');
} finally {
document.body.removeChild(textarea);
}
}
}
function setStatus(type, msg) {
document.getElementById('status-dot').className = 'status-dot ' + type;
document.getElementById('status-text').textContent = msg;
}
function setProgressIndeterminate(on) {
const bar = document.getElementById('progress-bar');
bar.className = 'progress-bar' + (on ? ' indeterminate' : '');
if (!on) bar.style.width = '0%';
}
function setProgressValue(v) {
const bar = document.getElementById('progress-bar');
bar.className = 'progress-bar';
bar.style.width = v + '%';
}
let _toastTimer = null;
function showToast(msg) {
const t = document.getElementById('toast');
t.textContent = msg;
t.classList.add('show');
clearTimeout(_toastTimer);
_toastTimer = setTimeout(() => t.classList.remove('show'), 2600);
}
</script>
</body>
</html>
|