CoolFace
Apppublic

Soulay/customs-compass

sourceHugging Facemitupdated 4mo agoView on Hugging Face
0likes
App README

๐Ÿงญ Customs Compass

AI compliance assistant for Chinese SMEs (hardware, batteries, robotics, electronics) exporting to the United States. Built with Streamlit + Ollama.

What it does

Given a product description and/or a compliance question, Customs Compass produces a structured bilingual answer covering:

  • โ€”US sales tax obligations โ€” state-by-state economic nexus thresholds, transaction rules, and base sales tax rates
  • โ€”Customs duties โ€” HTS codes and duty rates by product category
  • โ€”Federal certifications โ€” FCC, UL, FDA flags per product
  • โ€”Risk assessment โ€” Low / Medium / High flags based on your sales vs. each state's threshold
  • โ€”Source citations โ€” every claim cites the underlying CSV/JSON
  • โ€”Live CBP news โ€” relevant headlines from cbp.gov/newsroom injected into the context (cached 1 hour)

Architecture (hybrid)

LayerSourceUpdate model
Reference datanexus_thresholds.csv, hts_duty_codes.csv, tax_rates_by_state.jsonManual / version-controlled
CBP enforcement alertscbp_alerts.csv (curated from cbp.gov)Manual / version-controlled
CBP RAG corpuscbp_chunks.jsonl (595 chunks) + cbp_pages.jsonl (200 pages)Scraped from cbp.gov
RetrievalBM25-light (pure Python, title-boosted)Index built at startup (~0.5s)
Live newscbp.gov/newsroom (scraped, cached 1h)Auto on app load
ReasoningOllama llama3.2:3b (local)โ€”
FallbackDeterministic rule-based templatesโ€”

The app always works offline: if Ollama isn't running or the network is down, it gracefully falls back to a rule-based engine that still produces structured bilingual answers from the CSV data.

Quick start

1. Install dependencies

bash
pip install -r requirements.txt

2. (Recommended) Install & start Ollama

Download Ollama from <https://ollama.com>, then:

bash
ollama pull llama3.2:3b
ollama serve

3. Run the app

bash
streamlit run app.py

Open <http://localhost:8501> in your browser.

Running without Ollama (fallback mode)

The app detects whether Ollama is reachable at http://localhost:11434. If not, the sidebar will show ๐ŸŸก Ollama offline โ€” fallback mode and use a deterministic template engine that:

  1. 1.Extracts state names, product categories, and sales amounts from your question
  2. 2.Looks them up directly in the CSV files
  3. 3.Computes risk levels and produces a bilingual checklist

You can also force fallback mode via the sidebar checkbox โ€” useful for predictable, fast, offline-only responses.

Data files

nexus_thresholds.csv

Economic-nexus thresholds for all 50 US states + DC.

Columns: state, threshold_usd, transaction_rule, notes

Note: most states use $100,000; large markets (Texas, California, New York) use $500,000; Oregon, Delaware, Montana, New Hampshire, and Alaska have no statewide sales tax (threshold set to 0).

hts_duty_codes.csv

Customs duties + federal certifications per product category.

Columns: product_category, hts_code, duty_rate, fcc_needed, ul_needed, fda_needed, notes

Categories: battery_with_charger, battery_only, robotics_with_radio, consumer_electronics, medical_device, industrial_machinery, power_tools, led_lighting, drones, solar_panels, smart_home_devices, ev_charger, wearables, audio_equipment.

tax_rates_by_state.json

Base state sales tax rates (percentages). No-tax states are set to 0.

cbp_alerts.csv

Curated CBP enforcement & tariff alerts pulled from cbp.gov. Critical for Chinese SME exporters.

Columns: category, title, summary, relevant_products, country_focus, severity, action_required, source_url

