DEV Community

Madhav Gupta
Madhav Gupta

Posted on

MandiLens: Open-Source AI That Tells a Farmer Which Mandi Actually Pays More

Hacktoberfest Open-Source AI Challenge Week 2: Side Hustle Submission 💸

This is a submission for the Hacktoberfest Open-Source AI Challenge Week 2: Side Hustle

What I Built

Every day, a small farmer in India has to answer one question: which mandi (market) should I take my crop to today?

Public price data exists. The government publishes it. But the published price is a headline, not a payout. That higher price forty kilometres away can be wiped out by transport, loading, commission, and market charges. The farmer is left doing mental arithmetic with a number that was never the real number.

MandiLens turns that headline price into the number that actually matters: estimated net proceeds after your costs. You enter a crop, a quantity, and your expenses; MandiLens ranks the candidate markets by what you'd actually take home, and shows exactly how it got there.

It is built for the person who would pay for it: small and marginal farmers, and the traders and commission agents who advise them. The value is obvious in a single comparison — a ₹8/kg difference between two mandis on a tonne of maize is ₹8,000, and transport rarely eats all of it.

Three things make MandiLens more than another price dashboard:

  • A deterministic decision engine. Net proceeds are pure arithmetic: gross minus expenses, with correct unit conversion (kg, quintal, tonne) and percentage commissions. The same inputs always give the same outputs, and it is covered by unit tests. The AI is never allowed to overwrite these numbers.
  • Provenance on every number. Every price stores the source it came from and the source-reported date — not the time we fetched it. Freshness is computed from when the market actually reported, never from when we cached. Demo data is badged as DEMO DATA everywhere and is never silently mixed with real prices.
  • A grounded copilot that admits when it doesn't know. Ask it "compare maize across Samastipur" or "what's the cheapest mandi for potatoes" and it calls real tools against the real database. Each answer carries a provenance banner, a visible tool trace, and explicit grounded / missingData flags. If there is no real data, it says so instead of inventing a number. In deterministic mode it states plainly that the answer was not generated by a model.

Demo

Live app: https://mandilens.onrender.com

The Compare workbench ranks markets by net proceeds, not headline price.

A quick tour:

  1. Open Compare, type a crop and quantity, and add your transport and commission costs. MandiLens ranks markets by net proceeds, not by headline price.
  2. Open Copilot and ask a plain-language question. Watch the tool trace show you which prices it actually read.
  3. Browse the commodities and markets directories, then post a buy or sell offer in Listings.

Every copilot answer shows a provenance banner, a tool trace, and grounded / missingData flags.

Post a buy or sell offer and get scored matches with reasons.

Demo accounts (password DemoPass123 for all):

  • farmer@demo.mandilens.test
  • trader@demo.mandilens.test
  • buyer@demo.mandilens.test

The demo dataset is deterministic and clearly labelled: 8 Bihar APMC markets × 8 commodities × 45 days of prices, generated so anyone can reproduce exactly the same walkthrough.

Code

Repository: https://github.com/madhavgupta07/mandilens (MIT licensed)

MandiLens is a real application, not a sketch: 15 MongoDB models with proper indexes, a two-phase CSV/JSON import pipeline, an admin console with ingestion and audit, first-party authentication, alerts, watchlists, saved comparisons, and a listings marketplace with matching and inquiries. There are 73 passing tests covering unit conversions, the opportunity engine, NLU, the importer, ingestion idempotency, and — importantly — the reliability of the assistant (that it does not fabricate data).

How I Built It

Client → API → deterministic engine + grounded copilot → MongoDB, with AGMARKNET ingestion.

  • Next.js 16 (App Router) + React 19 + TypeScript, styled with Tailwind CSS v4, Radix primitives, and lucide-react.
  • MongoDB (Atlas) with Mongoose for storage, with a composite dedupeKey so re-running ingestion is idempotent rather than duplicating rows.
  • Decimal.js for every money and quantity calculation, because floating-point arithmetic has no place near someone's income.
  • Zod for validation at every API boundary, and Papaparse for the CSV import path.
  • vitest + mongodb-memory-server so the whole suite runs without needing a live database.
  • Render for deployment — the app and its environment are declared in a render.yaml blueprint.

The AI layer is deliberately provider-agnostic. AI_PROVIDER can be ollama (fully local, open-weight), openai_compatible (any compatible endpoint, including open-weight models like Gemma served through an OpenAI-compatible API), or deterministic — which needs no model key at all and still runs the full grounded tool-calling pipeline. The data-ingestion adapter talks to AGMARKNET (via data.gov.in) under the Government Open Data License – India.

Two design decisions I'd defend in a code review:

  1. The arithmetic is not the AI's job. The comparison engine is a pure, testable function. The copilot's tools call it; the model never recomputes it. This is the difference between a decision tool and a confident guess.
  2. Missing data is a first-class outcome. An empty result set is reported as "we have no recent price for this" — never as "the market is closed". Words like that change what a farmer does with their morning.

Why Does Open Innovation Matter?

Agricultural price intelligence is exactly the kind of tool where openness is not a philosophy but a requirement.

It has to be cheap enough to be free for the people who need it most. A farmer comparing two mandis is not a customer who can absorb a per-query AI bill. MandiLens runs entirely without a model key in deterministic mode, and can run a fully local open-weight model through Ollama when a model is wanted. There is no vendor whose pricing decides whether a smallholder gets to use it.

It has to be inspectable, because the stakes are someone's harvest. Judging is done against government data under an open licence, and every number in the app traces back to its source and its source-reported date. When a number is wrong, anybody can follow it home. That audit trail is only possible because the whole stack — the price engine, the ingestion adapter, the copilot's tools — is open and readable.

It has to be adaptable to a niche a general API handles poorly. "Which mandi pays more for my maize after transport" is not a question a general-purpose assistant answers well, because the answer lives in a specific, messy, local dataset. Grounding an open model in that dataset — and letting that logic be forked, localized, and translated by anyone — is what makes it work at all.

And it keeps the honest path the easy path. Running in deterministic mode means MandiLens would rather say "I don't have this price" than invent one. Open, inspectable code is what lets a judge, a contributor, or a farmer verify that claim instead of taking it on faith.

Open-source AI makes a tool like this possible for a community that a closed product would never find worth serving.

My Agent Session

MandiLens was built in a human-in-the-loop workflow with an AI coding agent: the architecture decisions, the grounding rules, the data model, and the review were human; the agent did a lot of the implementation and the test scaffolding. The design constraints that shaped it — the AI never overrides the arithmetic, missing data is never dressed up as certainty — were enforced by tests, not just intent. I can share an agent-session transcript to illustrate the process.

Prize Categories

My project is submitted for the Hacktoberfest Open-Source AI Challenge — Week 2: Side Hustle.

It also genuinely uses partner technology, so I am entering it in:

  • Best Use of MongoDB Atlas — MongoDB is the system of record: 15 collections with compound and unique indexes, an idempotent ingestion pipeline keyed on a composite dedupeKey, and provenance stored per price record.
  • Best Use of Render — the live demo is deployed on Render as a web service, with the environment defined in a checked-in render.yaml.
  • Best Use of Gemma — the copilot supports open-weight Gemma served over an OpenAI-compatible endpoint, and runs fully locally through Ollama; deterministic mode keeps it working even with no model key at all.

MandiLens is open source under the MIT licence. Mandi price data is attributed to AGMARKNET, Directorate of Marketing & Inspection, via data.gov.in under the Government Open Data License – India. It is an independent project and is not affiliated with or endorsed by AGMARKNET or the Government of India.

Top comments (0)