An end-to-end Explainable AI (XAI) and economic advisory platform for Indian agriculture.
Live Demo • Project Overview • Our Journey • Key Features • Tech Stack • Results
Click the badge above or follow this link to try the live application: https://huggingface.co/spaces/srits21/farm-friend-2
This system is hosted live on Hugging Face Spaces using a responsive, production-configured Gradio instance utilizing multi-threaded parallel execution protocols.
Farm Friend is a localized, transparent "Glass Box" Machine Learning system designed to optimize macro-level agricultural choices across India. By integrating a high-accuracy regression engine with explicit Government of India Minimum Support Price (MSP) benchmarks and localized Explainable AI (XAI), the framework converts dense soil, climate, and input metrics into actionable, transparent, and multi-lingual visual insights.
Figure 1: Full Architecture Pipeline from user UI interaction layers down to granular XAI calculation engines.
Real-world AI cannot exist as an uninterpretable "Black Box" when dealing with human livelihoods like smallholder farming. Our platform evolved systematically from raw accuracy targeting to full, real-time explainability.
V1: The High-Variance Black Box - A Trust Barrier
Our first iteration focused entirely on high-dimensional model tuning. We achieved an excellent accuracy score, but the system couldn't explain *why* it made its decisions. Farmers and agricultural extension officers had no reason to trust its outputs.
💡 **Lesson Learned:** Predictive accuracy without structural explainability creates a fundamental trust gap in real-world deployment.
V2: The Lagging Oracle - The Integration Bottleneck
We integrated SHAP (Shapley Additive Explanations) to calculate localized individual feature impacts. However, calling the tree-explainer dynamically during runtime created massive lag, forcing users to wait over 8 seconds for a single form submission.
💡 **Lesson Learned:** Naive feature attribution calculations completely break UI/UX fluidity. Localized explainer pipelines must be mathematically cached or globally initialized.
V3: The Unified Advisor - Real-Time Glass Box
Our final system resolved all execution and usability bottlenecks to create a production-grade advisory engine:
- Global SHAP Optimization: We optimized the system by pre-computing and initializing the
TreeExplainerglobally on app launch, cutting user response latency down from 8,000ms to <200ms. - Trilingual Reverse Mapping: We implemented an implicit backend translation schema to support English, Hindi (हिंदी), and Odia (ଓଡ଼ିଆ) natively, without breaking categorical encoded inputs.
- Robust Validation Strategy: Evaluated generalization using an 80/20 random-seeded train/test allocation cross-verified against an extensive 5-Fold grid validation layout.
✅ The Result: A reliable, split-second, transparent advisor that matches elite machine learning metrics with local community accessibility.
- Explainable AI Engine: Leverages SHAP localized attribution to explain the exact structural impact (+/- metric tonnes) of individual environmental inputs.
-
Economic Intelligence: Integrates official Government of India MSP benchmarks (2023-24) to calculate localized total revenue directly via:
$$\text{Income} = \text{Predicted Yield (t/ha)} \times \text{Area (ha)} \times 10 \times \text{MSP (₹/quintal)}$$ - Trilingual Localization: A fully interactive, reactive user interface adapting layout labels cleanly across English, Hindi, and Odia.
- Data-Driven Feature Alignment: Dynamically builds, fills, and drops category one-hot matrices via an exported template to avoid silent prediction crashes.
| Python | Scikit-Learn | Gradio | SHAP | Pandas | Joblib |
|---|---|---|---|---|---|
![]() |
Our validation study benchmarked four leading ensemble architectures across 19,295 historical records. The Random Forest Regressor demonstrated superior stability against climate anomalies:
| Model Architecture |
|
RMSE (t/ha) |
|---|---|---|
| 🌲 Random Forest (Selected) | 0.9558 | 2.1952 |
| 🐱 CatBoost Regressor | 0.9365 | 2.6334 |
| 🚀 XGBoost | 0.9249 | 2.8621 |
| ⚡ LightGBM | 0.9051 | 3.2174 |
To track trade-offs across speed, training resource efficiency, and residual variance constraints, a comparative multidimensional index map was constructed:
Using the globally initialized interpretability engine, localized weight effects are mapped explicitly during validation checkpoints to isolate key performance indicators:
Trilingual User Interface Layout
Real-Time XAI Explanations & Financial Outputs
Click here for instructions to run this project yourself locally.
-
Clone the Repository
git clone [https://github.com/Ritviks21/F2-Farm-Friend-.git](https://github.com/Ritviks21/F2-Farm-Friend-.git) cd F2-Farm-Friend- -
Install Dependencies
pip install -r requirements.txt
-
Train the Production Model
python src/train.py
-
Launch the Local Web App
python src/app.py
Contributions are highly welcome! This platform is designed as an expanding tool for open-source digital agriculture systems. If you would like to expand its functionality, feel free to fork the repository and submit a Pull Request.
-
Expand Regional Datasets:
- Integrate micro-level district data for Southern and Eastern coastal zones.
- Add additional crop varieties (e.g., specific commercial spices, rubber, plantation crops) to the backend encoding script.
-
Incorporate Deep Regressors:
- Experiment with multi-layer neural networks or localized deep tabular models to further minimize residual RMSE variance thresholds below
$2.19$ . - Expand hyperparameter sweeps utilizing automated frameworks like Optuna.
- Experiment with multi-layer neural networks or localized deep tabular models to further minimize residual RMSE variance thresholds below
-
Enhance the UI Framework:
- Introduce comparative visualizations (e.g., dynamic bar graphs or map charts) rendering right within the Gradio user interface layout.





