Spaces:
Running on Zero
Running on Zero
Download tools/make_samples.py from Nikit-K/kynex-extract: direct link, hf CLI and curl.
- Browser
- Download file 14.7 kB
-
https://huggingface.co/spaces/Nikit-K/kynex-extract/resolve/main/tools/make_samples.py
- Command line
-
hf download hf://spaces/Nikit-K/kynex-extract/tools/make_samples.py
-
curl -L -o make_samples.py https://huggingface.co/spaces/Nikit-K/kynex-extract/resolve/main/tools/make_samples.py
14.7 kB
| """Generate the demo documents bundled in this Space (synthetic, no real data).""" | |
| from __future__ import annotations | |
| from pathlib import Path | |
| import fitz | |
| HERE = Path(__file__).resolve().parents[1] | |
| OUT = HERE / "samples" | |
| OUT.mkdir(parents=True, exist_ok=True) | |
| PAGE_W, PAGE_H = 612, 792 | |
| MARGIN = 46 | |
| INK = (0.09, 0.10, 0.13) | |
| MUTED = (0.42, 0.45, 0.50) | |
| RULE = (0.80, 0.82, 0.86) | |
| def text(page, x, y, s, size=9.5, bold=False, color=INK, align="left"): | |
| font = "hebo" if bold else "helv" | |
| width = fitz.get_text_length(s, fontname=font, fontsize=size) | |
| if align == "right": | |
| x -= width | |
| elif align == "center": | |
| x -= width / 2 | |
| page.insert_text((x, y), s, fontname=font, fontsize=size, color=color) | |
| return width | |
| def rule(page, x0, y, x1, color=RULE, width=0.6): | |
| page.draw_line((x0, y), (x1, y), color=color, width=width) | |
| def header(page, doc_title): | |
| text(page, MARGIN, 58, "NORTHWIND INDUSTRIAL SUPPLY", 15, bold=True) | |
| text(page, MARGIN, 74, "4400 Harbor Way, Oakland, CA 94607", 8.5, color=MUTED) | |
| text( | |
| page, | |
| MARGIN, | |
| 86, | |
| "VAT / Tax ID 94-2817733 · accounts@northwind-supply.example", | |
| 8.5, | |
| color=MUTED, | |
| ) | |
| text(page, PAGE_W - MARGIN, 58, doc_title, 22, bold=True, align="right") | |
| rule(page, MARGIN, 100, PAGE_W - MARGIN, color=(0.20, 0.72, 0.60), width=1.4) | |
| def meta_block(page, y): | |
| rows = [ | |
| ("Invoice No.", "KY-2041"), | |
| ("Invoice Date", "2026-03-14"), | |
| ("Due Date", "2026-04-13"), | |
| ("PO Number", "PO-7731"), | |
| ("Terms", "Net 30"), | |
| ("Currency", "USD"), | |
| ] | |
| for i, (label, value) in enumerate(rows): | |
| text(page, PAGE_W - MARGIN - 150, y + i * 14, label, 8.5, color=MUTED) | |
| text(page, PAGE_W - MARGIN, y + i * 14, value, 9.5, bold=True, align="right") | |
| def address_block(page, y, title, lines): | |
| text(page, MARGIN, y, title, 7.5, bold=True, color=MUTED) | |
| for i, line in enumerate(lines): | |
| text(page, MARGIN, y + 14 + i * 12, line, 9.5) | |
| return y + 14 + len(lines) * 12 | |
| ITEMS = [ | |
| (1, "HI-4402", 'Hydraulic hose, 3/4" ID, 2-wire braid', 12, 48.50, 582.00), | |
| (2, "BR-2210", "Brass fittings, pack of 25", 6, 31.75, 190.50), | |
| (3, "FL-9001", "Filter element, 10 micron, spin-on", 24, 12.40, 297.60), | |
| (4, "SE-1180", "Seal kit, 63 mm cylinder", 8, 76.20, 609.60), | |
| (5, "VA-3345", "Directional valve, 4-way, 24 VDC", 3, 412.00, 1236.00), | |
| (6, "GA-0071", "Pressure gauge, 0-400 bar, glycerin filled", 10, 28.95, 289.50), | |
| (7, "PI-5520", 'Steel pipe, 1" schedule 80, 6 m', 40, 9.85, 394.00), | |
| (8, "LB-6630", "Lubricant, ISO VG 46, 5 L", 5, 54.30, 271.50), | |
| (9, "TK-8802", "Tool kit, seal puller and installers", 2, 96.75, 193.50), | |
| ] | |
| COLS = [ | |
| ("Line", 40, "left"), | |
| ("SKU", 95, "left"), | |
| ("Description", 190, "left"), | |
| ("Qty", 452, "right"), | |
| ("Unit price", 512, "right"), | |
| ("Amount", 566, "right"), | |
| ] | |
| def items_table(page, y): | |
| for label, x, align in COLS: | |
| text(page, x, y, label, 8, bold=True, color=MUTED, align=align) | |
| rule(page, MARGIN, y + 5, PAGE_W - MARGIN, color=INK, width=0.8) | |
| y += 17 | |
| for line, sku, desc, qty, price, amount in ITEMS: | |
| text(page, 40, y, str(line), 9) | |
| text(page, 95, y, sku, 9) | |
| text(page, 190, y, desc, 9) | |
| text(page, 452, y, f"{qty}", 9, align="right") | |
| text(page, 512, y, f"{price:,.2f}", 9, align="right") | |
| text(page, 566, y, f"{amount:,.2f}", 9, align="right") | |
| rule(page, MARGIN, y + 5, PAGE_W - MARGIN) | |
| y += 19 | |
| return y | |
| def totals(page, y): | |
| rows = [ | |
| ("Subtotal", "4,064.20"), | |
| ("Discount (2%)", "-81.28"), | |
| ("Shipping (FOB origin)", "145.00"), | |
| ("Tax (8.25%)", "328.59"), | |
| ] | |
| for label, value in rows: | |
| text(page, 500, y, label, 9, color=MUTED, align="right") | |
| text(page, 566, y, value, 9, align="right") | |
| y += 15 | |
| rule(page, 500, y - 5, 566, color=INK, width=0.8) | |
| y += 10 | |
| text(page, 500, y, "Total due (USD)", 10.5, bold=True, align="right") | |
| text(page, 566, y, "4,456.51", 11.5, bold=True, align="right") | |
| return y | |
| SCHEDULE = [ | |
| (1, "HI-4402", 12, "2026-03-20", "OAK-1"), | |
| (2, "BR-2210", 6, "2026-03-20", "OAK-1"), | |
| (3, "FL-9001", 24, "2026-03-24", "SAC-2"), | |
| (4, "SE-1180", 8, "2026-03-24", "SAC-2"), | |
| (5, "VA-3345", 3, "2026-03-27", "RNO-3"), | |
| (6, "GA-0071", 10, "2026-03-27", "RNO-3"), | |
| (7, "PI-5520", 40, "2026-03-31", "OAK-1"), | |
| (8, "LB-6630", 5, "2026-04-02", "SAC-2"), | |
| (9, "TK-8802", 2, "2026-04-02", "RNO-3"), | |
| ] | |
| def page_one(page): | |
| header(page, "INVOICE") | |
| meta_block(page, 126) | |
| y = address_block( | |
| page, | |
| 126, | |
| "VENDOR", | |
| [ | |
| "Northwind Industrial Supply", | |
| "4400 Harbor Way", | |
| "Oakland, CA 94607", | |
| "United States", | |
| ], | |
| ) | |
| address_block( | |
| page, | |
| y + 18, | |
| "BILL TO / SHIP TO", | |
| [ | |
| "Meridian Fabrication Ltd.", | |
| "1180 Foundry Road, Building C", | |
| "Sacramento, CA 95814", | |
| "United States", | |
| ], | |
| ) | |
| y = max(y + 90, 250) | |
| text(page, MARGIN, y, "Line items", 11, bold=True) | |
| y = items_table(page, y + 18) | |
| totals(page, y + 18) | |
| rule(page, MARGIN, 700, PAGE_W - MARGIN) | |
| text( | |
| page, | |
| MARGIN, | |
| 716, | |
| "Payment terms: Net 30 from the invoice date. Remit to Northwind Industrial Supply,", | |
| 8, | |
| color=MUTED, | |
| ) | |
| text( | |
| page, | |
| MARGIN, | |
| 728, | |
| "Account 4400-118-226, Routing 121000248. Please quote invoice KY-2041.", | |
| 8, | |
| color=MUTED, | |
| ) | |
| text(page, PAGE_W - MARGIN, 716, "Page 1 of 2", 8, color=MUTED, align="right") | |
| def page_two(page): | |
| header(page, "INVOICE") | |
| text(page, MARGIN, 126, "Invoice KY-2041 · delivery schedule", 13, bold=True) | |
| text( | |
| page, | |
| MARGIN, | |
| 144, | |
| "Lines shipped from three warehouses; quantities match the invoice lines.", | |
| 8.5, | |
| color=MUTED, | |
| ) | |
| cols = [ | |
| ("Line", 40, "left"), | |
| ("SKU", 100, "left"), | |
| ("Qty", 300, "right"), | |
| ("Ship date", 420, "left"), | |
| ("Warehouse", 566, "right"), | |
| ] | |
| y = 176 | |
| for label, x, align in cols: | |
| text(page, x, y, label, 8, bold=True, color=MUTED, align=align) | |
| rule(page, MARGIN, y + 5, PAGE_W - MARGIN, color=INK, width=0.8) | |
| y += 18 | |
| for line, sku, qty, ship, wh in SCHEDULE: | |
| text(page, 40, y, str(line), 9) | |
| text(page, 100, y, sku, 9) | |
| text(page, 300, y, str(qty), 9, align="right") | |
| text(page, 420, y, ship, 9) | |
| text(page, 566, y, wh, 9, align="right") | |
| rule(page, MARGIN, y + 5, PAGE_W - MARGIN) | |
| y += 19 | |
| y += 36 | |
| text(page, MARGIN, y, "Terms and conditions", 11, bold=True) | |
| paragraphs = [ | |
| "Goods remain the property of Northwind Industrial Supply until paid in full. Risk passes to the", | |
| "buyer on delivery. Claims for shortage or damage must be made in writing within 7 days of receipt;", | |
| "after that period the delivery is deemed accepted.", | |
| "", | |
| "Late payments accrue interest at 1.5% per month or the maximum permitted by law, whichever is lower.", | |
| "Prices are exclusive of duties and local taxes unless stated otherwise. Returns require a written", | |
| "authorisation and are subject to a 15% restocking fee.", | |
| "", | |
| "This invoice is generated for demonstration purposes; the company, addresses and identifiers are", | |
| "fictional.", | |
| ] | |
| for i, line in enumerate(paragraphs): | |
| if line: | |
| text( | |
| page, MARGIN, y + 18 + i * 14, line, 8.5, color=INK if i < 6 else MUTED | |
| ) | |
| text(page, PAGE_W - MARGIN, 740, "Page 2 of 2", 8, color=MUTED, align="right") | |
| def build_invoice(): | |
| doc = fitz.open() | |
| page = doc.new_page(width=PAGE_W, height=PAGE_H) | |
| page_one(page) | |
| page = doc.new_page(width=PAGE_W, height=PAGE_H) | |
| page_two(page) | |
| doc.set_metadata( | |
| {"title": "Invoice KY-2041", "author": "Northwind Industrial Supply"} | |
| ) | |
| target = OUT / "invoice_ky-2041.pdf" | |
| doc.save(target, garbage=4, deflate=True) | |
| doc.close() | |
| return target | |
| def build_ledger(): | |
| from openpyxl import Workbook | |
| from openpyxl.styles import Alignment, Font, PatternFill | |
| from openpyxl.utils import get_column_letter | |
| rows = [ | |
| ( | |
| "SUP-1001", | |
| "Apex Castings GmbH", | |
| "Germany", | |
| "Castings", | |
| 812400, | |
| "Low", | |
| "2027-01-31", | |
| ), | |
| ( | |
| "SUP-1002", | |
| "Bharat Precision Pvt Ltd", | |
| "India", | |
| "Machining", | |
| 486300, | |
| "Medium", | |
| "2026-11-30", | |
| ), | |
| ( | |
| "SUP-1003", | |
| "Cascade Polymers Inc.", | |
| "United States", | |
| "Plastics", | |
| 274150, | |
| "Low", | |
| "2027-03-15", | |
| ), | |
| ( | |
| "SUP-1004", | |
| "Delta Hydraulics S.A.", | |
| "Spain", | |
| "Hydraulics", | |
| 631900, | |
| "Medium", | |
| "2026-12-31", | |
| ), | |
| ( | |
| "SUP-1005", | |
| "Evergreen Fasteners Co.", | |
| "Taiwan", | |
| "Fasteners", | |
| 158700, | |
| "Low", | |
| "2027-06-30", | |
| ), | |
| ( | |
| "SUP-1006", | |
| "Fushun Steel Works", | |
| "China", | |
| "Raw steel", | |
| 1240800, | |
| "High", | |
| "2026-10-31", | |
| ), | |
| ( | |
| "SUP-1007", | |
| "Granite Tool & Die", | |
| "United States", | |
| "Tooling", | |
| 392250, | |
| "Low", | |
| "2027-02-28", | |
| ), | |
| ( | |
| "SUP-1008", | |
| "Helios Coatings AB", | |
| "Sweden", | |
| "Surface treatment", | |
| 211600, | |
| "Low", | |
| "2027-05-31", | |
| ), | |
| ( | |
| "SUP-1009", | |
| "Iberia Bearings SL", | |
| "Spain", | |
| "Bearings", | |
| 344800, | |
| "Medium", | |
| "2027-01-15", | |
| ), | |
| ( | |
| "SUP-1010", | |
| "Jinlong Electronics", | |
| "China", | |
| "Electronics", | |
| 908450, | |
| "High", | |
| "2026-09-30", | |
| ), | |
| ( | |
| "SUP-1011", | |
| "Kestrel Logistics BV", | |
| "Netherlands", | |
| "Logistics", | |
| 176900, | |
| "Low", | |
| "2027-04-30", | |
| ), | |
| ( | |
| "SUP-1012", | |
| "Larsen Composites A/S", | |
| "Denmark", | |
| "Composites", | |
| 289300, | |
| "Medium", | |
| "2027-03-31", | |
| ), | |
| ( | |
| "SUP-1013", | |
| "Meridian Rubber Ltd", | |
| "United Kingdom", | |
| "Seals", | |
| 197450, | |
| "Low", | |
| "2027-07-31", | |
| ), | |
| ( | |
| "SUP-1014", | |
| "Nordic Aluminium AS", | |
| "Norway", | |
| "Aluminium", | |
| 705600, | |
| "Medium", | |
| "2027-02-28", | |
| ), | |
| ( | |
| "SUP-1015", | |
| "Orion Sensors Inc.", | |
| "United States", | |
| "Sensors", | |
| 421750, | |
| "Low", | |
| "2027-08-31", | |
| ), | |
| ( | |
| "SUP-1016", | |
| "Pacific Wire & Cable", | |
| "United States", | |
| "Cable", | |
| 233900, | |
| "Medium", | |
| "2026-12-15", | |
| ), | |
| ( | |
| "SUP-1017", | |
| "Qingdao Plating Co.", | |
| "China", | |
| "Plating", | |
| 168200, | |
| "High", | |
| "2026-10-15", | |
| ), | |
| ( | |
| "SUP-1018", | |
| "Rheinmetall Fluidtechnik", | |
| "Germany", | |
| "Hydraulics", | |
| 964300, | |
| "Low", | |
| "2027-05-15", | |
| ), | |
| ( | |
| "SUP-1019", | |
| "Sakura Precision KK", | |
| "Japan", | |
| "Machining", | |
| 578400, | |
| "Low", | |
| "2027-06-15", | |
| ), | |
| ( | |
| "SUP-1020", | |
| "Tatra Forge a.s.", | |
| "Slovakia", | |
| "Forgings", | |
| 312650, | |
| "Medium", | |
| "2027-01-31", | |
| ), | |
| ( | |
| "SUP-1021", | |
| "UniBelt Transmissions", | |
| "Italy", | |
| "Belts", | |
| 143800, | |
| "Low", | |
| "2027-04-15", | |
| ), | |
| ( | |
| "SUP-1022", | |
| "Vulcan Heat Treating", | |
| "United States", | |
| "Heat treatment", | |
| 268900, | |
| "Medium", | |
| "2027-02-15", | |
| ), | |
| ( | |
| "SUP-1023", | |
| "Wuhan Precision Gears", | |
| "China", | |
| "Gears", | |
| 652100, | |
| "High", | |
| "2026-11-15", | |
| ), | |
| ( | |
| "SUP-1024", | |
| "Zenith Abrasives SA", | |
| "France", | |
| "Abrasives", | |
| 118450, | |
| "High", | |
| "2026-12-01", | |
| ), | |
| ] | |
| wb = Workbook() | |
| ws = wb.active | |
| ws.title = "Suppliers" | |
| headers = [ | |
| "Supplier ID", | |
| "Supplier", | |
| "Country", | |
| "Category", | |
| "Annual Spend (USD)", | |
| "Risk Rating", | |
| "Contract Renewal", | |
| ] | |
| ws.append(headers) | |
| header_fill = PatternFill("solid", fgColor="0F172A") | |
| for cell in ws[1]: | |
| cell.font = Font(color="FFFFFF", bold=True) | |
| cell.fill = header_fill | |
| cell.alignment = Alignment(horizontal="center") | |
| for row in rows: | |
| ws.append(list(row)) | |
| widths = [13, 30, 16, 20, 20, 13, 18] | |
| for i, width in enumerate(widths, start=1): | |
| ws.column_dimensions[get_column_letter(i)].width = width | |
| for cell in ws["E"][1:]: | |
| cell.number_format = "#,##0" | |
| ws.freeze_panes = "A2" | |
| summary = wb.create_sheet("Summary") | |
| summary["A1"] = "Supplier ledger — annual spend summary" | |
| summary["A1"].font = Font(bold=True, size=13) | |
| summary.append([]) | |
| summary.append(["As of", "2026-09-30"]) | |
| summary.append(["Suppliers", len(rows)]) | |
| summary.append(["Total annual spend (USD)", sum(r[4] for r in rows)]) | |
| summary.append(["High-risk suppliers", sum(1 for r in rows if r[5] == "High")]) | |
| summary["B5"].number_format = "#,##0" | |
| summary.column_dimensions["A"].width = 28 | |
| summary.column_dimensions["B"].width = 16 | |
| target = OUT / "supplier_ledger.xlsx" | |
| wb.save(target) | |
| return target | |
| if __name__ == "__main__": | |
| print(build_invoice()) | |
| print(build_ledger()) | |