Spaces:
Running on Zero
Running on Zero
Download make_examples.py from nutrientdocs/nutrient-document-decision-v2-demo: direct link, hf CLI and curl.
- Browser
- Download file 8.43 kB
-
https://huggingface.co/spaces/nutrientdocs/nutrient-document-decision-v2-demo/resolve/main/make_examples.py
- Command line
-
hf download hf://spaces/nutrientdocs/nutrient-document-decision-v2-demo/make_examples.py
-
curl -L -o make_examples.py https://huggingface.co/spaces/nutrientdocs/nutrient-document-decision-v2-demo/resolve/main/make_examples.py
8.43 kB
| """Renders the demo's synthetic example pages (fictional companies and people) at 150 dpi, A4: two invoices, a | |
| packing slip attached to the first, and a report page with headings, a table and a chart. w9_page1.png is page 1 of | |
| the IRS Form W-9 (Rev. March 2024; a US government work, public domain).""" | |
| import io | |
| import os | |
| import matplotlib | |
| matplotlib.use("Agg") | |
| import matplotlib.pyplot as plt # noqa: E402 | |
| from PIL import Image, ImageDraw, ImageFont # noqa: E402 | |
| W, H = 1240, 1754 | |
| FD = os.path.join(os.path.dirname(matplotlib.__file__), "mpl-data", "fonts", "ttf") | |
| F = lambda size, bold=False: ImageFont.truetype(os.path.join(FD, "DejaVuSans-Bold.ttf" if bold else "DejaVuSans.ttf"), size) | |
| OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "examples") | |
| def page(): | |
| im = Image.new("RGB", (W, H), "white") | |
| return im, ImageDraw.Draw(im) | |
| def table(d, x, y, cols, rows, widths, size=24): | |
| d.rectangle([x, y, x + sum(widths), y + 44], fill="#e9eef5") | |
| cx = x | |
| for c, w in zip(cols, widths): | |
| d.text((cx + 12, y + 9), c, font=F(size, True), fill="black"); cx += w | |
| y += 44 | |
| for r in rows: | |
| cx = x | |
| for c, w in zip(r, widths): | |
| d.text((cx + 12, y + 10), c, font=F(size), fill="black"); cx += w | |
| y += 46 | |
| d.line([x, y, x + sum(widths), y], fill="#c8ced6", width=1) | |
| return y | |
| def signature(d, x, y): | |
| pts = [(x, y + 30), (x + 25, y), (x + 40, y + 35), (x + 60, y + 5), (x + 80, y + 40), (x + 105, y + 10), (x + 130, y + 30), | |
| (x + 160, y + 15), (x + 200, y + 28)] | |
| d.line(pts, fill="#1a2b6d", width=4, joint="curve") | |
| def invoice_ardent(): | |
| im, d = page() | |
| d.text((90, 90), "Ardent Office Supply GmbH", font=F(40, True), fill="#1d3557") | |
| d.text((90, 145), "Hafenstraße 12 · 20457 Hamburg · Germany · VAT DE 812 345 678", font=F(22), fill="#444") | |
| d.text((W - 380, 90), "INVOICE", font=F(56, True), fill="#1d3557") | |
| for i, (k, v) in enumerate([("Invoice no.", "INV-2026-0418"), ("Date", "2 November 2026"), ("Due", "2 December 2026")]): | |
| d.text((W - 440, 180 + 38 * i), k, font=F(24), fill="#555"); d.text((W - 260, 180 + 38 * i), v, font=F(24, True), fill="black") | |
| d.text((90, 330), "Bill to", font=F(24, True), fill="#555") | |
| for i, l in enumerate(["Lindqvist Architects AB", "Attn: Maja Lindqvist", "Sveavägen 44, 111 34 Stockholm, Sweden"]): | |
| d.text((90, 370 + 36 * i), l, font=F(26), fill="black") | |
| y = table(d, 90, 540, ["Description", "Qty", "Unit price", "Amount"], | |
| [["Ergonomic desk chair, model K2", "6", "€310.00", "€1,860.00"], ["Height-adjustable desk 160 cm", "6", "€540.00", "€3,240.00"], | |
| ["Monitor arm, dual", "6", "€95.00", "€570.00"], ["Delivery and assembly", "1", "€380.00", "€380.00"]], | |
| [560, 120, 200, 180]) | |
| for i, (k, v) in enumerate([("Subtotal", "€6,050.00"), ("VAT 0% (reverse charge)", "€0.00")]): | |
| d.text((640, y + 30 + 40 * i), k, font=F(24), fill="#555"); d.text((960, y + 30 + 40 * i), v, font=F(24), fill="black") | |
| d.rectangle([620, y + 120, 1150, y + 180], fill="#1d3557") | |
| d.text((640, y + 132), "Total due", font=F(30, True), fill="white"); d.text((930, y + 132), "€6,050.00", font=F(30, True), fill="white") | |
| d.text((90, y + 260), "Payment details", font=F(26, True), fill="#1d3557") | |
| for i, l in enumerate(["Bank: Commerzbank Hamburg", "IBAN: DE89 3704 0044 0532 0130 00", "BIC: COBADEFFXXX", | |
| "Reference: INV-2026-0418"]): | |
| d.text((90, y + 305 + 38 * i), l, font=F(24), fill="black") | |
| d.text((90, H - 330), "Authorised signature", font=F(22), fill="#555") | |
| signature(d, 100, H - 290); d.line([90, H - 240, 520, H - 240], fill="#888", width=2) | |
| d.text((90, H - 225), "Jonas Reiter, Accounts", font=F(22), fill="#555") | |
| d.text((90, H - 120), "Thank you for your business. Late payments accrue 1% interest per month.", font=F(20), fill="#777") | |
| im.save(os.path.join(OUT, "invoice_ardent.png")) | |
| def packing_slip(): | |
| im, d = page() | |
| d.text((90, 90), "Ardent Office Supply GmbH", font=F(34, True), fill="#1d3557") | |
| d.text((90, 160), "DELIVERY NOTE DN-7781", font=F(44, True), fill="black") | |
| d.text((90, 230), "Attachment to invoice INV-2026-0418 · shipped 3 November 2026", font=F(24), fill="#444") | |
| d.text((90, 300), "Deliver to: Lindqvist Architects AB, Sveavägen 44, Stockholm", font=F(24), fill="black") | |
| y = table(d, 90, 380, ["Item", "Ordered", "Delivered", "Packages"], | |
| [["Ergonomic desk chair, model K2", "6", "6", "6"], ["Height-adjustable desk 160 cm", "6", "6", "12"], | |
| ["Monitor arm, dual", "6", "6", "1"]], [560, 160, 160, 180]) | |
| d.text((90, y + 60), "Received in good condition:", font=F(24), fill="#555") | |
| d.line([90, y + 170, 520, y + 170], fill="#888", width=2); d.text((90, y + 180), "Signature / date", font=F(20), fill="#777") | |
| im.save(os.path.join(OUT, "packing_slip.png")) | |
| def invoice_blueharbor(): | |
| im, d = page() | |
| d.rectangle([0, 0, W, 200], fill="#0b6e4f") | |
| d.text((90, 60), "BLUE HARBOR LOGISTICS LTD", font=F(42, True), fill="white") | |
| d.text((90, 125), "4 Quay Lane, Leith, Edinburgh EH6 6LX · Company no. SC512233", font=F(22), fill="#d8f3e8") | |
| d.text((90, 260), "Invoice BH-55102", font=F(40, True), fill="black") | |
| d.text((90, 320), "Issued 9 November 2026 · Payment terms 14 days", font=F(24), fill="#444") | |
| d.text((90, 390), "Customer: Lindqvist Architects AB (account LQ-204)", font=F(24), fill="black") | |
| y = table(d, 90, 470, ["Service", "Date", "Amount"], | |
| [["Freight Hamburg → Stockholm, 2 pallets", "3 Nov", "£640.00"], ["Customs documentation", "3 Nov", "£85.00"], | |
| ["Fuel surcharge 12%", "", "£76.80"]], [640, 200, 220]) | |
| d.text((620, y + 40), "Total GBP", font=F(30, True), fill="black"); d.text((900, y + 40), "£801.80", font=F(30, True), fill="black") | |
| d.text((90, y + 160), "Pay to sort code 80-22-60, account 10203040, or IBAN GB33 BUKB 2020 1555 5555 55.", font=F(22), fill="black") | |
| im.save(os.path.join(OUT, "invoice_blueharbor.png")) | |
| def report_page(): | |
| im, d = page() | |
| d.text((90, 80), "Harbourside Medical Group: Quarterly Operations Report, Q3 2026", font=F(22), fill="#777") | |
| d.text((90, 140), "2. Patient throughput", font=F(44, True), fill="black") | |
| body = ["Outpatient visits rose 8% over Q2, driven by the new evening clinic in Leith.", | |
| "Average waiting time for a first appointment fell from 19 to 14 days. Table 1", | |
| "lists the visits by site; Figure 1 shows the monthly trend for the two largest sites."] | |
| for i, l in enumerate(body): | |
| d.text((90, 220 + 36 * i), l, font=F(24), fill="black") | |
| d.text((90, 370), "Table 1. Outpatient visits by site", font=F(24, True), fill="black") | |
| y = table(d, 90, 410, ["Site", "Q2 2026", "Q3 2026", "Change"], | |
| [["Leith", "12,410", "14,020", "+13%"], ["Morningside", "9,880", "10,150", "+3%"], ["Portobello", "6,215", "6,390", "+3%"], | |
| ["Total", "28,505", "30,560", "+8%"]], [380, 220, 220, 200]) | |
| fig, ax = plt.subplots(figsize=(7.2, 3.6), dpi=150) | |
| ax.bar([x - 0.2 for x in range(3)], [4410, 4690, 4920], 0.4, label="Leith", color="#1d5fa8") | |
| ax.bar([x + 0.2 for x in range(3)], [3320, 3390, 3440], 0.4, label="Morningside", color="#8fb3d9") | |
| ax.set_xticks(range(3)); ax.set_xticklabels(["July", "August", "September"]); ax.set_ylabel("visits"); ax.legend(frameon=False, loc="upper left", bbox_to_anchor=(0, 1.15), ncol=2) | |
| for s in ("top", "right"): | |
| ax.spines[s].set_visible(False) | |
| fig.tight_layout(); buf = io.BytesIO(); fig.savefig(buf, format="png"); plt.close(fig) | |
| ch = Image.open(buf); im.paste(ch, (int((W - ch.width) / 2), y + 60)) | |
| yc = y + 60 + ch.height | |
| d.text((90, yc + 20), "Figure 1. Monthly outpatient visits, Leith and Morningside.", font=F(24, True), fill="black") | |
| d.text((90, yc + 100), "3. Staffing", font=F(44, True), fill="black") | |
| d.text((90, yc + 180), "Two locum GPs joined in August; nurse vacancies fell from 7 to 3.", font=F(24), fill="black") | |
| d.text((W / 2 - 20, H - 80), "– 4 –", font=F(22), fill="#777") | |
| im.save(os.path.join(OUT, "report_page.png")) | |
| if __name__ == "__main__": | |
| os.makedirs(OUT, exist_ok=True) | |
| invoice_ardent(); packing_slip(); invoice_blueharbor(); report_page() | |
| print(sorted(os.listdir(OUT))) | |