| title | KisaanKhata API |
|---|---|
| emoji | 🌾 |
| colorFrom | green |
| colorTo | blue |
| sdk | gradio |
| sdk_version | 5.9.0 |
| app_file | app.py |
| pinned | false |
A digital ledger for smallholder farmers in Pakistan.
KisaanKhata gives farmers an independent record of their loans and harvest sales, and automatically checks whether they were paid a fair price compared to real mandi (wholesale market) rates — fighting exploitation by middlemen (arhtiya).
kisaankhata/
├── backend/
│ ├── main.py FastAPI app entrypoint & CORS config
│ ├── database.py SQLite connection (SQLAlchemy)
│ ├── models.py ORM models: Farmer, LedgerEntry
│ ├── schemas.py Pydantic request/response schemas
│ ├── routers/
│ │ ├── ledger.py CRUD routes for farmers & entries
│ │ └── price_check.py Price-fairness check routes (+ voice)
│ ├── services/
│ │ ├── price_matcher.py Mandi price comparison logic
│ │ └── stt_service.py Whisper speech-to-text wrapper
│ ├── data/
│ │ └── mandi_prices_pk.csv Mandi price dataset (placeholder)
│ ├── requirements.txt
│ └── .env.example
├── frontend/ React app (scaffolded in a later prompt)
└── README.md
| Requirement | Version |
|---|---|
| Python | 3.10 + |
| ffmpeg | Any recent version — required by Whisper |
Install ffmpeg:
- Windows:
winget install ffmpegor download from https://ffmpeg.org/download.html - macOS:
brew install ffmpeg - Ubuntu/Debian:
sudo apt install ffmpeg
# From the project root
cd kisaankhata/backend
python -m venv .venv
# Windows PowerShell
.venv\Scripts\Activate.ps1
# macOS / Linux
source .venv/bin/activatepip install -r requirements.txt
⚠️ openai-whisperwill download the "base" model weights (~150 MB) on first use. This requires an internet connection the first time only.
ffmpeg is required by Whisper and must be installed separately.
Whisper uses ffmpeg to decode audio files. Without it, transcription will fail silently.
- Windows:
winget install ffmpegor download from https://ffmpeg.org/download.html- macOS:
brew install ffmpeg- Ubuntu/Debian:
sudo apt install ffmpegVerify installation:
ffmpeg -version
cp .env.example .env
# Edit .env if you want to change the Whisper model size or database path# Make sure you are inside kisaankhata/backend/ with the venv active
uvicorn main:app --reloadThe API will be available at http://127.0.0.1:8000
| URL | Description |
|---|---|
http://127.0.0.1:8000/ |
Health check |
http://127.0.0.1:8000/docs |
Swagger UI (interactive API docs) |
http://127.0.0.1:8000/redoc |
ReDoc API docs |
Vercel is serverless-first and excellent for the React frontend, but the FastAPI backend has three parts that do not fit Vercel's model well:
- SQLite persistence — Vercel functions have an ephemeral filesystem; the database would reset on every deploy/cold start.
- Background scheduler —
APSchedulerneeds a continuously running process; Vercel functions are request-scoped. - Local Whisper — the
faster-whispermodel and its dependencies are far larger than Vercel's function size limit.
The practical, low-cost setup is:
- Frontend → Vercel (fast CDN, automatic deploys from GitHub)
- Backend → Render (free tier, persistent disk, continuous process) or Hugging Face Spaces (free Docker Spaces)
The backend/render.yaml blueprint is already included. You only need to create the service and add secrets.
- Go to render.com and sign up/log in with GitHub.
- Click New + → Blueprint.
- Connect your
DarainHyder/KisaanKhatarepo. - Render will read
backend/render.yamland propose a free web service calledkisaankhata-api. - In the Render dashboard, add these environment variables under Environment:
TWILIO_ACCOUNT_SID— from your Twilio consoleTWILIO_AUTH_TOKEN— from your Twilio consoleTWILIO_PHONE_NUMBER— e.g.+1234567890
- Click Create Blueprint. Render builds and deploys the backend.
- Once live, copy the backend URL (e.g.
https://kisaankhata-api.onrender.com).
The SQLite file is stored on a 1 GB persistent disk at
./data/kisaankhata.db, so data survives redeploys.ffmpegis installed automatically via thepackages:section inrender.yaml.
Hugging Face Spaces can host the FastAPI backend through its free Gradio SDK — no Docker needed. A small app.py file at the repo root mounts a minimal Gradio UI onto the existing FastAPI app, so HF Spaces can serve it while all /api/*, /admin/*, /docs, etc. routes keep working.
Trade-offs vs Render:
- Sleeping containers — free Spaces go to sleep after inactivity, so the 6-hour AMIS scraper will not run reliably. Voice price checks and ledger endpoints still work fine when the Space is awake.
- Cold-start time — the
faster-whispersmallmodel (~466 MB) downloads on first voice request, so the first transcription after a sleep will be slow.
If those trade-offs are acceptable, follow these steps:
- Go to huggingface.co/spaces and click Create new Space.
- Fill in:
- Space name: e.g.
kisaankhata-api - License: MIT (or your preference)
- Space SDK: Gradio
- Space hardware: CPU free (upgrade later if needed)
- Space name: e.g.
- Choose Create.
- Push this repo to the Space. The easiest way is to use the Space's Git URL:
Alternatively, use the Hugging Face UI Files tab to upload
git clone https://huggingface.co/spaces/YOUR_USERNAME/kisaankhata-api cd kisaankhata-api git remote add upstream https://github.com/DarainHyder/KisaanKhata.git git pull upstream main git push origin mainapp.py,requirements.txt, and thebackend/folder. - HF Spaces reads
requirements.txtat the repo root, installsgradioplus the backend dependencies, and runsapp.py. - Once the build is green, your backend URL is
https://YOUR_USERNAME-kisaankhata-api.hf.space. - (Optional) Add Twilio secrets in the Space Settings → Secrets if you plan to use SMS/WhatsApp.
The SQLite database is stored at
/data/kisaankhata.db, which is persisted by Hugging Face Spaces.
- Go to vercel.com and sign up/log in with GitHub.
- Click Add New… → Project.
- Import
DarainHyder/KisaanKhata. - In the project settings:
- Framework Preset: Vite
- Root Directory:
frontend - Build Command:
npm run build - Output Directory:
dist
- Add an environment variable:
VITE_API_BASE_URL= your backend URL +/api- If you used Render:
https://kisaankhata-api.onrender.com/api - If you used Hugging Face Spaces:
https://YOUR_USERNAME-kisaankhata-api.hf.space/api
- If you used Render:
- Click Deploy.
Vercel will rebuild and redeploy automatically on every push to main.
For local development the Vite dev proxy still forwards /api to http://localhost:8000, so no change is needed.
| Method | Path | Description |
|---|---|---|
POST |
/api/ledger/farmers |
Register a new farmer |
GET |
/api/ledger/farmers/{id} |
Get a single farmer |
POST |
/api/ledger/entry |
Log a loan or sale entry |
GET |
/api/ledger/{farmer_id} |
All entries for a farmer (date desc) |
GET |
/api/ledger/{farmer_id}/summary |
Loan/sale totals + net balance |
POST |
/api/ledger/voice-entry |
Upload audio → raw Whisper transcript |
| Method | Path | Description |
|---|---|---|
GET |
/api/price-check/{entry_id} |
Check fairness of a single sale entry |
GET |
/api/price-check/farmer/{farmer_id} |
Check all sales for a farmer |
POST |
/api/price-check/voice |
Voice-first price check (audio upload) |
- Stage 1 — Project scaffold (models, schemas, routers, service stubs)
- Stage 2 — Implement CRUD routes + farmer financial summary
- Stage 3 — Dual-source price comparison (Kaggle mandi + FAOSTAT)
- Stage 4 — Whisper STT integration (voice-entry endpoint, Urdu default)
- Stage 5 — Transcript parsing → structured ledger entry (NLP)
- Stage 6 — React frontend (farmer dashboard, voice UI)
- Stage 7 — Deploy (Railway / Render + Vercel)
| Layer | Technology |
|---|---|
| Backend framework | FastAPI |
| Database | SQLite (via SQLAlchemy ORM) |
| Data validation | Pydantic v2 |
| Speech-to-text | OpenAI Whisper (local) |
| Data processing | pandas |
| Frontend (future) | React + Vite |
MIT — build freely, share knowledge, empower farmers. 🌾