Inspiration

Construction procurement moves $13 trillion annually, yet back-office processing remains analog. We read procurement reports mentioning spending hours manually cross-checking invoices against purchase orders; one accidentally paid a duplicate invoice for $47,000 that slipped past manual review. Another caught a fraud case three months late: a vendor submitting invoices with round-number amounts and zero line-item breakdown, a textbook red flag nobody noticed. Existing tools are either enterprise-only platforms costing six figures, or generic scanners that don't understand construction terms like "GRN," "challan," or "BOQ." We built MarginGuard AI: drag-and-drop a scanned invoice and get structured extraction, risk scoring, three-way matching, fraud alerts, and an AI recommendation in seconds.

What it does

MarginGuard AI is a full-stack procurement intelligence platform with six core capabilities:

  1. Intelligent Document Processing: Upload invoices, POs, GRNs, and contracts in any format (PDF, DOCX, PNG). Auto-classifies document type, extracts structured data (vendor, line items, amounts, dates), powered by Google Gemini 2.0 Flash with fully offline fallback.
  2. Automated Three-Way Matching finds related POs, GRNs, and invoices from the same vendor, then performs fuzzy matching using Jaccard similarity to compare quantities, prices, and totals across documents.
  3. Real-Time Fraud Detection Six independent engines: duplicate detection, price anomaly z-scores, round-number flags, missing PO alerts, weekend date checks, and new vendor high-amount warnings.
  4. AI Procurement Copilot: RAG-powered chat that lets you query your entire document corpus in plain English, backed by a custom TF-IDF inverted index and Gemini with conversation history.
  5. Risk Scoring & Recommendations: Every document gets a composite risk score and an AI-generated recommendation: Approve, Review, Reject, or Escalate with reasoning and next steps.
  6. Live Dashboard: Real-time analytics: total spend, active vendors, pending documents, fraud alerts, spend trends, vendor distribution.

How we built it

Stack: Next.js 14 (App Router), TypeScript, Tailwind CSS 4, shadcn/ui, Prisma v6 + SQLite, Google Gemini 2.0 Flash, Recharts, Zustand.

Document Pipeline (9 steps): Upload → OCR (pdf-parse for PDFs, Gemini Vision for images, Tesseract CLI fallback) → Classification (weighted keyword scorer) → Structured Extraction (Gemini with temperature: 0.1 + regex JSON parser, offline regex fallback) → Validation (completeness scoring) → DB Persistence (Prisma) → Auto-Linking & Three-Way Match (Jaccard fuzzy matching, 0.4 threshold, 5% quantity tolerance) → Fraud Detection (6 parallel rule engines) → Risk Scoring (weighted composite) → AI Summary & Recommendation (Gemini or template fallback).

RAG Chat: Documents chunked at 500 chars with 100-char overlap → TF-IDF inverted index in-memory with IDF weighting =log( N+1/df+1) +1;

→ Top 5 chunks + summaries + last 10 messages sent to Gemini → Offline pattern-matching fallback.

Challenges we ran into

15-second upload latency. Four sequential Gemini calls. Restructured to parallel execution with instant local fallbacks dropped to ~8s AI path, ~5s offline.

### Chat hallucinations. Gemini fabricated vendor names and amounts. Solved with strict RAG: prompt enforces "say you don't know" when data is missing; context window only contains real indexed data.

Accomplishments that we're proud of

End-to-end pipeline that works. Upload a real construction invoice and get extraction, risk scoring, fraud checks, and AI recommendation in under 10 seconds.

### Graceful AI degradation. Every feature has a fully functional offline fallback that works at 100% without any API key. ### Zero-dependency RAG. Custom TF-IDF search engine built from scratch: no Pinecone, no Weaviate, no vector DB. ### Six-layer fraud engine. Independent analytical checks (duplicates, z-score anomalies, round numbers, missing POs, weekend dates, new vendor risk) providing thorough coverage without false-positive overload. ### Fuzzy matching that handles real documents. "OPC Cement 53 Grade 50kg bags" on a PO correctly matches "Cement OPC 53G Bags" on an invoice while still catching quantity/price discrepancies.

What we learned

OCR isn't solved for construction. Poor scans, handwritten notes, complex tables, multilingual content. Gemini Vision crushes Tesseract here, but a degradation chain (Gemini → Tesseract → graceful message) is essential.

### LLM extraction needs battle-tested prompts. Always request "raw JSON only," use regex JSON extraction as safety net, keep temperature: 0.1. ### Matching is fuzzy by nature. Descriptions never match exactly. The match result should reduce the reviewer's search space, not replace the reviewer. ### Fraud detection surfaces questions, not accusations. "This amount is 3.2σ above vendor average" is more useful than "this is fraud." ### Offline-first has compounding benefits. Deployable anywhere, including construction sites with poor connectivity. AI enhances; it doesn't depend. ### TypeScript + Prisma eliminates bug classes. Generated types caught dozens of null-reference and type-mismatch bugs at compile time.

What's next for MarginGuard AI

Multi-currency & multi-language: Handle INR, GBP, EUR, AED with exchange normalization; extract from Hindi, Arabic documents.

### Predictive spend analytics: Forecast cement/steel price trends, predict vendor risk, recommend optimal order timing. ### ERP integrations: Connectors for SAP Ariba, Oracle Procurement Cloud, Tally ERP for automatic document ingestion. ### Collaborative approval workflows: Multi-user review chains with comments, routing, and full audit trails. ### Mobile-first field view: PWA for site engineers to photograph delivery challans on-site and get instant PO matching.

Built With

Share this project:

Updates

Submission history