Skip to content

Commit 4214b19

Browse files
artizclaude
andcommitted
wasm demo: Hugging Face fallback for the TableFormer graphs
Mirror the layout-model fallback: try local ./models/tableformer/ first, then the CORS Hugging Face base, so TableFormer works on the hosted (phone) demo. External data is fetched from the same base that served the .onnx. Refs #157 Signed-off-by: artiz <artem.kustikov@gmail.com> Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EY5KAiquN4YpVf2PXEQkVT
1 parent a934e6d commit 4214b19

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

crates/docling-wasm/www/pipeline.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ const REC_MODELS = {
4545
},
4646
};
4747

48-
// TableFormer graphs, loaded same-origin from ./models/tableformer/ (the same
49-
// files download_dependencies.sh fetches). The encoder is self-contained; the
50-
// decoder/bbox use ONNX external data, so their .onnx.data sidecar rides along
51-
// via ort-web's externalData option (path = the location stored in the .onnx).
52-
const TF_DIR = "./models/tableformer/";
48+
// TableFormer graphs: local ./models/tableformer/ first (download_dependencies.sh),
49+
// then the CORS Hugging Face mirror for the hosted demo. The encoder is
50+
// self-contained; the decoder/bbox use ONNX external data, so their .onnx.data
51+
// sidecar rides along via ort-web's externalData option (path = the location
52+
// stored in the .onnx). Fetched lazily — ~380 MB total — only when a table
53+
// profile is chosen.
54+
const TF_DIRS = ["./models/tableformer/", MODEL_BASE + "tableformer/"];
5355

5456
/// The stateful session the wasm TfSession interop expects (see
5557
/// src/tableformer.rs): `encode` runs the image encoder once and stashes the
@@ -208,10 +210,23 @@ export function createOcr({ onStatus }) {
208210
async function ensureTf() {
209211
if (tf) return tf;
210212
const load = async (name, external) => {
211-
const model = await fetchProgress(TF_DIR + name + ".onnx", `tableformer ${name}`);
213+
// Find the first base (local, then HF) that serves the .onnx; take its
214+
// external data from the same base.
215+
let base = null;
216+
let model = null;
217+
for (const b of TF_DIRS) {
218+
try {
219+
model = await fetchProgress(b + name + ".onnx", `tableformer ${name}`);
220+
base = b;
221+
break;
222+
} catch (e) {
223+
/* try the next base */
224+
}
225+
}
226+
if (!model) throw new Error(`tableformer ${name}.onnx not found (local or HF)`);
212227
const opts = { executionProviders: ["wasm"], logSeverityLevel: 3 };
213228
if (external) {
214-
const data = await fetchProgress(TF_DIR + name + ".onnx.data", `tableformer ${name} data`);
229+
const data = await fetchProgress(base + name + ".onnx.data", `tableformer ${name} data`);
215230
opts.externalData = [{ path: name + ".onnx.data", data: new Uint8Array(data) }];
216231
}
217232
return ort.InferenceSession.create(model, opts);

0 commit comments

Comments
 (0)