2026-07-14 16:10:03 +08:00
2026-07-07 09:53:54 +08:00

NBD Offer Pre-Item Master

NBD Offer Pre-Item Master is a Streamlit application that converts supplier / offer Excel workbooks into a formal one-sheet pre_item_master Excel export matching the SQL table schema.

It reads every workbook sheet, recognizes known formats, builds a merge blueprint, maps source columns to SQL columns, previews one row per product, and exports a deterministic pre_item_master_YYYYMMDD_HHMMSS.xlsx file.

Language

  • Runtime UI language: English only
  • Project documentation remains bilingual (English + Traditional Chinese)
  • Traditional Chinese README: README_CHT.md
Document English Traditional Chinese
Product / technical specification docs/SPEC.md same bilingual file
User workflow docs/WORKFLOW.md same bilingual file
LLM specification docs/LLM_SPEC.md docs/LLM_SPEC_CHT.md
SQL export schema config/db_schema.yaml same
LLM target schema config/schema.yaml same
Handover: business flow docs/BUSINESS_FLOW.md same bilingual file
Handover: config docs/CONFIG.md same bilingual file
Handover: operations docs/OPERATIONS.md same bilingual file
Handover: troubleshooting docs/TROUBLESHOOTING.md same bilingual file
Handover: deployment docs/DEPLOYMENT.md same bilingual file
Data dictionary docs/DATA_DICTIONARY.md same bilingual file
AI analysis tuning docs/AI_ANALYSIS_TUNING.md same bilingual file

Verified status

Run the full verification suite before every merge:

python -m pytest -q

Known Kuji workbook regression fixtures export successfully:

kuji_bntw.xlsx   -> type=kuji_tw, engine=hybrid, mappings=20, targets=14, rows=15, cols=57, sheet=pre_item_master
kuji_bntw_2.xlsx -> type=kuji_tw, engine=hybrid, mappings=19, targets=14, rows=12, cols=57, sheet=pre_item_master

The exported workbook is not blank: it contains one pre_item_master sheet, 57 SQL columns, and non-empty data rows.

What the app does

  1. Upload an .xlsx workbook.
  2. Preview sheets before processing.
  3. Recognize known workbook formats using fingerprints.
  4. Analyze workbook structure with deterministic rules and optional LLM assistance.
  5. Detect cross-sheet relations and merge product rows.
  6. Map source columns to SQL target columns.
  7. Preview merged rows.
  8. Export a single-sheet Excel workbook named pre_item_master_<timestamp>.xlsx.
  9. Optionally submit a Match Blueprint proposal for Admin review and future reuse.

Basic-user adjustments are bounded by operator_review settings and only change the current interpretation; they never edit the uploaded workbook, Type YAML, or SQL schema. Preview and Export are protected by a reviewed-snapshot hash, so any later Type, sheet, header, or mapping change requires fresh confirmation.

New workbook formats use a controlled learning workflow: privacy-safe evidence, explainable similarity scoring, full deterministic replay, Admin approval, version archive and rollback. See Controlled Format Learning.

Architecture overview

app.py
  └─ ui/upload_page.py       Upload + fingerprint recognition
  └─ ui/confirm_page.py      Workbook analysis + blueprint confirmation
  └─ ui/preview_page.py      Merge preview, one row per product
  └─ ui/export_page.py       Formal SQL-shaped Excel export
  └─ ui/copy.py              English-only UI copy registry

services/excel_parser.py       Workbook parsing and known-format structure hints
services/fingerprint.py        Format recognition by workbook signature
services/workbook_analyzer.py  LLM + deterministic fallback orchestration
services/llm_stages.py         OpenAI-compatible LLM calls
services/merge_engine.py       Cross-sheet row merge
services/export_service.py     Excel writer
services/config_manager.py     settings.example.yaml + runtime settings.yaml overlay

config/settings.example.yaml         Tracked safe settings template
config/settings.yaml                 Runtime settings file, gitignored
config/db_schema.yaml                Formal SQL table export columns
config/schema.yaml                   LLM target field list
config/types/*.yaml                  Known workbook type definitions

Installation

git clone https://git.umis.moe/davidlo/NBD_Offer_PreItemMaster.git
cd NBD_Offer_PreItemMaster
python -m pip install -r requirements.txt

Run

python -m streamlit run app.py

Open the URL printed by Streamlit, usually:

http://localhost:8501

Optional LLM setup

The application reads only project-owned configuration at runtime.

To enable AI matching, copy config/settings.example.yaml to config/settings.yaml, then edit the active profile:

llm:
  enabled: true
  active_profile: local
  profiles:
    local:
      label: "DGX Spark Local"
      base_url: "http://localhost:8000/v1"
      model: "local-model-name"
      api_key: "YOUR_LOCAL_API_KEY_OR_BLANK"
      stream_response: true
      mode: small
      max_sheet_rows: 80
      max_mapping_columns: 40
    cloud:
      label: "Cloud AI"
      base_url: "https://api.deepseek.com/v1"
      model: "deepseek-chat"
      api_key: "YOUR_CLOUD_API_KEY_OR_BLANK"
      stream_response: true
      mode: large
      max_sheet_rows: 200
      max_mapping_columns: 100

Switch active_profile between local and cloud to change provider without code changes. Leave the active profile's api_key empty to keep optional LLM analysis unavailable. The app intentionally uses one credential field per profile. Keep stream_response: true for live AI analysis progress in the Streamlit trace panel. If a provider does not support OpenAI-compatible streaming, the app automatically switches that request to a normal non-streaming response, so the UI does not error; set it to false only if you want to skip the streaming attempt entirely.

Testing

python -m pytest -q

Important regression coverage:

  • settings.example.yaml defaults plus gitignored runtime settings.yaml overlay
  • AI request resolution from project config only
  • UI translation helper and bilingual stepper labels
  • known-format deterministic export behavior when LLM is online
  • Kuji fixture end-to-end export contract

Security and portability rules

  • No API key in tracked files.
  • Runtime app code reads only project config.
  • config/settings.example.yaml is the tracked template; config/settings.yaml is runtime-only and gitignored.
  • Known workbook formats keep deterministic row-model authority; LLM may add mappings but should not rewrite validated summaries/groups.
  • Export contract is defined by config/db_schema.yaml.

Repository

S
Description
No description provided
Readme
2.8 MiB
0 Stars 1 Watchers 0 Forks
Languages
Python 99.8%
Dockerfile 0.1%