FarmAssist is a modern, full-stack smart farming ecosystem that empowers farmers and agronomists with data-driven decision-making tools. By combining custom Machine Learning pipelines, agronomic heuristic engines, a relational database backend, and an interactive analytics dashboard, FarmAssist provides actionable advice on crop selection, soil health, fertilizer management, yield prediction, and climate monitoring. It also features a conversational AI agent to offer real-time agronomic consulting.
- Key Features
- Technology Stack
- Machine Learning Pipeline & Feature Engineering
- Database Schema (Prisma Models)
- System Architecture
- Installation & Setup
- API Endpoints Reference
-
ML Classifier: Utilizes a custom-trained
CalibratedClassifierCVwrapping aHistGradientBoostingClassifierwith Sigmoid Calibration. - Top-N Predictions: Predicts the top 3 most suitable crops out of 51 supported classes, showing precise calibrated confidence percentages for each.
-
Reliability Classifier: If the top crop confidence is
$\ge 70%$ , the recommendation is flagged as Reliable; otherwise, it warns the user of environmental marginality. - Inputs Taken: Soil Nitrogen (N), Phosphorus (P), Potassium (K), pH level, Temperature (Β°C), Humidity (%), and Rainfall (mm).
Computes a hybrid Soil Quality Score (
- Method 1 (60% Weight): Top-1 crop prediction confidence (the calibrated output probability from the ML classifier). High confidence indicates the soil profile closely matches a well-characterized crop profile.
-
Method 3 (40% Weight): Normalized weighted sum of raw soil parameters (N, P, K, pH, temperature, humidity, rainfall). Features are normalized against training-set limits (
$0\text{--}1$ ) and weighted by their absolute Pearson correlation with crop labels. -
Qualitative Ratings:
-
$\ge 80$ : Excellent -
$\ge 65$ : Good -
$\ge 50$ : Average -
$\ge 35$ : Below Average -
$< 35$ : Poor
-
Calculates expected crop yield (tonnes/acre) using a combination of baseline agronomical yields, soil quality multipliers, and regional seasonal factors:
- Agromonic Lookup Table: Holds baseline yields for all 51 crops (e.g., Banana = 8.0 t/acre, Potato = 7.0 t/acre, Rice = 2.0 t/acre, Wheat = 2.2 t/acre).
-
Soil Multiplier: Scales yield from
$0.40\times$ (poor soil) up to$1.20\times$ (excellent soil) using the formula:$$\text{Multiplier} = 0.40 + \left(\frac{\text{Soil Quality Score}}{100}\right) \times 0.80$$ -
Seasonal Factors: Adjusts yields for different crop cycles (Kharif:
$1.05$ , Rabi:$1.00$ , Zaid:$0.90$ , Summer:$0.90$ , Winter:$1.00$ , Annual:$1.00$ ). -
Formula:
$$\text{Yield} = \text{Base Yield} \times \text{Soil Multiplier} \times \text{Seasonal Factor} \times \text{Plot Area (acres)}$$
- Nutrient Check: Evaluates current N, P, and K values against crop-specific optimal ranges.
- Dosage Calculations: Calculates exact quantities (kg/acre) of corrective fertilizers (Urea, DAP, MOP, or maintenance NPK 10-26-26) needed to reach optimal thresholds, including split-application guidance (e.g., 50% at sowing, 50% at tillering).
- pH & Soil Advice: Recommends amendments based on pH (e.g., agricultural lime for acidic soils, gypsum/sulfur for alkaline soils) and textures (e.g., frequency tips for Sandy vs. Clay soils).
- CRUD Operations: Create, read, update, and delete distinct farm plots (e.g., "North Field", "West Orchard").
- State Capture: Stores plot location, area (acres), soil parameters, and local climate averages.
- Historical Soil Logs: Automatically records soil history readings to track changes in NPK, pH, and climate parameters over time.
- AI Engine: Powered by Gemini 2.5 Flash integrated via
langchain-google-genai. - Stateful Memory: Maintains conversational context for the last 10 turns (20 messages) per session using an in-memory history window (
RunnableWithMessageHistory). - Agricultural Persona: Configured as "FarmAssist AI", providing concise, friendly, and expert advice on pest control, crop rotation, organic practices, and climate adjustments.
- Feature Weights: Visualizes ML feature importance weights (18 total features) via interactive horizontal bar charts.
- Soil Radar Chart: Compares current plot soil nutrients against optimal reference benchmarks.
- Crop Distribution Pie: Aggregates user crop prediction history into a dynamic donut chart.
- Model Summary Card: Displays training parameters, test accuracy (99.77%), split samples, calibration folds, and data scalers used.
- Framework: React.js (Vite-based SPA)
- Styling: Tailwind CSS v4, Lucide React (Icons), Shadcn UI elements
- State Management & Fetching: TanStack React Query v5, React Context (Auth & Lands contexts)
- Visualizations: Recharts (Radar, Bar, Pie charts)
- Routing: React Router DOM v7
- Environment: Node.js (ES modules), Express.js
- Database ORM: Prisma Client v5 (PostgreSQL database connection)
- Security: JSON Web Tokens (JWT) authentication, bcrypt password hashing
- Validation: Zod schemas
- Microservice Framework: Flask, Flask-CORS
- Scientific Computing: NumPy, Pandas, Scikit-Learn
- Generative AI Framework: LangChain, langchain-google-genai, google-generativeai
- Utilities: Python-Dotenv, Pickle
The machine learning model recommendations are trained and validated in train_final.ipynb.
To create sharp decision boundaries and boost classifier confidence on borderline or ambiguous inputs, the model scales the 7 raw features into 18 features using domain-specific agronomic relationships:
-
Soil Nutrient Ratios:
-
N_P_ratio: Nitrogen-Phosphorus balance ($N / (P + 10^{-5})$). -
N_K_ratio: Nitrogen-Potassium balance ($N / (K + 10^{-5})$). -
P_K_ratio: Phosphorus-Potassium balance ($P / (K + 10^{-5})$). -
NPK_sum: Sum of primary nutrients ($N + P + K$ ). -
NPK_product: Nutrient product interaction ($N \times P \times K$ ).
-
-
Climate & Environmental Indexes:
-
heat_index: Heat/thermal stress ($\text{temperature} \times \text{humidity} / 100$ ). -
aridity: De Martonne aridity index ($\text{rainfall} / (\text{temperature} + 1)$). -
temp_rain: Interaction between temperature and precipitation ($\text{temperature} \times \text{rainfall}$ ). -
humid_rain: Precipitation multiplied by relative humidity ($\text{humidity} \times \text{rainfall}$ ).
-
-
pH Interactions:
-
ph_N: Available nitrogen interaction ($pH \times N$ ). -
ph_class: Categorized acidity levels (0: Acidic$< 5.5$ , 1: Moderately Acidic$5.5\text{--}6.5$ , 2: Neutral$6.5\text{--}7.5$ , 3: Alkaline$> 7.5$ ).
-
During model development in the Jupyter Notebook, multiple algorithms were compared on identical stratified test sets (80% train / 20% test, 6,596 samples total, 51 classes) and evaluated on borderline samples:
| Model Config | Feature Eng? | Calibration? | Test Accuracy | Prediction Confidence (Ambiguous Test Sample) | Status |
|---|---|---|---|---|---|
| 1. Baseline Random Forest | No | No | 99.70% | 54.0% | β Low Confidence |
| 2. Random Forest | Yes | No | 99.70% | 51.7% | β Low Confidence |
| 3. ExtraTrees Classifier | Yes | No | 99.77% | 62.7% | |
| 4. HistGradientBoosting | Yes | No | 99.32% | 70.8% | β High Confidence (No Calib) |
| 5. HistGradientBoosting | Yes | Sigmoid Calib (CV=3) | 99.77% | 58.3% | π Selected & Served (Calibrated) |
| 6. Soft Voting Ensemble | Yes | No | 99.62% | 60.8% |
Note
While raw HistGradientBoosting achieved the highest raw confidence on the ambiguous test sample, HistGradientBoosting with Sigmoid Calibration (Model 5) was selected as the final model because calibration aligns model probabilities with true physical probabilities, preventing overconfidence while maintaining an outstanding 99.77% test accuracy across all 51 classes.
The system uses a PostgreSQL database structured via schema.prisma:
erDiagram
User ||--o{ Land : "manages"
User ||--o{ CropPrediction : "requests"
User ||--o{ YieldPrediction : "requests"
User ||--o{ FertilizerRecommendation : "requests"
Land ||--o{ SoilReading : "logs"
Land ||--o{ CropPrediction : "linked to"
Land ||--o{ YieldPrediction : "linked to"
Land ||--o{ FertilizerRecommendation : "linked to"
User {
String id PK
String email UK
String name
String password
String phone
String avatarUrl
DateTime createdAt
}
Land {
String id PK
String userId FK
String name
String location
Float area
Float nitrogen
Float phosphorus
Float potassium
Float ph
String soilType
Float temperature
Float humidity
Float rainfall
Boolean isActive
}
SoilReading {
String id PK
String landId FK
Float nitrogen
Float phosphorus
Float potassium
Float ph
Float temperature
Float humidity
Float rainfall
DateTime recordedAt
}
CropPrediction {
String id PK
String userId FK
String landId FK
Float nitrogen
Float phosphorus
Float potassium
Float ph
Float temperature
Float humidity
Float rainfall
String predictedCrop
Float confidence
Json alternativeCrops
DateTime createdAt
}
YieldPrediction {
String id PK
String userId FK
String landId FK
String crop
String season
Float area
Float predictedYield
Float soilQualityScore
DateTime createdAt
}
FertilizerRecommendation {
String id PK
String userId FK
String landId FK
String crop
String soilType
Float nitrogen
Float phosphorus
Float potassium
Float ph
String recommendedFertilizer
String nitrogenStatus
String phosphorusStatus
String potassiumStatus
String advice
DateTime createdAt
}
+-----------------------+
| React Frontend |
| (Vite + Tailwind) |
+-----------+-----------+
|
HTTP REST API
|
v
+-----------+-----------+
| Node.js Express |
| API Gateway |
+-----+-----------+-----+
| |
Prisma ORM Axios (Internal HTTP)
| |
v v
+-----+-----+ +-+---------------------+
|PostgreSQL | | | Python Flask ML |
| Database | | | Microservice |
+-----------+ | +----------+----------+
| |
| Pickle Loader
| v
| +------+------+
| | HGB Model |
| | LangChain |
| +-------------+
+-----------------------+
- Node.js (v18 or higher)
- Python 3.10+ (with
pipandvirtualenv) - PostgreSQL database server running locally or hosted
The Flask backend serves predictions and Chatbot workflows.
- Navigate to the Flask directory:
cd backend/flask - Create and activate a Python virtual environment:
python -m venv .venv # Windows: .venv\Scripts\activate # Linux/macOS: source .venv/bin/activate
- Install dependencies:
pip install -r requirements.txt
- Create a
.envfile inbackend/flask/.env:FLASK_PORT=5001 GEMINI_API_KEY=your_gemini_api_key_here MODEL_PATH=../model.pkl ENCODER_PATH=../encoder.pkl METADATA_PATH=../soil_metadata.pkl
- Run the server:
The Flask microservice will start on
python app.py
http://localhost:5001.
This service acts as the gateway database interface and auth manager.
- Navigate to the Node directory:
cd backend/node - Install dependencies:
npm install
- Create a
.envfile inbackend/node/.env:PORT=5000 DATABASE_URL="postgresql://username:password@localhost:5432/farmassist?schema=public" JWT_SECRET="your-super-secret-key-here" PYTHON_SERVICE_URL="http://localhost:5001"
- Push database migrations using Prisma:
npx prisma db push
- Run the server:
The API gateway will start on
node server.js
http://localhost:5000.
- Navigate to the frontend directory:
cd frontend - Install dependencies:
npm install
- Create a
.envfile infrontend/.env:VITE_API_URL="http://localhost:5000" VITE_FLASK_API_URL="http://localhost:5001"
- Start the Vite development server:
Open browser and navigate to
npm run dev
http://localhost:5173(or the console-indicated port).
POST /predict-crop- Predicts top crops and computes soil quality metrics.POST /soil-quality- Computes soil quality index only.POST /predict-yield- Computes expected crop yield tonnes/acre.POST /recommend-fertilizer- Formulates NPK adjustment advice.GET /model-info- Retrieves feature weights and training configurations.POST /api/chat- Chats with FarmAssist AI (incorporates LangChain session state).DELETE /api/chat/<session_id>- Clears session chat history.
All routes except login/register require an Authorization: Bearer <token> header.
- Authentication (
/api/auth)POST /register- Register a new user account.POST /login- Login to account.GET /me- Retrieve current logged-in user profile.
- Lands CRUD (
/api/lands)GET /- Retrieve all lands managed by user.POST /- Register a new farm plot.PUT /:id- Update plot details.DELETE /:id- Delete agricultural plot.
- Predictions (
/api/predictions)POST /crop- Predicts crop, saves result, returns index.GET /crop- Retrieve user's historical crop predictions.POST /yield- Predicts crop yield, saves result.GET /yield- Retrieve yield prediction history.POST /soil-quality- Calculates soil quality index score (proxies Flask).
- Fertilizer (
/api/fertilizer)POST /- Formulates fertilizer advice, saves records.GET /- Retrieves user's history of fertilizer calculations.
πΎ FarmAssist - Bridging AI, data science, and modern agronomic standards to make farming smarter, sustainable, and highly productive.