ps2181/invoice-processing-pipeline
<div align="center">
<img src="https://capsule-render.vercel.app/api?type=waving&color=gradient&customColorList=6,11,20&height=200§ion=header&text=Invoice%20Processing%20Pipeline&fontSize=40&fontColor=fff&animation=twinkling&fontAlignY=35&desc=Self-Improving%20Multi-Agent%20Fraud%20Detection%20%7C%20OpenEnv%20%2B%20GRPO%20%2B%20Qwen2.5&descAlignY=55&descSize=16" width="100%"/>
<p> <a href="https://ps2181-invoice-processing-pipeline.hf.space/web"> <img src="https://img.shields.io/badge/๐%20Live%20Demo-HuggingFace%20Spaces-FF9D00?style=for-the-badge&logo=huggingface&logoColor=white" /> </a> <a href="https://colab.research.google.com/drive/1C13giNt-NmbzKNFJr5L1fms3L8LfmB"> <img src="https://img.shields.io/badge/Training%20Colab-Open%20Notebook-F9AB00?style=for-the-badge&logo=googlecolab&logoColor=white" /> </a> <a href="https://ps2181-invoice-processing-pipeline.hf.space/docs"> <img src="https://img.shields.io/badge/API%20Docs-FastAPI-009688?style=for-the-badge&logo=fastapi&logoColor=white" /> </a> </p>
<p> <img src="https://img.shields.io/badge/Framework-OpenEnv-1A356E?style=for-the-badge" /> <img src="https://img.shields.io/badge/Model-Qwen2.5--1.5B%20+%20LoRA%20r%3D16-8B1A4E?style=for-the-badge" /> <img src="https://img.shields.io/badge/Training-GRPO%20+%20Unsloth-00A67E?style=for-the-badge" /> <img src="https://img.shields.io/badge/Agents-5%20Adversarial-E44D26?style=for-the-badge" /> </p>
<p> <img src="https://img.shields.io/badge/Tasks-10%20Progressive-6C3483?style=for-the-badge" /> <img src="https://img.shields.io/badge/Deployment-Docker%20%7C%20HF%20Spaces-0D1117?style=for-the-badge&logo=docker" /> <img src="https://img.shields.io/badge/Theme-%234%20Self--Improvement-FF6B35?style=for-the-badge" /> <img src="https://img.shields.io/badge/Hackathon-Meta%20PyTorch%202026-185FA5?style=for-the-badge" /> </p>
<br/>
Meta PyTorch OpenEnv Hackathon โ Grand Finale ยท April 25โ26, 2026 Team: Pritam Satpathy & Gnana Nawin T ยท VIT, Vellore
<br/>
<a href="https://git.io/typing-svg"> <img src="https://readme-typing-svg.demolab.com?font=Fira+Code&weight=600&size=22&pause=1000&color=007A87¢er=true&vCenter=true&width=750&lines=5-Agent+Adversarial+Fraud+Detection+System;Self-Improving+via+Cross-Episode+Regulator;GRPO-Trained+LoRA+Agents+on+Live+Environment;Invoice+%E2%86%92+Extract+%E2%86%92+Audit+%E2%86%92+Approve+%E2%86%92+Improve" alt="Typing SVG" /> </a>
</div>
๐ฅ The Core Idea
A system that continuously generates harder challenges targeting its own weakest points.
Most fraud detection pipelines are static. Ours gets harder for itself over time: the Regulator finds where the Auditor keeps failing, the Generator exploits those exact blind spots in the next episode, the Auditor's new mistakes update the Regulator โ and the loop closes without any human intervention.
Primary theme: #4 Self-Improvement ยท Secondary: #1 Multi-Agent Interactions
<div align="center"> <img width="1710" height="326" alt="5-agent self-improvement loop" src="https://github.com/user-attachments/assets/319654c3-aa24-47e8-9716-734d4e902168" /> </div>
๐ค 5-Agent Architecture
๐ฏ Regulator โโbias weightsโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโบ โก Generator
โฒ โ
โ raw invoice text
โ missed fraud types โผ
โ ๐ Extractor
โ โ
โ structured data
โ โผ
โโโโโ episode outcome โโโโ โ
Approver โโaudit resultsโโโ ๐ต๏ธ Auditor<div align="center">
</div>
โก Three Novel Features
<table> <tr> <td width="33%" align="center">
๐ฎ Predictive Regulator
Computes trend slopes over 5-episode windows.<br/>Warns of emerging blind spots before detection rates cross the critical threshold โ proactive oversight, not reactive retraining.
+0.15 early-warning bonus
</td> <td width="33%" align="center">
๐งฌ Compound Fraud
Invoices carry two fraud signals simultaneously (e.g. phantom vendor + price gouging).<br/>Partial credit +0.65 for catching one; full reward +0.99 for both.
Prevents single-signal heuristics.
</td> <td width="33%" align="center">
๐ Confidence Calibration
Tracks (confidence, correct?) pairs per fraud type.<br/>Detects overconfident misses โ the Auditor saying "90% sure, approved" on fraud โ the most dangerous real-world failure mode.
</td> </tr> </table>
๐ฏ 10 Tasks โ Progressive Curriculum
<div align="center">
</div>
Dynamic difficulty also adjusts within each task via a rolling 10-episode score window: score above 0.85 โ heavier OCR, more discrepancies, deeper traps. Drop below 0.60 โ it eases off.
๐ Training Results โ GRPO on Live Environment
All 3 agents trained with TRL GRPOTrainer + Unsloth using the deployed HF Space as the live reward verifier โ /grader endpoint is the reward function during training.
Before vs After Training
<div align="center">
</div>
Setup: Qwen2.5-1.5B-Instruct ยท 4-bit QLoRA r=16 ยท Unsloth + TRL ยท Google Colab A100
Extractor Reward Curve
X-axis: training step (1โ20) ยท Y-axis: reward (0โ1). Left: total GRPO reward across 4 independent signals (format 0.10 + field accuracy 0.40 + math 0.25 + completeness 0.25). Right: live `/grader` score peaking at 0.914 โ above Qwen 72B baseline (0.67) and untrained 1.5B (0.46).
Left: Total GRPO reward across 4 signals (format + field + math + completeness) over 20 training steps. Right: Live environment grader score peaking at 0.914 โ above Qwen 72B baseline (0.67) and untrained 1.5B baseline (0.46).
Auditor Reward Curve (Run 2 โ Bug Fixed)
X-axis: training step (1โ30) ยท Y-axis: reward (0โ1). Total reward (blue) and live env reward (orange) with ยฑ1 std band. Best total: 0.719 at step 10. Live env reward climbed from 0.01 (dead signal, Run 1) to 0.52 after fixing the TRL episode_id list indexing bug.
Total reward (blue) and live env reward (orange) over 30 steps with ยฑ1 std band. Best total reward: 0.719. Live env reward rose from 0.01 (dead signal in Run 1) to 0.52 after fixing the episode_id list bug.
Generator Reward Curve
X-axis: training step (1โ30) ยท Y-axis: reward (0โ1). Live evasion reward (red) flat near 0 โ Auditor+Approver caught all fraud attempts. Fraud plausibility reward (orange dashed) stable at ~0.20 โ Generator learned realistic invoice structure even without successful evasion.
Live evasion reward (red) flat near 0 โ Auditor+Approver caught all fraud attempts. Fraud plausibility reward (orange dashed) learned and stable at ~0.20, showing the Generator learned to produce realistic-looking invoices even without successful evasion.
๐ Reward Hacking Caught at Step 10
At step 10 the model achieved math_consistency = 0.97 and completeness = 1.0 while field_accuracy = 0.00 โ it had learned to output arithmetically-consistent JSON with entirely hallucinated values:
Step 10 โ Reward Hacking Detected:
format: 0.10 โ
math_consistency: 0.97 โ
โ model gaming this signal
completeness: 1.00 โ
โ model gaming this signal
field_accuracy: 0.00 โ โ hallucinating all values
Action: adjusted training emphasis on field_accuracy weight
Result: field_accuracy climbed to 0.30+ by step 30Without 4 independent signals, a single aggregated reward would have called this success. Independent signals are diagnostics, not just incentives.
Auditor Training โ Run 2 (exact data)
<div align="center">
Run 1 (dead signal): live env reward flat at 0.010 โ TRL passes episode_id as a list; old code sent the whole list instead of indexing per completion
</div>
๐ Reward Architecture
๐ Extractor โ 4 Independent Signals
reward_format(extracted) # 0.10 โ all 5 required JSON keys present?
reward_field_accuracy(extracted, gt) # 0.40 โ vendor / date / currency / total match?
reward_math_consistency(extracted) # 0.25 โ qty ร unit_price = amount per line?
reward_completeness(extracted, gt) # 0.25 โ all expected line items captured?
# All clamped to (0.01, 0.99) โ no log(0), no gradient collapse at boundaries๐ต๏ธ Auditor
<div align="center">
</div>
โก Generator (Adversarial Self-Play)
๐ฏ Regulator โ Cross-Episode
Total = Precision(0.35) + Recall(0.35) + No-over-flagging(0.15) + Early-warning-bonus(0.15)The early-warning bonus rewards predictions of emerging blind spots โ before detection rates cross the critical threshold.
๐ง Trained LoRA Agents
<div align="center">
</div>
LoRA target modules: q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj
๐ The Regulator in Action
After each episode, the Regulator publishes a report the Generator uses to bias its next batch:
GET /regulator/report
{
"total_audits_recorded": 20,
"detection_rates": {
"phantom_vendor": "31% โ BLIND SPOT (-0.08โ)",
"price_gouging": "74% โ OK (+0.03โ)",
"math_fraud": "81% โ OK (+0.01โ)",
"duplicate_submission": "62% โก EMERGING (-0.02โ)"
},
"blind_spots": ["phantom_vendor"],
"emerging_blind_spots": ["duplicate_submission"],
"generator_weights": {
"phantom_vendor": 0.30, โ 3ร upweighted (blind spot)
"duplicate_submission": 0.20, โ 2ร upweighted (emerging)
"price_gouging": 0.125,
"math_fraud": 0.125,
"compound_fraud": 0.10
},
"verdict": "Recommend retraining on: phantom_vendor"
}๐ญ Sample Multi-Agent Episode
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
MULTI-AGENT PIPELINE ยท LIVE EPISODE
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ฏ REGULATOR (30-episode rolling window)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
phantom_vendor 31% โ BLIND SPOT โ prioritised 60%
price_gouging 74% โ OK
math_fraud 81% โ OK
duplicate 62% โ OK
โก GENERATOR (Qwen2.5 LoRA)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Fraud focus : phantom_vendor (60% Regulator weight)
Vendor : ShadowByte Technologies โ not in registry
๐ EXTRACTOR (Qwen2.5 LoRA)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Reward : 0.847 [format 0.10 ยท field 0.38 ยท math 0.25 ยท completeness 0.12]
๐ต๏ธ AUDITOR (Qwen2.5 LoRA)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
INV-85529 โ ๐จ FLAGGED [PHANTOM VENDOR] conf=0.91
INV-85530 โ โ
APPROVED conf=0.88
โ
APPROVER
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
INV-85529 โ โ REJECT
Generator reward : 0.60 (evaded Auditor on 1/3, Approver caught)
๐ฏ REGULATOR UPDATE
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
phantom_vendor detection: 31% โ 45% โ improving
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ๐ Quick Start
# Health check
curl https://ps2181-invoice-processing-pipeline.hf.space/health
# Environment-wide metrics
curl https://ps2181-invoice-processing-pipeline.hf.space/metrics
# Auto-progressive curriculum episode
curl -X POST https://ps2181-invoice-processing-pipeline.hf.space/reset \
-H "Content-Type: application/json" -d '{"task_id": "curriculum"}'
# Start multi-agent episode
curl -X POST https://ps2181-invoice-processing-pipeline.hf.space/multi/reset
# Regulator blind spot report
curl https://ps2181-invoice-processing-pipeline.hf.space/regulator/reportRun Training (Google Colab)

