Add WebGPU demo (#1)
Browse files- Delete style.css (585793139918ab742943858996497d82dc451ec6)
- Delete index.js (598879cddb02ac1212a818ff2451c29e18b5ddce)
- Upload 6 files (55165a50b7a06a4230b28b7ccaace09a80c563c7)
Co-authored-by: Joshua <[email protected]>
- .gitattributes +1 -0
- assets/chart.png +0 -0
- assets/code.jpg +0 -0
- assets/document.png +3 -0
- assets/table.jpg +0 -0
- index.html +528 -26
- index.js +0 -76
- parser.js +442 -0
- style.css +0 -76
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
assets/document.png filter=lfs diff=lfs merge=lfs -text
|
assets/chart.png
ADDED
|
assets/code.jpg
ADDED
|
assets/document.png
ADDED
|
Git LFS Details
|
assets/table.jpg
ADDED
|
index.html
CHANGED
|
@@ -1,29 +1,531 @@
|
|
| 1 |
-
<!
|
| 2 |
<html lang="en">
|
| 3 |
-
|
| 4 |
-
<head>
|
| 5 |
<meta charset="UTF-8" />
|
| 6 |
-
<link rel="stylesheet" href="style.css" />
|
| 7 |
-
|
| 8 |
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
| 9 |
-
<title>
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
<
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|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en">
|
| 3 |
+
<head>
|
|
|
|
| 4 |
<meta charset="UTF-8" />
|
|
|
|
|
|
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
| 6 |
+
<title>Granite Docling Image Converter</title>
|
| 7 |
+
<script src="https://cdn.tailwindcss.com"></script>
|
| 8 |
+
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet" />
|
| 9 |
+
<style>
|
| 10 |
+
body {
|
| 11 |
+
font-family: "Inter", sans-serif;
|
| 12 |
+
}
|
| 13 |
+
.loader {
|
| 14 |
+
border-top-color: #3498db;
|
| 15 |
+
animation: spin 1s linear infinite;
|
| 16 |
+
}
|
| 17 |
+
.loader-large {
|
| 18 |
+
border: 8px solid #e5e7eb;
|
| 19 |
+
border-top: 8px solid #3498db;
|
| 20 |
+
animation: spin 1s linear infinite;
|
| 21 |
+
}
|
| 22 |
+
.loader-small {
|
| 23 |
+
border: 4px solid #e5e7eb;
|
| 24 |
+
border-top: 4px solid #3498db;
|
| 25 |
+
animation: spin 1s linear infinite;
|
| 26 |
+
}
|
| 27 |
+
@keyframes spin {
|
| 28 |
+
0% {
|
| 29 |
+
transform: rotate(0deg);
|
| 30 |
+
}
|
| 31 |
+
100% {
|
| 32 |
+
transform: rotate(360deg);
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
/* Custom toggle switch */
|
| 36 |
+
.toggle-checkbox:checked {
|
| 37 |
+
right: 0;
|
| 38 |
+
border-color: #4f46e5;
|
| 39 |
+
}
|
| 40 |
+
.toggle-checkbox:checked + .toggle-label {
|
| 41 |
+
background-color: #4f46e5;
|
| 42 |
+
}
|
| 43 |
+
.overlay {
|
| 44 |
+
background-color: rgba(255, 255, 0, 0.3);
|
| 45 |
+
border: 2px solid #fbbf24;
|
| 46 |
+
transition: background-color 0.2s;
|
| 47 |
+
}
|
| 48 |
+
.overlay:hover {
|
| 49 |
+
background-color: rgba(255, 255, 0, 0.7);
|
| 50 |
+
}
|
| 51 |
+
</style>
|
| 52 |
+
</head>
|
| 53 |
+
<body class="bg-gray-100 text-gray-800 antialiased">
|
| 54 |
+
<div id="model-loader-overlay" class="fixed inset-0 bg-black bg-opacity-60 flex flex-col items-center justify-center z-50">
|
| 55 |
+
<div class="loader-large ease-linear rounded-full h-24 w-24 mb-4"></div>
|
| 56 |
+
<h2 class="text-center text-white text-xl font-semibold">Loading Model...</h2>
|
| 57 |
+
<p class="text-center text-white text-md mt-2">This may take a moment. The model is being downloaded to your browser.</p>
|
| 58 |
+
<progress id="model-progress" value="0" max="100" class="w-64 mt-4 bg-gray-200 rounded-full h-2"></progress>
|
| 59 |
+
<p id="progress-text" class="text-center text-white text-sm mt-2">0%</p>
|
| 60 |
+
</div>
|
| 61 |
+
|
| 62 |
+
<main class="container mx-auto p-4 md:p-8">
|
| 63 |
+
<header class="text-center mb-8">
|
| 64 |
+
<h1 class="text-4xl font-bold text-gray-900">Granite Docling WebGPU</h1>
|
| 65 |
+
<p class="text-lg text-gray-600 mt-2">Convert document images to HTML using 🤗 Transformers.js!</p>
|
| 66 |
+
</header>
|
| 67 |
+
|
| 68 |
+
<div class="grid grid-cols-1 lg:grid-cols-2 gap-8">
|
| 69 |
+
<!-- Left Panel: Image Input -->
|
| 70 |
+
<div class="bg-white p-6 rounded-lg shadow-md">
|
| 71 |
+
<h2 class="text-2xl font-semibold mb-4">1. Select an Image</h2>
|
| 72 |
+
|
| 73 |
+
<div
|
| 74 |
+
id="image-drop-area"
|
| 75 |
+
class="border-2 border-dashed border-gray-300 rounded-lg p-8 text-center cursor-pointer transition-colors duration-200 hover:border-indigo-500 hover:bg-indigo-50"
|
| 76 |
+
>
|
| 77 |
+
<div id="image-placeholder">
|
| 78 |
+
<svg class="mx-auto h-12 w-12 text-gray-400" stroke="currentColor" fill="none" viewBox="0 0 48 48" aria-hidden="true">
|
| 79 |
+
<path
|
| 80 |
+
d="M28 8H12a4 4 0 00-4 4v20m32-12v8m0 0v8a4 4 0 01-4 4H12a4 4 0 01-4-4v-4m32-4l-3.172-3.172a4 4 0 00-5.656 0L28 28M8 32l9.172-9.172a4 4 0 015.656 0L28 28m0 0l4 4m4-24h8m-4-4v8"
|
| 81 |
+
stroke-width="2"
|
| 82 |
+
stroke-linecap="round"
|
| 83 |
+
stroke-linejoin="round"
|
| 84 |
+
/>
|
| 85 |
+
</svg>
|
| 86 |
+
<p class="mt-2 text-sm text-gray-600">
|
| 87 |
+
<span class="font-semibold text-indigo-600">Drag and drop</span>
|
| 88 |
+
or click to select a file
|
| 89 |
+
</p>
|
| 90 |
+
<p class="text-xs text-gray-500">PNG, JPG, WEBP</p>
|
| 91 |
+
<input type="file" id="file-input" class="hidden" accept="image/*" />
|
| 92 |
+
</div>
|
| 93 |
+
<div id="image-preview-container" class="hidden relative">
|
| 94 |
+
<img id="image-preview" src="" alt="Selected image" class="mx-auto rounded-md shadow-sm" />
|
| 95 |
+
<button
|
| 96 |
+
id="remove-image-btn"
|
| 97 |
+
class="absolute top-2 right-2 z-10 bg-red-500 text-white rounded-full p-2 hover:bg-red-600 transition-colors focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500"
|
| 98 |
+
>
|
| 99 |
+
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
|
| 100 |
+
<path
|
| 101 |
+
fill-rule="evenodd"
|
| 102 |
+
d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"
|
| 103 |
+
clip-rule="evenodd"
|
| 104 |
+
/>
|
| 105 |
+
</svg>
|
| 106 |
+
</button>
|
| 107 |
+
</div>
|
| 108 |
+
</div>
|
| 109 |
+
|
| 110 |
+
<div class="mt-4 flex">
|
| 111 |
+
<input
|
| 112 |
+
type="text"
|
| 113 |
+
id="prompt-input"
|
| 114 |
+
class="flex-1 px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
|
| 115 |
+
value="Convert this page to docling."
|
| 116 |
+
/>
|
| 117 |
+
<button
|
| 118 |
+
id="generate-btn"
|
| 119 |
+
class="ml-2 px-4 py-2 bg-indigo-600 text-white rounded-md hover:bg-indigo-700 disabled:bg-gray-400 disabled:cursor-not-allowed"
|
| 120 |
+
>
|
| 121 |
+
Generate
|
| 122 |
+
</button>
|
| 123 |
+
</div>
|
| 124 |
+
|
| 125 |
+
<h3 class="text-lg font-semibold mt-6 mb-3" id="examples-title">Or try an example:</h3>
|
| 126 |
+
<div class="flex space-x-4 overflow-x-auto" id="examples-container">
|
| 127 |
+
<img
|
| 128 |
+
src="./assets/document.png"
|
| 129 |
+
class="example-image h-36 w-auto border-2 border-gray-200 rounded-md cursor-pointer hover:border-indigo-500 transition-colors"
|
| 130 |
+
alt="Example document"
|
| 131 |
+
data-prompt="Convert this page to docling."
|
| 132 |
+
title="Document parsing"
|
| 133 |
+
/>
|
| 134 |
+
<img
|
| 135 |
+
src="./assets/chart.png"
|
| 136 |
+
class="example-image h-36 w-auto border-2 border-gray-200 rounded-md cursor-pointer hover:border-indigo-500 transition-colors"
|
| 137 |
+
alt="Example chart"
|
| 138 |
+
data-prompt="Convert chart to OTSL."
|
| 139 |
+
title="Chart parsing"
|
| 140 |
+
/>
|
| 141 |
+
<img
|
| 142 |
+
src="./assets/table.jpg"
|
| 143 |
+
class="example-image h-36 w-auto border-2 border-gray-200 rounded-md cursor-pointer hover:border-indigo-500 transition-colors"
|
| 144 |
+
alt="Example table"
|
| 145 |
+
data-prompt="Convert this table to OTSL."
|
| 146 |
+
title="Table parsing"
|
| 147 |
+
/>
|
| 148 |
+
<img
|
| 149 |
+
src="./assets/code.jpg"
|
| 150 |
+
class="example-image h-36 w-auto border-2 border-gray-200 rounded-md cursor-pointer hover:border-indigo-500 transition-colors"
|
| 151 |
+
alt="Example code"
|
| 152 |
+
data-prompt="Convert code to text."
|
| 153 |
+
title="Code parsing"
|
| 154 |
+
/>
|
| 155 |
+
</div>
|
| 156 |
+
</div>
|
| 157 |
+
|
| 158 |
+
<!-- Right Panel: Output -->
|
| 159 |
+
<div class="bg-white p-6 rounded-lg shadow-md flex flex-col">
|
| 160 |
+
<div class="flex justify-between items-center mb-4">
|
| 161 |
+
<h2 class="text-2xl font-semibold">2. View Result</h2>
|
| 162 |
+
<div id="processing-indicator" class="flex items-center space-x-2 text-gray-500 hidden">
|
| 163 |
+
<div class="loader-small ease-linear rounded-full h-6 w-6"></div>
|
| 164 |
+
<p class="text-sm">Processing image...</p>
|
| 165 |
+
</div>
|
| 166 |
+
<div class="flex items-center space-x-2">
|
| 167 |
+
<span class="text-sm font-medium">Docling</span>
|
| 168 |
+
<div class="relative inline-block w-10 mr-2 align-middle select-none transition duration-200 ease-in">
|
| 169 |
+
<input
|
| 170 |
+
type="checkbox"
|
| 171 |
+
name="toggle"
|
| 172 |
+
id="view-toggle"
|
| 173 |
+
class="toggle-checkbox absolute block w-6 h-6 rounded-full bg-white border-4 appearance-none cursor-pointer"
|
| 174 |
+
checked
|
| 175 |
+
/>
|
| 176 |
+
<label for="view-toggle" class="toggle-label block overflow-hidden h-6 rounded-full bg-gray-300 cursor-pointer"></label>
|
| 177 |
+
</div>
|
| 178 |
+
<span class="text-sm font-medium text-indigo-600">HTML</span>
|
| 179 |
+
</div>
|
| 180 |
+
</div>
|
| 181 |
+
|
| 182 |
+
<div id="output-container" class="flex-1 border border-gray-200 rounded-lg overflow-hidden bg-gray-50">
|
| 183 |
+
<div id="welcome-message" class="h-full flex items-center justify-center text-center text-gray-500">
|
| 184 |
+
<p>Select an image to see the result here.</p>
|
| 185 |
+
</div>
|
| 186 |
+
|
| 187 |
+
<!-- Docling Output -->
|
| 188 |
+
<div id="docling-view" class="h-full p-4 hidden">
|
| 189 |
+
<pre class="h-full whitespace-pre-wrap text-sm overflow-auto"><code id="docling-output"></code></pre>
|
| 190 |
+
</div>
|
| 191 |
+
|
| 192 |
+
<!-- HTML Output -->
|
| 193 |
+
<div id="html-view" class="h-full w-full">
|
| 194 |
+
<iframe id="html-iframe" sandbox="allow-scripts" class="w-full h-full border-0"></iframe>
|
| 195 |
+
</div>
|
| 196 |
+
</div>
|
| 197 |
+
</div>
|
| 198 |
+
</div>
|
| 199 |
+
</main>
|
| 200 |
+
|
| 201 |
+
<!-- Hidden canvas for image processing -->
|
| 202 |
+
<canvas id="hidden-canvas" class="hidden"></canvas>
|
| 203 |
+
|
| 204 |
+
<script type="module">
|
| 205 |
+
import { AutoProcessor, AutoModelForVision2Seq, RawImage, TextStreamer, load_image } from "https://cdn.jsdelivr.net/npm/@huggingface/[email protected]";
|
| 206 |
+
import { doclingToHtml } from "./parser.js";
|
| 207 |
+
|
| 208 |
+
const modelLoaderOverlay = document.getElementById("model-loader-overlay");
|
| 209 |
+
const imageDropArea = document.getElementById("image-drop-area");
|
| 210 |
+
const imagePlaceholder = document.getElementById("image-placeholder");
|
| 211 |
+
const imagePreviewContainer = document.getElementById("image-preview-container");
|
| 212 |
+
const imagePreview = document.getElementById("image-preview");
|
| 213 |
+
const removeImageBtn = document.getElementById("remove-image-btn");
|
| 214 |
+
const fileInput = document.getElementById("file-input");
|
| 215 |
+
const exampleImages = document.querySelectorAll(".example-image");
|
| 216 |
+
const examplesContainer = document.getElementById("examples-container");
|
| 217 |
+
const examplesTitle = document.getElementById("examples-title");
|
| 218 |
+
|
| 219 |
+
const processingIndicator = document.getElementById("processing-indicator");
|
| 220 |
+
const welcomeMessage = document.getElementById("welcome-message");
|
| 221 |
+
const doclingView = document.getElementById("docling-view");
|
| 222 |
+
const htmlView = document.getElementById("html-view");
|
| 223 |
+
const doclingOutput = document.getElementById("docling-output");
|
| 224 |
+
const htmlIframe = document.getElementById("html-iframe");
|
| 225 |
+
const viewToggle = document.getElementById("view-toggle");
|
| 226 |
+
const hiddenCanvas = document.getElementById("hidden-canvas");
|
| 227 |
+
const promptInput = document.getElementById("prompt-input");
|
| 228 |
+
const generateBtn = document.getElementById("generate-btn");
|
| 229 |
+
|
| 230 |
+
let model, processor;
|
| 231 |
+
let currentImageWidth, currentImageHeight;
|
| 232 |
+
let currentImage = null;
|
| 233 |
+
|
| 234 |
+
/**
|
| 235 |
+
* Loads and initializes the model and processor.
|
| 236 |
+
*/
|
| 237 |
+
async function initializeModel() {
|
| 238 |
+
try {
|
| 239 |
+
const model_id = "onnx-community/granite-docling-258M-ONNX";
|
| 240 |
+
processor = await AutoProcessor.from_pretrained(model_id);
|
| 241 |
+
|
| 242 |
+
const progress = {};
|
| 243 |
+
model = await AutoModelForVision2Seq.from_pretrained(model_id, {
|
| 244 |
+
dtype: {
|
| 245 |
+
embed_tokens: "fp16", // fp32 (231 MB) | fp16 (116 MB)
|
| 246 |
+
vision_encoder: "fp32", // fp32 (374 MB)
|
| 247 |
+
decoder_model_merged: "fp32", // fp32 (658 MB) | q4 (105 MB), q4 sometimes into repetition issues
|
| 248 |
+
},
|
| 249 |
+
device: "webgpu",
|
| 250 |
+
progress_callback: (data) => {
|
| 251 |
+
if (data.status === "progress" && data.file?.endsWith?.("onnx_data")) {
|
| 252 |
+
progress[data.file] = data;
|
| 253 |
+
const progressPercent = Math.round(data.progress);
|
| 254 |
+
|
| 255 |
+
if (Object.keys(progress).length !== 3) return;
|
| 256 |
+
let sum = 0;
|
| 257 |
+
let total = 0;
|
| 258 |
+
for (const [key, val] of Object.entries(progress)) {
|
| 259 |
+
sum += val.loaded;
|
| 260 |
+
total += val.total;
|
| 261 |
+
}
|
| 262 |
+
|
| 263 |
+
const overallPercent = Math.round((sum / total) * 100);
|
| 264 |
+
document.getElementById("model-progress").value = overallPercent;
|
| 265 |
+
document.getElementById("progress-text").textContent = overallPercent + "%";
|
| 266 |
+
}
|
| 267 |
+
},
|
| 268 |
+
});
|
| 269 |
+
modelLoaderOverlay.style.display = "none";
|
| 270 |
+
console.log("Model loaded successfully.");
|
| 271 |
+
} catch (error) {
|
| 272 |
+
console.error("Failed to load model:", error);
|
| 273 |
+
modelLoaderOverlay.innerHTML = `
|
| 274 |
+
<h2 class="text-center text-red-500 text-xl font-semibold">Failed to Load Model</h2>
|
| 275 |
+
<p class="text-center text-white text-md mt-2">Please refresh the page to try again. Check the console for errors.</p>
|
| 276 |
+
`;
|
| 277 |
+
}
|
| 278 |
+
}
|
| 279 |
+
|
| 280 |
+
/**
|
| 281 |
+
* Processes an image and generates Docling text.
|
| 282 |
+
* @param {ImageBitmap|HTMLImageElement} imageObject An image object to process.
|
| 283 |
+
*/
|
| 284 |
+
async function processImage(imageObject) {
|
| 285 |
+
if (!model || !processor) {
|
| 286 |
+
alert("Model is not loaded yet. Please wait.");
|
| 287 |
+
return;
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
// Reset UI
|
| 291 |
+
setUiState("processing");
|
| 292 |
+
clearOverlays();
|
| 293 |
+
let fullText = "";
|
| 294 |
+
doclingOutput.textContent = "";
|
| 295 |
+
htmlIframe.srcdoc = "";
|
| 296 |
+
|
| 297 |
+
try {
|
| 298 |
+
// 1. Draw image to canvas and get RawImage
|
| 299 |
+
const ctx = hiddenCanvas.getContext("2d");
|
| 300 |
+
hiddenCanvas.width = imageObject.width;
|
| 301 |
+
hiddenCanvas.height = imageObject.height;
|
| 302 |
+
ctx.drawImage(imageObject, 0, 0);
|
| 303 |
+
const image = RawImage.fromCanvas(hiddenCanvas);
|
| 304 |
+
|
| 305 |
+
// 2. Create input messages
|
| 306 |
+
const messages = [
|
| 307 |
+
{
|
| 308 |
+
role: "user",
|
| 309 |
+
content: [{ type: "image" }, { type: "text", text: promptInput.value }],
|
| 310 |
+
},
|
| 311 |
+
];
|
| 312 |
+
|
| 313 |
+
// 3. Prepare inputs for the model
|
| 314 |
+
const text = processor.apply_chat_template(messages, {
|
| 315 |
+
add_generation_prompt: true,
|
| 316 |
+
});
|
| 317 |
+
const inputs = await processor(text, [image], {
|
| 318 |
+
do_image_splitting: true,
|
| 319 |
+
});
|
| 320 |
+
// 5. Generate output
|
| 321 |
+
await model.generate({
|
| 322 |
+
...inputs,
|
| 323 |
+
max_new_tokens: 4096,
|
| 324 |
+
streamer: new TextStreamer(processor.tokenizer, {
|
| 325 |
+
skip_prompt: true,
|
| 326 |
+
skip_special_tokens: false,
|
| 327 |
+
callback_function: (streamedText) => {
|
| 328 |
+
fullText += streamedText;
|
| 329 |
+
doclingOutput.textContent += streamedText;
|
| 330 |
+
},
|
| 331 |
+
}),
|
| 332 |
+
});
|
| 333 |
+
|
| 334 |
+
// Strip <|end_of_text|> from the end
|
| 335 |
+
fullText = fullText.replace(/<\|end_of_text\|>$/, "");
|
| 336 |
+
doclingOutput.textContent = fullText;
|
| 337 |
+
|
| 338 |
+
// Parse loc tags and create overlays
|
| 339 |
+
const locRegex = /<loc_(\d+)>/g;
|
| 340 |
+
const locMatches = [];
|
| 341 |
+
let match;
|
| 342 |
+
while ((match = locRegex.exec(fullText)) !== null) {
|
| 343 |
+
locMatches.push(parseInt(match[1]));
|
| 344 |
+
}
|
| 345 |
+
const overlays = [];
|
| 346 |
+
for (let i = 0; i < locMatches.length; i += 4) {
|
| 347 |
+
if (i + 3 < locMatches.length) {
|
| 348 |
+
overlays.push(locMatches.slice(i, i + 4));
|
| 349 |
+
}
|
| 350 |
+
}
|
| 351 |
+
const imgRect = imagePreview.getBoundingClientRect();
|
| 352 |
+
const containerRect = imagePreviewContainer.getBoundingClientRect();
|
| 353 |
+
const imageOffsetLeft = imgRect.left - containerRect.left;
|
| 354 |
+
const imageOffsetTop = imgRect.top - containerRect.top;
|
| 355 |
+
const scaleX = imgRect.width / currentImageWidth;
|
| 356 |
+
const scaleY = imgRect.height / currentImageHeight;
|
| 357 |
+
overlays.forEach(([leftLoc, topLoc, rightLoc, bottomLoc]) => {
|
| 358 |
+
const left = imageOffsetLeft + (leftLoc / 500) * currentImageWidth * scaleX;
|
| 359 |
+
const top = imageOffsetTop + (topLoc / 500) * currentImageHeight * scaleY;
|
| 360 |
+
const width = ((rightLoc - leftLoc) / 500) * currentImageWidth * scaleX;
|
| 361 |
+
const height = ((bottomLoc - topLoc) / 500) * currentImageHeight * scaleY;
|
| 362 |
+
const overlay = document.createElement("div");
|
| 363 |
+
overlay.className = "overlay";
|
| 364 |
+
overlay.style.position = "absolute";
|
| 365 |
+
overlay.style.left = left + "px";
|
| 366 |
+
overlay.style.top = top + "px";
|
| 367 |
+
overlay.style.width = width + "px";
|
| 368 |
+
overlay.style.height = height + "px";
|
| 369 |
+
imagePreviewContainer.appendChild(overlay);
|
| 370 |
+
});
|
| 371 |
+
|
| 372 |
+
// After generation, create the HTML iframe
|
| 373 |
+
htmlIframe.srcdoc = doclingToHtml(fullText);
|
| 374 |
+
} catch (error) {
|
| 375 |
+
console.error("Error during image processing:", error);
|
| 376 |
+
doclingOutput.textContent = `An error occurred: ${error.message}`;
|
| 377 |
+
} finally {
|
| 378 |
+
setUiState("result");
|
| 379 |
+
}
|
| 380 |
+
}
|
| 381 |
+
|
| 382 |
+
/**
|
| 383 |
+
* Handles the selection of an image file.
|
| 384 |
+
* @param {File|string} source The image file or URL.
|
| 385 |
+
*/
|
| 386 |
+
function handleImageSelection(source) {
|
| 387 |
+
const reader = new FileReader();
|
| 388 |
+
const img = new Image();
|
| 389 |
+
|
| 390 |
+
img.onload = () => {
|
| 391 |
+
currentImageWidth = img.naturalWidth;
|
| 392 |
+
currentImageHeight = img.naturalHeight;
|
| 393 |
+
currentImage = img;
|
| 394 |
+
imagePreview.src = img.src;
|
| 395 |
+
imagePlaceholder.classList.add("hidden");
|
| 396 |
+
imagePreviewContainer.classList.remove("hidden");
|
| 397 |
+
examplesContainer.classList.add("hidden");
|
| 398 |
+
examplesTitle.classList.add("hidden");
|
| 399 |
+
processImage(img);
|
| 400 |
+
};
|
| 401 |
+
|
| 402 |
+
img.onerror = () => {
|
| 403 |
+
alert("Failed to load image.");
|
| 404 |
+
};
|
| 405 |
+
|
| 406 |
+
if (typeof source === "string") {
|
| 407 |
+
// It's a URL
|
| 408 |
+
// To avoid CORS issues with canvas, we can try to fetch it first
|
| 409 |
+
fetch(source)
|
| 410 |
+
.then((res) => res.blob())
|
| 411 |
+
.then((blob) => {
|
| 412 |
+
img.src = URL.createObjectURL(blob);
|
| 413 |
+
})
|
| 414 |
+
.catch((e) => {
|
| 415 |
+
console.error("CORS issue likely. Trying proxy or direct load.", e);
|
| 416 |
+
// Fallback to direct load which might taint the canvas
|
| 417 |
+
img.crossOrigin = "anonymous";
|
| 418 |
+
img.src = source;
|
| 419 |
+
});
|
| 420 |
+
} else {
|
| 421 |
+
// It's a File object
|
| 422 |
+
reader.onload = (e) => {
|
| 423 |
+
img.src = e.target.result;
|
| 424 |
+
};
|
| 425 |
+
reader.readAsDataURL(source);
|
| 426 |
+
}
|
| 427 |
+
}
|
| 428 |
+
|
| 429 |
+
/**
|
| 430 |
+
* Manages the visibility of UI components based on the app state.
|
| 431 |
+
* @param {'initial'|'processing'|'result'} state The current state.
|
| 432 |
+
*/
|
| 433 |
+
function setUiState(state) {
|
| 434 |
+
welcomeMessage.style.display = "none";
|
| 435 |
+
processingIndicator.classList.add("hidden");
|
| 436 |
+
doclingView.classList.add("hidden");
|
| 437 |
+
htmlView.classList.add("hidden");
|
| 438 |
+
|
| 439 |
+
if (state === "initial") {
|
| 440 |
+
welcomeMessage.style.display = "flex";
|
| 441 |
+
generateBtn.disabled = true;
|
| 442 |
+
} else if (state === "processing") {
|
| 443 |
+
viewToggle.checked = false;
|
| 444 |
+
processingIndicator.classList.remove("hidden");
|
| 445 |
+
doclingView.classList.remove("hidden");
|
| 446 |
+
generateBtn.disabled = true;
|
| 447 |
+
} else if (state === "result") {
|
| 448 |
+
viewToggle.checked = true;
|
| 449 |
+
htmlView.classList.remove("hidden");
|
| 450 |
+
generateBtn.disabled = false;
|
| 451 |
+
}
|
| 452 |
+
}
|
| 453 |
+
|
| 454 |
+
/**
|
| 455 |
+
* Clears all overlay divs from the image preview container.
|
| 456 |
+
*/
|
| 457 |
+
function clearOverlays() {
|
| 458 |
+
document.querySelectorAll(".overlay").forEach((el) => el.remove());
|
| 459 |
+
}
|
| 460 |
+
|
| 461 |
+
// Drag and Drop
|
| 462 |
+
imageDropArea.addEventListener("click", () => fileInput.click());
|
| 463 |
+
imageDropArea.addEventListener("dragover", (e) => {
|
| 464 |
+
e.preventDefault();
|
| 465 |
+
imageDropArea.classList.add("border-indigo-500", "bg-indigo-50");
|
| 466 |
+
});
|
| 467 |
+
imageDropArea.addEventListener("dragleave", () => {
|
| 468 |
+
imageDropArea.classList.remove("border-indigo-500", "bg-indigo-50");
|
| 469 |
+
});
|
| 470 |
+
imageDropArea.addEventListener("drop", (e) => {
|
| 471 |
+
e.preventDefault();
|
| 472 |
+
imageDropArea.classList.remove("border-indigo-500", "bg-indigo-50");
|
| 473 |
+
const files = e.dataTransfer.files;
|
| 474 |
+
if (files.length > 0 && files[0].type.startsWith("image/")) {
|
| 475 |
+
handleImageSelection(files[0]);
|
| 476 |
+
}
|
| 477 |
+
});
|
| 478 |
+
|
| 479 |
+
// File input
|
| 480 |
+
fileInput.addEventListener("change", (e) => {
|
| 481 |
+
const files = e.target.files;
|
| 482 |
+
if (files.length > 0) {
|
| 483 |
+
handleImageSelection(files[0]);
|
| 484 |
+
}
|
| 485 |
+
});
|
| 486 |
+
|
| 487 |
+
// Example images
|
| 488 |
+
exampleImages.forEach((img) => {
|
| 489 |
+
img.addEventListener("click", () => {
|
| 490 |
+
promptInput.value = img.dataset.prompt;
|
| 491 |
+
handleImageSelection(img.src);
|
| 492 |
+
});
|
| 493 |
+
});
|
| 494 |
+
|
| 495 |
+
// Remove image
|
| 496 |
+
removeImageBtn.addEventListener("click", (e) => {
|
| 497 |
+
e.stopPropagation();
|
| 498 |
+
currentImage = null;
|
| 499 |
+
imagePreview.src = "";
|
| 500 |
+
fileInput.value = ""; // Reset file input
|
| 501 |
+
imagePlaceholder.classList.remove("hidden");
|
| 502 |
+
imagePreviewContainer.classList.add("hidden");
|
| 503 |
+
examplesContainer.classList.remove("hidden");
|
| 504 |
+
examplesTitle.classList.remove("hidden");
|
| 505 |
+
setUiState("initial");
|
| 506 |
+
doclingOutput.textContent = "";
|
| 507 |
+
htmlIframe.srcdoc = "";
|
| 508 |
+
clearOverlays();
|
| 509 |
+
});
|
| 510 |
+
|
| 511 |
+
// View toggle
|
| 512 |
+
viewToggle.addEventListener("change", () => {
|
| 513 |
+
const isHtmlView = viewToggle.checked;
|
| 514 |
+
htmlView.classList.toggle("hidden", !isHtmlView);
|
| 515 |
+
doclingView.classList.toggle("hidden", isHtmlView);
|
| 516 |
+
});
|
| 517 |
+
|
| 518 |
+
// Generate button
|
| 519 |
+
generateBtn.addEventListener("click", () => {
|
| 520 |
+
if (currentImage) {
|
| 521 |
+
processImage(currentImage);
|
| 522 |
+
}
|
| 523 |
+
});
|
| 524 |
+
|
| 525 |
+
document.addEventListener("DOMContentLoaded", () => {
|
| 526 |
+
setUiState("initial"); // Set initial view correctly
|
| 527 |
+
initializeModel();
|
| 528 |
+
});
|
| 529 |
+
</script>
|
| 530 |
+
</body>
|
| 531 |
+
</html>
|
index.js
DELETED
|
@@ -1,76 +0,0 @@
|
|
| 1 |
-
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/[email protected]';
|
| 2 |
-
|
| 3 |
-
// Reference the elements that we will need
|
| 4 |
-
const status = document.getElementById('status');
|
| 5 |
-
const fileUpload = document.getElementById('upload');
|
| 6 |
-
const imageContainer = document.getElementById('container');
|
| 7 |
-
const example = document.getElementById('example');
|
| 8 |
-
|
| 9 |
-
const EXAMPLE_URL = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/city-streets.jpg';
|
| 10 |
-
|
| 11 |
-
// Create a new object detection pipeline
|
| 12 |
-
status.textContent = 'Loading model...';
|
| 13 |
-
const detector = await pipeline('object-detection', 'Xenova/detr-resnet-50');
|
| 14 |
-
status.textContent = 'Ready';
|
| 15 |
-
|
| 16 |
-
example.addEventListener('click', (e) => {
|
| 17 |
-
e.preventDefault();
|
| 18 |
-
detect(EXAMPLE_URL);
|
| 19 |
-
});
|
| 20 |
-
|
| 21 |
-
fileUpload.addEventListener('change', function (e) {
|
| 22 |
-
const file = e.target.files[0];
|
| 23 |
-
if (!file) {
|
| 24 |
-
return;
|
| 25 |
-
}
|
| 26 |
-
|
| 27 |
-
const reader = new FileReader();
|
| 28 |
-
|
| 29 |
-
// Set up a callback when the file is loaded
|
| 30 |
-
reader.onload = e2 => detect(e2.target.result);
|
| 31 |
-
|
| 32 |
-
reader.readAsDataURL(file);
|
| 33 |
-
});
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
// Detect objects in the image
|
| 37 |
-
async function detect(img) {
|
| 38 |
-
imageContainer.innerHTML = '';
|
| 39 |
-
imageContainer.style.backgroundImage = `url(${img})`;
|
| 40 |
-
|
| 41 |
-
status.textContent = 'Analysing...';
|
| 42 |
-
const output = await detector(img, {
|
| 43 |
-
threshold: 0.5,
|
| 44 |
-
percentage: true,
|
| 45 |
-
});
|
| 46 |
-
status.textContent = '';
|
| 47 |
-
output.forEach(renderBox);
|
| 48 |
-
}
|
| 49 |
-
|
| 50 |
-
// Render a bounding box and label on the image
|
| 51 |
-
function renderBox({ box, label }) {
|
| 52 |
-
const { xmax, xmin, ymax, ymin } = box;
|
| 53 |
-
|
| 54 |
-
// Generate a random color for the box
|
| 55 |
-
const color = '#' + Math.floor(Math.random() * 0xFFFFFF).toString(16).padStart(6, 0);
|
| 56 |
-
|
| 57 |
-
// Draw the box
|
| 58 |
-
const boxElement = document.createElement('div');
|
| 59 |
-
boxElement.className = 'bounding-box';
|
| 60 |
-
Object.assign(boxElement.style, {
|
| 61 |
-
borderColor: color,
|
| 62 |
-
left: 100 * xmin + '%',
|
| 63 |
-
top: 100 * ymin + '%',
|
| 64 |
-
width: 100 * (xmax - xmin) + '%',
|
| 65 |
-
height: 100 * (ymax - ymin) + '%',
|
| 66 |
-
})
|
| 67 |
-
|
| 68 |
-
// Draw label
|
| 69 |
-
const labelElement = document.createElement('span');
|
| 70 |
-
labelElement.textContent = label;
|
| 71 |
-
labelElement.className = 'bounding-box-label';
|
| 72 |
-
labelElement.style.backgroundColor = color;
|
| 73 |
-
|
| 74 |
-
boxElement.appendChild(labelElement);
|
| 75 |
-
imageContainer.appendChild(boxElement);
|
| 76 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
parser.js
ADDED
|
@@ -0,0 +1,442 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export class DoclingConverter {
|
| 2 |
+
constructor() {
|
| 3 |
+
this.simpleTagMap = {
|
| 4 |
+
doctag: "div",
|
| 5 |
+
document: "div",
|
| 6 |
+
ordered_list: "ol",
|
| 7 |
+
unordered_list: "ul",
|
| 8 |
+
list_item: "li",
|
| 9 |
+
caption: "figcaption",
|
| 10 |
+
footnote: "sup",
|
| 11 |
+
formula: "div",
|
| 12 |
+
page_footer: "footer",
|
| 13 |
+
page_header: "header",
|
| 14 |
+
picture: "figure",
|
| 15 |
+
chart: "figure",
|
| 16 |
+
table: "table",
|
| 17 |
+
otsl: "table",
|
| 18 |
+
text: "p",
|
| 19 |
+
paragraph: "p",
|
| 20 |
+
title: "h1",
|
| 21 |
+
document_index: "div",
|
| 22 |
+
form: "form",
|
| 23 |
+
key_value_region: "dl",
|
| 24 |
+
reference: "a",
|
| 25 |
+
smiles: "span",
|
| 26 |
+
};
|
| 27 |
+
this.selfClosingTagMap = {
|
| 28 |
+
checkbox_selected: '<input type="checkbox" checked disabled>',
|
| 29 |
+
checkbox_unselected: '<input type="checkbox" disabled>',
|
| 30 |
+
page_break: '<hr class="page-break">',
|
| 31 |
+
};
|
| 32 |
+
this.TABLE_TAG_CONFIG = {
|
| 33 |
+
"<ched>": { htmlTag: "th" },
|
| 34 |
+
"<rhed>": { htmlTag: "th", scope: "row" },
|
| 35 |
+
"<srow>": { htmlTag: "th", scope: "row" },
|
| 36 |
+
"<fcel>": { htmlTag: "td" },
|
| 37 |
+
"<ecel>": { htmlTag: "td" },
|
| 38 |
+
"<ucel>": { htmlTag: "td" },
|
| 39 |
+
"<lcel>": { htmlTag: "td" },
|
| 40 |
+
"<xcel>": { htmlTag: "td" },
|
| 41 |
+
};
|
| 42 |
+
this.TABLE_TAG_REGEX = new RegExp(`(${Object.keys(this.TABLE_TAG_CONFIG).join("|")})`);
|
| 43 |
+
const selfClosingNames = Object.keys(this.selfClosingTagMap).join("|");
|
| 44 |
+
this.combinedTagRegex = new RegExp(`(<([a-z_0-9]+)>(.*?)<\\/\\2>)|(<(${selfClosingNames})>)`, "s");
|
| 45 |
+
}
|
| 46 |
+
escapeHtml(text) {
|
| 47 |
+
if (!text) return "";
|
| 48 |
+
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
| 49 |
+
}
|
| 50 |
+
convert(docling) {
|
| 51 |
+
let html = ` ${docling} `;
|
| 52 |
+
html = this.cleanupMetadataTokens(html);
|
| 53 |
+
html = this.processTags(html);
|
| 54 |
+
return html.trim();
|
| 55 |
+
}
|
| 56 |
+
processTags(text) {
|
| 57 |
+
let remainingText = text;
|
| 58 |
+
let result = "";
|
| 59 |
+
while (remainingText.length > 0) {
|
| 60 |
+
const match = remainingText.match(this.combinedTagRegex);
|
| 61 |
+
if (match && typeof match.index === "number") {
|
| 62 |
+
const textBefore = remainingText.substring(0, match.index);
|
| 63 |
+
result += this.escapeHtml(textBefore);
|
| 64 |
+
const fullMatch = match[0];
|
| 65 |
+
const pairedTagName = match[2];
|
| 66 |
+
const pairedContent = match[3];
|
| 67 |
+
const selfClosingTagName = match[5];
|
| 68 |
+
if (pairedTagName !== undefined) {
|
| 69 |
+
result += this.convertSingleTag(pairedTagName, pairedContent);
|
| 70 |
+
} else if (selfClosingTagName !== undefined) {
|
| 71 |
+
result += this.selfClosingTagMap[selfClosingTagName] || "";
|
| 72 |
+
}
|
| 73 |
+
remainingText = remainingText.substring(match.index + fullMatch.length);
|
| 74 |
+
} else {
|
| 75 |
+
result += this.escapeHtml(remainingText);
|
| 76 |
+
break;
|
| 77 |
+
}
|
| 78 |
+
}
|
| 79 |
+
return result;
|
| 80 |
+
}
|
| 81 |
+
convertSingleTag(tagName, content) {
|
| 82 |
+
if (tagName === "list_item") {
|
| 83 |
+
content = content.trim().replace(/^[·-]\s*/g, "");
|
| 84 |
+
}
|
| 85 |
+
switch (tagName) {
|
| 86 |
+
case "code":
|
| 87 |
+
return this.convertBlockCode(content);
|
| 88 |
+
case "otsl":
|
| 89 |
+
return this.convertTable(content);
|
| 90 |
+
case "picture":
|
| 91 |
+
case "chart":
|
| 92 |
+
return this.convertPictureOrChart(tagName, content);
|
| 93 |
+
case "inline":
|
| 94 |
+
return this.convertInlineContent(content);
|
| 95 |
+
case "section_header_level_0":
|
| 96 |
+
case "section_header_level_1":
|
| 97 |
+
case "section_header_level_2":
|
| 98 |
+
case "section_header_level_3":
|
| 99 |
+
case "section_header_level_4":
|
| 100 |
+
case "section_header_level_5":
|
| 101 |
+
const level = parseInt(tagName.at(-1), 10) + 1;
|
| 102 |
+
return `<h${level}>${this.processTags(content)}</h${level}>`;
|
| 103 |
+
default:
|
| 104 |
+
const htmlTag = this.simpleTagMap[tagName];
|
| 105 |
+
if (htmlTag) {
|
| 106 |
+
const processedContent = this.processTags(content);
|
| 107 |
+
const startTag = this.getStartTag(tagName, htmlTag);
|
| 108 |
+
return `${startTag}${processedContent}</${htmlTag}>`;
|
| 109 |
+
}
|
| 110 |
+
console.warn(`Unknown tag encountered: ${tagName}, escaping it.`);
|
| 111 |
+
return this.escapeHtml(`<${tagName}>${content}</${tagName}>`);
|
| 112 |
+
}
|
| 113 |
+
}
|
| 114 |
+
getStartTag(doclingTag, htmlTag) {
|
| 115 |
+
switch (doclingTag) {
|
| 116 |
+
case "doctag":
|
| 117 |
+
case "document":
|
| 118 |
+
return '<div class="docling-document">';
|
| 119 |
+
case "formula":
|
| 120 |
+
return '<div class="formula">';
|
| 121 |
+
case "document_index":
|
| 122 |
+
return '<div class="toc">';
|
| 123 |
+
case "smiles":
|
| 124 |
+
return '<span class="smiles">';
|
| 125 |
+
case "reference":
|
| 126 |
+
return '<a href="#">';
|
| 127 |
+
default:
|
| 128 |
+
return `<${htmlTag}>`;
|
| 129 |
+
}
|
| 130 |
+
}
|
| 131 |
+
convertInlineContent(content) {
|
| 132 |
+
const inlineTagRegex = /<(code|formula|text|smiles)>(.*?)<\/\1>/s;
|
| 133 |
+
let remainingText = content;
|
| 134 |
+
let result = "";
|
| 135 |
+
while (remainingText.length > 0) {
|
| 136 |
+
const match = remainingText.match(inlineTagRegex);
|
| 137 |
+
if (match && typeof match.index === "number") {
|
| 138 |
+
const textBefore = remainingText.substring(0, match.index);
|
| 139 |
+
result += this.escapeHtml(textBefore);
|
| 140 |
+
const [fullMatch, tagName, innerContent] = match;
|
| 141 |
+
switch (tagName) {
|
| 142 |
+
case "code":
|
| 143 |
+
const langRegex = /<_(.*?)_>/;
|
| 144 |
+
const langMatch = innerContent.match(langRegex);
|
| 145 |
+
if (langMatch && langMatch[1]) {
|
| 146 |
+
const language = this.sanitizeLanguageName(langMatch[1]);
|
| 147 |
+
const codeContent = innerContent.replace(langRegex, "").trim();
|
| 148 |
+
const escapedCode = this.escapeHtml(codeContent);
|
| 149 |
+
const langClass = language !== "unknown" ? ` class="language-${language}"` : "";
|
| 150 |
+
result += `<code${langClass}>${escapedCode}</code>`;
|
| 151 |
+
} else {
|
| 152 |
+
result += `<code>${this.escapeHtml(innerContent)}</code>`;
|
| 153 |
+
}
|
| 154 |
+
break;
|
| 155 |
+
case "formula":
|
| 156 |
+
result += `<span class="formula">${this.escapeHtml(innerContent)}</span>`;
|
| 157 |
+
break;
|
| 158 |
+
case "smiles":
|
| 159 |
+
result += `<span class="smiles">${this.escapeHtml(innerContent)}</span>`;
|
| 160 |
+
break;
|
| 161 |
+
case "text":
|
| 162 |
+
result += this.escapeHtml(innerContent);
|
| 163 |
+
break;
|
| 164 |
+
}
|
| 165 |
+
remainingText = remainingText.substring(match.index + fullMatch.length);
|
| 166 |
+
} else {
|
| 167 |
+
result += this.escapeHtml(remainingText);
|
| 168 |
+
break;
|
| 169 |
+
}
|
| 170 |
+
}
|
| 171 |
+
return result;
|
| 172 |
+
}
|
| 173 |
+
convertBlockCode(content) {
|
| 174 |
+
const langRegex = /<_(.*?)_>/;
|
| 175 |
+
const langMatch = content.match(langRegex);
|
| 176 |
+
let language = "unknown";
|
| 177 |
+
let codeContent = content;
|
| 178 |
+
if (langMatch && langMatch[1]) {
|
| 179 |
+
language = this.sanitizeLanguageName(langMatch[1]);
|
| 180 |
+
codeContent = content.replace(langRegex, "").trim();
|
| 181 |
+
}
|
| 182 |
+
const escapedCode = this.escapeHtml(codeContent);
|
| 183 |
+
const langClass = language !== "unknown" ? ` class="language-${language}"` : "";
|
| 184 |
+
return `<pre><code${langClass}>${escapedCode}</code></pre>`;
|
| 185 |
+
}
|
| 186 |
+
convertTable(content) {
|
| 187 |
+
const rows = content
|
| 188 |
+
.trim()
|
| 189 |
+
.split(/<nl>/)
|
| 190 |
+
.filter((row) => row.length > 0);
|
| 191 |
+
const cellGrid = [];
|
| 192 |
+
rows.forEach((rowStr, rowIndex) => {
|
| 193 |
+
var _a;
|
| 194 |
+
const parts = rowStr.split(this.TABLE_TAG_REGEX);
|
| 195 |
+
const currentRow = [];
|
| 196 |
+
let gridColIndex = 0;
|
| 197 |
+
for (let i = 1; i < parts.length; i += 2) {
|
| 198 |
+
const tag = parts[i];
|
| 199 |
+
const cellContent = parts[i + 1] || "";
|
| 200 |
+
switch (tag) {
|
| 201 |
+
case "<lcel>":
|
| 202 |
+
if (currentRow.length > 0) {
|
| 203 |
+
currentRow[currentRow.length - 1].colspan++;
|
| 204 |
+
}
|
| 205 |
+
break;
|
| 206 |
+
case "<ucel>":
|
| 207 |
+
if (rowIndex > 0 && ((_a = cellGrid[rowIndex - 1]) === null || _a === void 0 ? void 0 : _a[gridColIndex])) {
|
| 208 |
+
cellGrid[rowIndex - 1][gridColIndex].rowspan++;
|
| 209 |
+
}
|
| 210 |
+
gridColIndex++;
|
| 211 |
+
break;
|
| 212 |
+
case "<xcel>":
|
| 213 |
+
if (currentRow.length > 0) {
|
| 214 |
+
currentRow[currentRow.length - 1].colspan++;
|
| 215 |
+
}
|
| 216 |
+
break;
|
| 217 |
+
default:
|
| 218 |
+
if (this.TABLE_TAG_CONFIG[tag]) {
|
| 219 |
+
currentRow.push({
|
| 220 |
+
content: cellContent,
|
| 221 |
+
tag,
|
| 222 |
+
colspan: 1,
|
| 223 |
+
rowspan: 1,
|
| 224 |
+
});
|
| 225 |
+
gridColIndex++;
|
| 226 |
+
}
|
| 227 |
+
break;
|
| 228 |
+
}
|
| 229 |
+
}
|
| 230 |
+
cellGrid.push(currentRow);
|
| 231 |
+
});
|
| 232 |
+
const htmlRows = cellGrid
|
| 233 |
+
.map((row) => {
|
| 234 |
+
const cellsHtml = row
|
| 235 |
+
.map((cell) => {
|
| 236 |
+
const config = this.TABLE_TAG_CONFIG[cell.tag];
|
| 237 |
+
if (!config) return "";
|
| 238 |
+
const attrs = [];
|
| 239 |
+
if (cell.colspan > 1) attrs.push(`colspan="${cell.colspan}"`);
|
| 240 |
+
if (cell.rowspan > 1) attrs.push(`rowspan="${cell.rowspan}"`);
|
| 241 |
+
if (config.scope) attrs.push(`scope="${config.scope}"`);
|
| 242 |
+
const processedContent = this.processTags(cell.content);
|
| 243 |
+
const attrString = attrs.length > 0 ? ` ${attrs.join(" ")}` : "";
|
| 244 |
+
return `<${config.htmlTag}${attrString}>${processedContent}</${config.htmlTag}>`;
|
| 245 |
+
})
|
| 246 |
+
.join("");
|
| 247 |
+
return `<tr>${cellsHtml}</tr>`;
|
| 248 |
+
})
|
| 249 |
+
.join("");
|
| 250 |
+
return `<table><tbody>${htmlRows}</tbody></table>`;
|
| 251 |
+
}
|
| 252 |
+
convertPictureOrChart(tag, content) {
|
| 253 |
+
if (/<(fcel|ched|rhed)>/.test(content)) {
|
| 254 |
+
const cleanedContent = content.replace(/<[a-z_]+>/g, (match) => {
|
| 255 |
+
if (match.startsWith("<fcel") || match.startsWith("<ched") || match.startsWith("<rhed") || match.startsWith("<nl")) {
|
| 256 |
+
return match;
|
| 257 |
+
}
|
| 258 |
+
return "";
|
| 259 |
+
});
|
| 260 |
+
return this.convertTable(cleanedContent);
|
| 261 |
+
}
|
| 262 |
+
let captionHtml = "";
|
| 263 |
+
const captionRegex = /<caption>(.*?)<\/caption>/s;
|
| 264 |
+
const captionMatch = content.match(captionRegex);
|
| 265 |
+
if (captionMatch && captionMatch[1]) {
|
| 266 |
+
const captionContent = this.processTags(captionMatch[1]);
|
| 267 |
+
captionHtml = `<figcaption>${captionContent}</figcaption>`;
|
| 268 |
+
}
|
| 269 |
+
const contentWithoutCaption = content.replace(captionRegex, "");
|
| 270 |
+
const classificationRegex = /<([a-z_]+)>/;
|
| 271 |
+
const classMatch = contentWithoutCaption.match(classificationRegex);
|
| 272 |
+
let altText = tag;
|
| 273 |
+
if (classMatch) {
|
| 274 |
+
altText = classMatch[1].replace(/_/g, " ");
|
| 275 |
+
}
|
| 276 |
+
const imgHtml = `<img alt="${this.escapeHtml(altText)}" src="">`;
|
| 277 |
+
const figureTag = this.simpleTagMap[tag] || "figure";
|
| 278 |
+
return `<${figureTag}>${imgHtml}${captionHtml}</${figureTag}>`;
|
| 279 |
+
}
|
| 280 |
+
sanitizeLanguageName(lang) {
|
| 281 |
+
const lowerLang = lang.toLowerCase();
|
| 282 |
+
const aliasMap = {
|
| 283 |
+
"c#": "csharp",
|
| 284 |
+
"c++": "cpp",
|
| 285 |
+
objectivec: "objective-c",
|
| 286 |
+
visualbasic: "vb",
|
| 287 |
+
javascript: "js",
|
| 288 |
+
typescript: "ts",
|
| 289 |
+
python: "py",
|
| 290 |
+
ruby: "rb",
|
| 291 |
+
dockerfile: "docker",
|
| 292 |
+
};
|
| 293 |
+
return aliasMap[lowerLang] || lowerLang.replace(/[\s#+]/g, "-");
|
| 294 |
+
}
|
| 295 |
+
cleanupMetadataTokens(docling) {
|
| 296 |
+
return docling.replace(/<loc_[0-9]+>/g, "");
|
| 297 |
+
}
|
| 298 |
+
}
|
| 299 |
+
|
| 300 |
+
export function doclingToHtml(docling) {
|
| 301 |
+
const converter = new DoclingConverter();
|
| 302 |
+
const body = converter.convert(docling);
|
| 303 |
+
return `<!DOCTYPE html>
|
| 304 |
+
<html>
|
| 305 |
+
<head>
|
| 306 |
+
<meta charset="UTF-8">
|
| 307 |
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" integrity="sha384-//SZkxyB7axjCAopkAL1E1rve+ZSPKapD89Lo/lLhcsXR+zOYl5z6zJZEFXil+q0" crossorigin="anonymous">
|
| 308 |
+
|
| 309 |
+
<style>
|
| 310 |
+
html {
|
| 311 |
+
background-color: #f5f5f5;
|
| 312 |
+
font-family: Arial, sans-serif;
|
| 313 |
+
line-height: 1.6;
|
| 314 |
+
}
|
| 315 |
+
header, footer {
|
| 316 |
+
text-align: center;
|
| 317 |
+
margin-bottom: 1rem;
|
| 318 |
+
font-size: 1em;
|
| 319 |
+
}
|
| 320 |
+
body {
|
| 321 |
+
max-width: 800px;
|
| 322 |
+
margin: 0 auto;
|
| 323 |
+
padding: 2rem;
|
| 324 |
+
background-color: white;
|
| 325 |
+
box-shadow: 0 0 10px rgba(0,0,0,0.1);
|
| 326 |
+
}
|
| 327 |
+
h1, h2, h3, h4, h5, h6 {
|
| 328 |
+
color: #333;
|
| 329 |
+
margin-top: 1.5em;
|
| 330 |
+
margin-bottom: 0.5em;
|
| 331 |
+
}
|
| 332 |
+
h1 {
|
| 333 |
+
font-size: 2em;
|
| 334 |
+
border-bottom: 1px solid #eee;
|
| 335 |
+
padding-bottom: 0.3em;
|
| 336 |
+
}
|
| 337 |
+
table {
|
| 338 |
+
border-collapse: collapse;
|
| 339 |
+
margin: 1em 0;
|
| 340 |
+
width: 100%;
|
| 341 |
+
}
|
| 342 |
+
th, td {
|
| 343 |
+
border: 1px solid #ddd;
|
| 344 |
+
padding: 8px;
|
| 345 |
+
text-align: left;
|
| 346 |
+
}
|
| 347 |
+
th {
|
| 348 |
+
background-color: #f2f2f2;
|
| 349 |
+
font-weight: bold;
|
| 350 |
+
}
|
| 351 |
+
figure {
|
| 352 |
+
margin: 1.5em 0;
|
| 353 |
+
text-align: center;
|
| 354 |
+
}
|
| 355 |
+
figcaption {
|
| 356 |
+
color: #666;
|
| 357 |
+
font-style: italic;
|
| 358 |
+
margin-top: 0.5em;
|
| 359 |
+
}
|
| 360 |
+
img {
|
| 361 |
+
max-width: 100%;
|
| 362 |
+
height: auto;
|
| 363 |
+
}
|
| 364 |
+
pre {
|
| 365 |
+
background-color: #f6f8fa;
|
| 366 |
+
border-radius: 3px;
|
| 367 |
+
padding: 1em;
|
| 368 |
+
overflow: auto;
|
| 369 |
+
}
|
| 370 |
+
code {
|
| 371 |
+
font-family: monospace;
|
| 372 |
+
background-color: #f6f8fa;
|
| 373 |
+
padding: 0.2em 0.4em;
|
| 374 |
+
border-radius: 3px;
|
| 375 |
+
}
|
| 376 |
+
pre code {
|
| 377 |
+
background-color: transparent;
|
| 378 |
+
padding: 0;
|
| 379 |
+
}
|
| 380 |
+
.formula {
|
| 381 |
+
text-align: center;
|
| 382 |
+
padding: 0.5em;
|
| 383 |
+
margin: 1em 0;
|
| 384 |
+
}
|
| 385 |
+
.formula:not(:has(.katex)) {
|
| 386 |
+
color: transparent;
|
| 387 |
+
}
|
| 388 |
+
.page-break {
|
| 389 |
+
page-break-after: always;
|
| 390 |
+
border-top: 1px dashed #ccc;
|
| 391 |
+
margin: 2em 0;
|
| 392 |
+
}
|
| 393 |
+
.key-value-region {
|
| 394 |
+
background-color: #f9f9f9;
|
| 395 |
+
padding: 1em;
|
| 396 |
+
border-radius: 4px;
|
| 397 |
+
margin: 1em 0;
|
| 398 |
+
}
|
| 399 |
+
.key-value-region dt {
|
| 400 |
+
font-weight: bold;
|
| 401 |
+
}
|
| 402 |
+
.key-value-region dd {
|
| 403 |
+
margin-left: 1em;
|
| 404 |
+
margin-bottom: 0.5em;
|
| 405 |
+
}
|
| 406 |
+
.form-container {
|
| 407 |
+
border: 1px solid #ddd;
|
| 408 |
+
padding: 1em;
|
| 409 |
+
border-radius: 4px;
|
| 410 |
+
margin: 1em 0;
|
| 411 |
+
}
|
| 412 |
+
.form-item {
|
| 413 |
+
margin-bottom: 0.5em;
|
| 414 |
+
}
|
| 415 |
+
</style>
|
| 416 |
+
</head>
|
| 417 |
+
<body>
|
| 418 |
+
${body}
|
| 419 |
+
<script type="module">
|
| 420 |
+
import katex from 'https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.mjs';
|
| 421 |
+
import renderMathInElement from "https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.mjs";
|
| 422 |
+
|
| 423 |
+
const mathElements = document.querySelectorAll('.formula');
|
| 424 |
+
for (let element of mathElements) {
|
| 425 |
+
katex.render(element.textContent, element, {
|
| 426 |
+
throwOnError: false,
|
| 427 |
+
});
|
| 428 |
+
}
|
| 429 |
+
|
| 430 |
+
renderMathInElement(document.body, {
|
| 431 |
+
delimiters: [
|
| 432 |
+
{left: "$$", right: "$$", display: true},
|
| 433 |
+
{left: "\\\\[", right: "\\\\]", display: true},
|
| 434 |
+
{left: "$", right: "$", display: false},
|
| 435 |
+
{left: "\\\\(", right: "\\\\)", display: false}
|
| 436 |
+
],
|
| 437 |
+
throwOnError : false,
|
| 438 |
+
});
|
| 439 |
+
</script>
|
| 440 |
+
</body>
|
| 441 |
+
</html>`;
|
| 442 |
+
}
|
style.css
DELETED
|
@@ -1,76 +0,0 @@
|
|
| 1 |
-
* {
|
| 2 |
-
box-sizing: border-box;
|
| 3 |
-
padding: 0;
|
| 4 |
-
margin: 0;
|
| 5 |
-
font-family: sans-serif;
|
| 6 |
-
}
|
| 7 |
-
|
| 8 |
-
html,
|
| 9 |
-
body {
|
| 10 |
-
height: 100%;
|
| 11 |
-
}
|
| 12 |
-
|
| 13 |
-
body {
|
| 14 |
-
padding: 32px;
|
| 15 |
-
}
|
| 16 |
-
|
| 17 |
-
body,
|
| 18 |
-
#container {
|
| 19 |
-
display: flex;
|
| 20 |
-
flex-direction: column;
|
| 21 |
-
justify-content: center;
|
| 22 |
-
align-items: center;
|
| 23 |
-
}
|
| 24 |
-
|
| 25 |
-
#container {
|
| 26 |
-
position: relative;
|
| 27 |
-
gap: 0.4rem;
|
| 28 |
-
|
| 29 |
-
width: 640px;
|
| 30 |
-
height: 640px;
|
| 31 |
-
max-width: 100%;
|
| 32 |
-
max-height: 100%;
|
| 33 |
-
|
| 34 |
-
border: 2px dashed #D1D5DB;
|
| 35 |
-
border-radius: 0.75rem;
|
| 36 |
-
overflow: hidden;
|
| 37 |
-
cursor: pointer;
|
| 38 |
-
margin: 1rem;
|
| 39 |
-
|
| 40 |
-
background-size: 100% 100%;
|
| 41 |
-
background-position: center;
|
| 42 |
-
background-repeat: no-repeat;
|
| 43 |
-
font-size: 18px;
|
| 44 |
-
}
|
| 45 |
-
|
| 46 |
-
#upload {
|
| 47 |
-
display: none;
|
| 48 |
-
}
|
| 49 |
-
|
| 50 |
-
svg {
|
| 51 |
-
pointer-events: none;
|
| 52 |
-
}
|
| 53 |
-
|
| 54 |
-
#example {
|
| 55 |
-
font-size: 14px;
|
| 56 |
-
text-decoration: underline;
|
| 57 |
-
cursor: pointer;
|
| 58 |
-
}
|
| 59 |
-
|
| 60 |
-
#example:hover {
|
| 61 |
-
color: #2563EB;
|
| 62 |
-
}
|
| 63 |
-
|
| 64 |
-
.bounding-box {
|
| 65 |
-
position: absolute;
|
| 66 |
-
box-sizing: border-box;
|
| 67 |
-
border: solid 2px;
|
| 68 |
-
}
|
| 69 |
-
|
| 70 |
-
.bounding-box-label {
|
| 71 |
-
color: white;
|
| 72 |
-
position: absolute;
|
| 73 |
-
font-size: 12px;
|
| 74 |
-
margin: -16px 0 0 -2px;
|
| 75 |
-
padding: 1px;
|
| 76 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|