Covers:

  • โ€”Section 301 tariffs on Chinese electronics (+25%)
  • โ€”Section 232 steel/aluminum derivatives
  • โ€”De Minimis suspension (EO 14324, effective Aug 29 2025) โ€” all sub-$800 shipments now dutiable
  • โ€”UFLPA (Uyghur Forced Labor Prevention Act) โ€” rebuttable presumption against XUAR-sourced goods
  • โ€”AD/CVD (Antidumping/Countervailing Duties) โ€” solar, batteries, steel
  • โ€”IPR seizures โ€” counterfeit electronics/batteries (China = 66% of FY2025 seizures)
  • โ€”Lithium battery safety โ€” UN38.3, UL 2054 requirements
  • โ€”IEEPA tariffs โ€” emergency authority for rapid tariff changes

Example queries

  • โ€”"We sell power banks to Texas, $200k annual sales. Do we need to collect sales tax?"
  • โ€”"Our startup ships lithium batteries to California and New York. What certifications do we need?"
  • โ€”"Medical thermometer exports to Florida with $150k revenue โ€” what are our obligations?"
  • โ€”"We're sending drones to Oregon. Any federal compliance needs?"

Risk model

The app classifies nexus risk in three bands relative to the state's economic threshold:

RiskSales vs thresholdMeaning
๐ŸŸข Low< 70%No obligation; continue monitoring
๐ŸŸก Medium70% โ€“ 100%Approaching nexus; register pre-emptively
๐Ÿ”ด Highโ‰ฅ 100%Obligation triggered; register & collect immediately

Configuration

Edit constants at the top of app.py to tune behavior:

  • โ€”OLLAMA_MODEL โ€” switch to llama3.2:1b for faster responses, qwen2:7b for better Chinese
  • โ€”NEWS_CACHE_TTL โ€” CBP news cache duration in seconds
  • โ€”NEWS_FETCH_TIMEOUT โ€” HTTP timeout for CBP fetches

Project structure

DD/
โ”œโ”€โ”€ app.py                      # Single-file Streamlit application
โ”œโ”€โ”€ requirements.txt            # streamlit, pandas, requests
โ”œโ”€โ”€ nexus_thresholds.csv        # Economic nexus data (50 states + DC)
โ”œโ”€โ”€ hts_duty_codes.csv          # Customs duties + certifications
โ”œโ”€โ”€ tax_rates_by_state.json     # State sales tax rates
โ”œโ”€โ”€ cbp_alerts.csv              # CBP enforcement & tariff alerts (Section 301, UFLPA, etc.)
โ”œโ”€โ”€ cbp_pages.jsonl             # CBP page corpus (200 full pages, metadata)
โ”œโ”€โ”€ cbp_chunks.jsonl            # CBP chunked corpus (595 chunks, ~4000 chars each)
โ””โ”€โ”€ README.md                   # This file

How the RAG works

When you ask a question, the app:

  1. 1.Extracts states, product categories, and sales amounts via regex/keyword matching
  2. 2.Looks up structured data: nexus thresholds, HTS codes, tax rates, curated CBP alerts
  3. 3.Retrieves the top-3 most relevant CBP page excerpts from cbp_chunks.jsonl using a BM25-light scoring (pure Python, no extra deps):
  4. 4.Tokenizes question + product names + state names
  5. 5.Scores each chunk via Okapi BM25 (k1=1.5, b=0.75)
  6. 6.Boosts terms appearing in the page title (2.5ร—)
  7. 7.Deduplicates so you get at most one chunk per source page
  8. 8.Injects all retrieved context into the LLM prompt (Ollama) or the fallback template engine
  9. 9.Renders a bilingual answer with risk flags and source citations

Limitations

  • โ€”The included CSV data is for demonstration only and reflects general public information. Real engagements should verify against the latest state tax authority and CBP publications.
  • โ€”The CBP news fetcher relies on the public HTML structure of cbp.gov/newsroom. If CBP restructures the site, the fetcher will return an empty list (the app continues to function).
  • โ€”llama3.2:3b is a small model. Chinese output quality may vary; the fallback engine uses pre-translated templates for consistent Chinese.

Disclaimer

This tool provides educational guidance only. It does not constitute legal, tax, or customs advice. Always consult a licensed CPA, customs broker, or trade attorney before making compliance decisions.

License

Provided as-is for educational use.