How ltsroy/jansetu works
1. What this repo is
JanSetu is a Python-first decision system for MPLADS allocation. The architectural center is a deterministic analytical engine that turns public deprivation data plus citizen demand into an optimized ₹5 Cr works plan, then packages the result into an audit-ready sanction letter and dashboard.
2. Two major boundary layers
The repo cleanly separates:
- an offline/core layer in jansetu/ that is deterministic, testable, and mostly pure-Python; and
- data/product delivery layers around it: scrapers, cloud export scripts, AI-assisted packet generation, optimization service, and multiple UIs.
3. Input data and provenance
The system ingests a heterogeneous set of public sources: SECC, Census, OSM, VIIRS, GHSL, MPLADS spend, LGD boundary keys, JJM, IMD rainfall, CAG rules, news RSS, and citizen-intake text. jansetu/provenance.py and jansetu/indicators.py form the trust boundary: they register source lineage and validate inputs before they affect scoring.
4. Core analytical pipeline
The main flow inside jansetu/ is:
ingestion → deprivation scoring → diagnostic modeling → tiering → demand/need divergence → works eligibility → optimization.
The main modules are ingest.py, deprivation.py, modeling.py, tiers.py, divergence.py, works.py, and optimize.py.
5. Decision logic and explainability
The deprivation model is intentionally glass-box: weighted indicators are decomposed back to source-tagged contributions. modeling.py checks whether the policy weights align with the underlying data structure, while tiers.py and sensitivity.py test robustness and deterministic grouping rather than training a predictive model.
6. Optimization boundary
jansetu.optimize is the exact portfolio solver, implemented with OR-Tools CP-SAT. It enforces the budget and statutory earmarks, so the “plan” is not just a ranking; it is a constrained selection of works. optimizer/ wraps this as a separate Cloud Run microservice with its own Dockerfile and requirements.
7. Audit and governance outputs
After optimization, audit.py produces the fairness certificate and reproducibility hash. watchdog.py adds compliance overlays such as fund-lapse countdowns and CAG checks, so the final output is defensible, not just optimal.
8. Live intelligence overlays
Several modules enrich the core plan with time-sensitive context:
- retrospective.py compares historic spend vs deprivation,
- living.py refreshes indicators with newer scheme data,
- foresight.py uses IMD timing for weather-sensitive works,
- atlas.py ranks across constituencies,
- brief.py synthesizes these into a command brief.
These are overlays on top of the stable deprivation/optimization core.
9. AI-assisted intake and letter generation
The ai/ package is a separate language-model boundary. ai/intake.py ingests citizen text, ai/orchestrator.py coordinates generation, and ai/letter.py drafts the sanction packet. The README and deployment path indicate Gemini is used here, but it is not part of the core decision math.
10. Presentation layer
The repo contains more than one UI surface:
- app.py provides the local web app entry point,
- dashboard/ is a self-contained static bundle for direct browser use,
- frontend/ is a Vite/React TypeScript app,
- src/ is a small Next.js-style app with an API route (src/app/api/live-data/route.ts) and a hotspot map component.
A diagram should show these as separate presentation consumers of the same underlying JanSetu data/model outputs.
11. Cloud and external service dependencies
The production path uses BigQuery for the warehouse layer, Earth Engine for VIIRS/GHSL extraction, Google Maps for visualization, Firebase for intake sync, and Cloud Run for the optimizer service. These services matter because they split the repo into offline analytical code versus cloud-backed data/execution paths.
12. Tests and reproducibility as a subsystem
The tests/ suite is large and explicit about invariants: deprivation monotonicity, earmark enforcement, astroturf resistance, cloud artifact contracts, and API behavior. Together with DATA.md, MODELS.md, and provenance hashing, testing is part of the architecture: it preserves reproducibility and policy traceability across the whole pipeline.
Main entry points: brief.py Brief · pipeline.py Pipeline · test_pipeline.py Tests · ingest.py Ingest
Components 30
Core engine
Deterministic analytical pipeline and governance logic in jansetu/.
- Ingest · Loads heterogeneous public and citizen inputs into the decision pipeline.
jansetu/ingest.py - Provenance · Registers source lineage and provenance metadata.
jansetu/provenance.py - Indicators · Validates and structures source data before scoring.
jansetu/indicators.py - Deprivation · Computes weighted deprivation scores from source-tagged inputs.
jansetu/deprivation.py - Modeling · Explains policy-weight fit and decomposes contributions.
jansetu/modeling.py - Tiers · Deterministic tiering and robustness checks for constituencies or units.
jansetu/tiers.py - Demand · Represents citizen demand and need signals.
jansetu/demand.py - Divergence · Measures demand-versus-need mismatch for prioritization.
jansetu/divergence.py - Works · Translates signals into eligible works candidates.
jansetu/works.py - Optimize · Exact constrained portfolio selection under budget and earmarks.
jansetu/optimize.py - Audit · Produces fairness certificates and reproducibility hashes.
jansetu/audit.py - Watchdog · Adds lapse, CAG, and compliance overlays to the final plan.
jansetu/watchdog.py - Pipeline · Coordinates the end-to-end JanSetu workflow.
jansetu/pipeline.py
Live overlays
Time-sensitive enrichment and comparative analysis layered onto the core plan.
- Brief · Summarizes the optimized plan and overlays into a command brief.
jansetu/brief.py - Retrospective · Compares historic spend against deprivation.
jansetu/retrospective.py - Living · Refreshes indicators with newer scheme data.
jansetu/living.py - Foresight · Uses IMD timing for weather-sensitive works.
jansetu/foresight.py - Atlas · Ranks constituencies across the broader field.
jansetu/atlas.py
AI boundary
Language-model assisted intake and sanction packet generation.
Presentation
Multiple consumer surfaces for the same JanSetu outputs.
Data & services
Scrapers, warehouse scripts, optimizer service, and external integration paths.
- Optimizer svc · Containerized service wrapper around the exact optimizer.
optimizer/main.py - Scrapers · Source-specific collectors for news, schemes, IMD, MPLADS, and CAG.
scrapers/__init__.py - Earth Engine · VIIRS/GHSL extraction and external geospatial export.
earthengine/ee_export.js - Warehouse SQL · BigQuery loading and deprivation warehouse transforms.
sql/01_spine.sql
Verification
Tests and contract checks that preserve reproducibility and policy traceability.
- Tests · Policy, contract, and reproducibility checks across the system.
tests/test_pipeline.py
Connections 39
- Pipeline → Ingest: orchestrates · Coordinates the main ingestion-to-plan flow.
- Ingest → Provenance: tags lineage · Attaches source provenance to incoming records.
- Ingest → Indicators: normalizes · Prepares heterogeneous inputs for validation.
- Indicators → Deprivation: feeds · Validated indicators enter deprivation scoring.
- Deprivation → Modeling: explains · Deprivation outputs are decomposed into contributions.
- Modeling → Tiers: groups · Policy structure informs deterministic tiering.
- Tiers → Demand: compares · Tiered units are compared against demand signals.
- Demand → Divergence: diffs · Need and demand are contrasted for prioritization.
- Divergence → Works: filters · Gap analysis narrows candidate works.
- Works → Optimize: constrains · Eligible works are input to the constrained solver.
- Optimize → Audit: certifies · Selected plan is hashed and audited.
- Optimize → Watchdog: overlays · Compliance logic is applied to the plan.
- Watchdog → Brief: summarizes · Compliance context is folded into the command brief.
- Audit → Brief: attests · Audit evidence informs the final brief.
- Retrospective → Brief: enriches · Historic spend context feeds the synthesis layer.
- Living → Brief: enriches · Fresh scheme data informs the brief.
- Foresight → Brief: enriches · Weather timing context feeds the brief.
- Atlas → Brief: ranks · Cross-constituency ranking context feeds the brief.
- Intake AI → AI Flow: inputs · Citizen text enters the AI workflow.
- AI Flow → Letter AI: drafts · The orchestrator produces the sanction packet text.
- Pipeline → Retrospective: layers · Historical comparison sits above the core pipeline.
- Pipeline → Living: layers · Indicator refresh sits above the core pipeline.
- Pipeline → Foresight: layers · Weather sensitivity sits above the core pipeline.
- Pipeline → Atlas: layers · Cross-constituency ranking sits above the core pipeline.
- Pipeline → Brief: produces · The pipeline emits a synthesized command brief.
- App → Brief: serves · Local app surfaces the synthesized outputs.
- Dashboard → Brief: renders · Static dashboard presents the same plan artifacts.
- Frontend → Brief: renders · React UI consumes JanSetu outputs.
- Next App → Brief: renders · Next-style app consumes live and demo data.
- Next App → Pipeline: requests · API and page routes can pull generated data.
- Optimizer svc → Optimize: wraps · Cloud Run service exposes the exact solver.
- Scrapers → Ingest: supplies · Source collectors feed the ingestion boundary.
- Earth Engine → Indicators: supplies · Geospatial extraction contributes indicator inputs.
- Warehouse SQL → Deprivation: models · Warehouse transforms materialize deprivation data.
- Tests → Ingest: checks · Tests assert ingestion and provenance contracts.
- Tests → Optimize: checks · Tests enforce budget and earmark invariants.
- Tests → Audit: checks · Tests validate audit hashes and certificates.
- Tests → AI Flow: checks · Tests cover AI intake and letter generation contracts.
- Tests → Pipeline: checks · Tests protect end-to-end determinism.