Tool · built by Consigliere · November 8, 2026
Invoice duty exposure (sample data)
Paste a supplier invoice as CSV and see the likely HS chapter and a sample duty for each line. Sample data only; a sketch of exposure, not a customs tool.
His notes on this demo
# Invoice duty exposure (SAMPLE DATA) **Goal:** paste a supplier invoice as CSV, and see which HS chapter each line probably falls in and roughly what duty it would carry at a sample rate. ## What it shows Supply chain and AI on one page, with no model server. A small multinomial naive Bayes classifier, trained on 36 hand-written product descriptions, guesses a two-digit HS chapter from each line's description. A lookup table keyed by "chapter|origin" then prices the duty. Lines with no rate for that pair are left unpriced, not counted as zero. Lines the classifier is unsure of (probability under 0.5) are marked for review and still priced. It is not a large language model. Nothing is sent over the network, on load or on click. ## How to open 1. Double-click `index.html` in this folder. It opens in any browser. 2. `sample-data.js` and `model.js` must sit beside it. They load by relative path. 3. The page starts with a sample invoice and a sample rate table. Edit either textarea and press **Price the invoice**. 4. CSV format: header `description,value_usd,origin`, then one line per invoice line. Example check: `1000` at `16.5` for `61|China` gives a duty of 165.00. Check first: the totals line should read invoice value $14,100.00, sample duty on priced lines $1,405.50 (of which on lines for review $84.50), 1 unpriced line, 2 lines for review. The banner says SAMPLE DATA on every view. ## What it is not - Not real data. The 36 training descriptions, the 8 invoice lines and the rate table are invented for the demo. The HS chapter labels are real; the rates are not HTS rates. - The rate column is percent. A rate written as a fraction (`0.165`) passes the 0 to 100 check and is applied as 0.165%, so check the units yourself. - Not a customs tool. It does not know the current tariff, any exemptions, Section 301 or Section 232 measures, or the origin rules. Do not use its numbers for a filing. - Not connected to any supplier or business. It does not work with a real supplier's data. - Not a full CSV reader. `parseCsv` does not handle quotes, so a comma inside a description usually adds a field, and that row is refused: `"pump, centrifugal",1000,China` gives "bad line 2". A row with three fields is read as typed, so `pump, 1000,China` reads as description `pump` with value 1000. Remove commas from descriptions where you can, and check each priced line against your invoice. ## What I am unsure of - **Accuracy is modest.** Leave-one-out on the 36 examples gets 24 of 36 right (66.7%). The number is pinned in `test.mjs`, so a change to the model shows up. - **It leans toward chapter 39.** Eight of the 12 misses were predicted as chapter 39 (plastics). An unseen word adds the most probability to the chapter with the fewest training words. Chapters 39 and 84 have 19 each, against 21 to 23 for the rest, so an unseen word favors them, and when they tie the sort order gives 39. - **The review flag is noisy.** On leave-one-out, every miss has a probability under 0.5, so the flag catches all 12 misses. It also flags 17 of the 24 correct answers, so most lines get flagged. The test pins these counts at the 0.5 threshold. I did not find a confident-but-wrong case in this sample. - **Flagged lines are still priced.** They count in the duty total. The page shows how much of the total sits on them: $84.50 of $1,405.50 in the sample. - **No uncertainty on the duty.** The duty is a product of a guessed chapter and a rate. It is a sketch of exposure, not a range. ## Tests `test.mjs` runs 21 checks against known inputs and exits 1 if any check fails. It is kept with the project source, not in this download. The output below is from a passing run of it. Passing run: ``` ok tokenize lowercases and drops one-letter words ok parseCsv reads typed rows from the sample invoice ok parseCsv rejects a wrong header ok parseCsv numbers a bad line against the original text ok parseCsv rejects a missing value and a blank origin ok train counts 6 chapters, 36 examples and a 93-word vocabulary ok class posteriors sum to 1 and are ordered by probability ok known descriptions get their known chapter ok an unseen word, even one named like an object key, does not zero out a chapter ok an empty description returns the class priors and is flagged for review ok leave-one-out accuracy on the sample is pinned at 24 of 36 ok review flag at 0.5 on leave-one-out: all 12 misses flagged, 17 of 24 correct ones flagged ok checkRates accepts numbers from 0 to 100 and refuses strings, fractions over 100 and NaN ok reviewDuty is the duty on review-flagged lines only ok priceLines: 1000 at 16.5% is a duty of 165 ok a pair with no rate is unpriced (null), not zero ok a rate of 0 is priced at duty 0, not unpriced ok the sample invoice totals match a hand sum: value 14,100, duty 1,405.50 ok the unpriced line is left out of the duty total, not added as zero or NaN ok the low-confidence line is flagged for review ok round2 rounds to cents all checks passed ``` Deliberate breaks, each made in a copy of the demo, with the change confirmed in the copy before the test ran. The repo copy of `model.js` was never edited: - Tokenizer keeps one-letter words (`length > 1` to `length > 0`): 2 checks fail. - Smoothing drops the `+ 1` (Laplace): 7 checks fail, including leave-one-out. - Duty scale off by 10x (`/ 100` to `/ 10`): 2 checks fail. - Review threshold moved from 0.5 to 0.6: 1 check fails (the review-flag counts). - Classify reads the count directly, `(model.counts[c][t] || 0)`, in place of `countOf`: 1 check fails, the unseen-word check, with `non-finite probability`. The other 20 checks pass.
Page updated Build 961 · 29 Nov 2026 · fa5b278