Colab โ /reset (fresh synthetic invoice from live environment)
โ model generates JSON
โ /grader scores against ground truth
โ GRPO updates weights toward higher-reward completions
โ repeat 200 steps๐๏ธ Repository Structure
invoice-processing-pipeline/
โ
โโโ server/
โ โโโ app.py # FastAPI โ 18 endpoints
โ โโโ environment.py # 10 tasks ยท graders ยท dynamic difficulty
โ โโโ multi_agent_environment.py # 5-agent system + AuditorPerformanceTracker
โ โโโ agents.py # Lazy-loading LoRA inference wrappers
โ โโโ web_ui.py # Gradio UI (mounted at /web)
โ
โโโ models.py # Pydantic: Action ยท Observation ยท State
โโโ inference.py # Standalone inference helper
โโโ client.py # OpenEnv-compatible Python client
โ
โโโ extractor_training_grpo.ipynb # ๐ฅ Extractor GRPO training (Unsloth + TRL)
โโโ auditor_grpo_training.ipynb # ๐ฅ Auditor GRPO training
โโโ generator_grpo_training.ipynb # ๐ฅ Generator GRPO training
โ
โโโ assets/
โ โโโ reward_curve.png # Extractor training curve
โ โโโ auditor_reward_curve_run2.png
โ โโโ generator_reward_curve.png
โ
โโโ openenv.yaml # OpenEnv manifest (all tasks declared)
โโโ Dockerfile # HF Spaces Docker (port 7860, non-root UID 1000)
โโโ pyproject.toml # Project metadata + dependencies
โโโ requirements.txt # Runtime dependencies
โโโ validate-submission.sh # Submission validator script
โโโ BLOG.md # HuggingFace blog post
โโโ ROUND2_PROBLEM_STATEMENT.md # Full problem statement + reward design rationale๐ API Reference
Core OpenEnv
Multi-Agent
Regulator
๐๏ธ Tech Stack
<div align="center">
</div>
๐ญ Theme Alignment
<div align="center">
</div>
๐ฅ Team
<div align="center">
Meta PyTorch OpenEnv Hackathon โ Grand Finale ยท April 25โ26, 2026 ยท Bangalore
</div>
๐ All Links
<div align="center">
</div>
<div align="center">
<img src="https://capsule-render.vercel.app/api?type=waving&color=gradient&customColorList=6,11,20&height=100§ion=footer&animation=twinkling" width="100%"/>
Built with โค๏ธ for the Meta PyTorch OpenEnv Hackathon 2026
"The system that gets harder for itself โ so the agent never stops learning."
</div>
