Agricultural crop recommendation API powered by machine learning.
AgroStat predicts the best crop to grow based on an Indian state/union territory, temperature, and humidity. It uses an XGBoost classifier trained on soil nutrient profiles and climate data.
- Features
- Project Structure
- Prerequisites
- Installation
- Running the API
- API Reference
- Example Usage
- Datasets
- How It Works
- Environment Variables
- License
- Crop prediction β Predict the best crop for any supported Indian state given temperature and humidity.
- Top-5 recommendations β Returns the top 5 crops ranked by prediction probability.
- Nutrient profiling β Automatically maps state-level soil nutrient data (N, P, K) to model inputs.
- Auto-training β Trains the XGBoost model from CSV datasets on first run if no saved artifact exists.
- Re-trainable β Re-train the model on demand via a single API call.
- Interactive docs β Auto-generated Swagger UI and ReDoc documentation.
- CORS-enabled β Ready for frontend integration out of the box.
agrostat/
βββ backend/
β βββ app/
β β βββ core/
β β β βββ config.py # App settings, paths, CORS config
β β βββ routes/
β β β βββ crop.py # API route handlers
β β βββ schemas/
β β β βββ crop.py # Pydantic request/response models
β β βββ services/
β β β βββ crop_recommender.py # ML model service (train, predict, load/save)
β β βββ main.py # FastAPI application entrypoint
β βββ artifacts/ # Saved model artifacts (auto-generated)
βββ datasets/
β βββ Crop_recommendation.csv # Crop features & labels dataset
β βββ place.csv # State-level soil nutrient data
βββ models/
β βββ crop_recommender.py # Original Colab notebook script
βββ .env.example # Environment variable template
βββ .gitignore
βββ requirements.txt
βββ LICENSE
βββ README.md
- Python 3.10+
- pip (or your preferred package manager)
For Render deployment, use Python 3.11.x (or 3.12.x). Python 3.14 may fail because some ML dependencies may not provide prebuilt wheels yet.
-
Clone the repository:
git clone https://github.com/devansharora18/agrostat.git cd agrostat -
Create and activate a virtual environment (recommended):
python -m venv venv source venv/bin/activate # macOS / Linux venv\Scripts\activate # Windows
-
Install dependencies:
pip install -r requirements.txt
-
(Optional) Copy and configure environment variables:
cp .env.example .env
Start the server from the project root directory:
python -m backend.app.mainOr use uvicorn directly:
uvicorn backend.app.main:app --host 0.0.0.0 --port 8000 --reloadOn first startup the model will be trained automatically from the CSV datasets (this takes a few seconds). Subsequent starts will load the saved artifact instantly.
Once running, visit:
| URL | Description |
|---|---|
| http://localhost:8000 | API root |
| http://localhost:8000/docs | Swagger UI (interactive) |
| http://localhost:8000/redoc | ReDoc (read-only) |
All endpoints are prefixed with /api/v1.
GET /
Returns basic API information and links.
Response:
{
"name": "AgroStat API",
"version": "1.0.0",
"docs": "/docs",
"health": "/api/v1/health"
}GET /api/v1/health
Returns the current health status and readiness of the ML model.
Response:
{
"status": "ok",
"model_ready": true,
"available_states": 34,
"available_crops": 22
}POST /api/v1/predict
Predict the best crop to grow for a given state, temperature, and humidity.
Request Body:
| Field | Type | Required | Description |
|---|---|---|---|
state |
string | β | Indian state or union territory |
temperature |
float | β | Temperature in Β°C (-10 to 60) |
humidity |
float | β | Relative humidity in % (0 to 100) |
Example Request:
{
"state": "West Bengal",
"temperature": 30.0,
"humidity": 85.0
}Example Response:
{
"crop": "rice",
"state": "West Bengal",
"temperature": 30.0,
"humidity": 85.0,
"nutrient_profile": {
"N": "Low",
"P": "Medium",
"K": "Medium"
},
"top_5": [
{ "crop": "rice", "probability": 0.742315 },
{ "crop": "jute", "probability": 0.098421 },
{ "crop": "papaya", "probability": 0.051203 },
{ "crop": "coconut", "probability": 0.032117 },
{ "crop": "banana", "probability": 0.021894 }
]
}Error Responses:
400β Invalid state name or out-of-range inputs.503β Model not loaded yet.
GET /api/v1/states
Returns all Indian states and union territories available for prediction.
Response:
{
"count": 34,
"states": [
"Andhra Pradesh",
"Arunachal Pradesh",
"Assam",
"..."
]
}GET /api/v1/crops
Returns all crops the model is trained to predict.
Response:
{
"count": 22,
"crops": [
"apple",
"banana",
"blackgram",
"..."
]
}POST /api/v1/train
Re-trains the XGBoost model from the CSV datasets and saves the new artifacts to disk.
Response:
{
"message": "Model re-trained and saved successfully.",
"accuracy": 0.9568,
"classification_report": { "..." }
}Error Responses:
500β Training failed due to data or system errors.
# Predict a crop
curl -X POST http://localhost:8000/api/v1/predict \
-H "Content-Type: application/json" \
-d '{"state": "Punjab", "temperature": 25.0, "humidity": 70.0}'
# List available states
curl http://localhost:8000/api/v1/states
# List available crops
curl http://localhost:8000/api/v1/crops
# Health check
curl http://localhost:8000/api/v1/healthimport requests
response = requests.post(
"http://localhost:8000/api/v1/predict",
json={
"state": "Maharashtra",
"temperature": 28.5,
"humidity": 75.0,
},
)
data = response.json()
print(f"Recommended crop: {data['crop']}")
print("Top 5 recommendations:")
for item in data["top_5"]:
print(f" {item['crop']}: {item['probability']:.2%}")Contains 2,200 samples with the following features:
| Column | Description |
|---|---|
N |
Nitrogen content in soil |
P |
Phosphorous content in soil |
K |
Potassium content in soil |
temperature |
Temperature in degrees Celsius |
humidity |
Relative humidity (%) |
ph |
Soil pH value |
rainfall |
Rainfall in mm |
label |
Crop name (target variable) |
Contains state-level soil nutrient distribution data across Indian states and union territories, with columns for Nitrogen, Phosphorous, and Potassium levels categorised as VL (Very Low), Low, L, M (Medium), H, High, and VH (Very High).
- Nutrient Mapping β Each Indian state is mapped to its dominant soil nutrient levels (N, P, K) using the
place.csvdataset. - Feature Engineering β Raw nutrient values from the crop dataset are categorised into Low/Medium/High and label-encoded. These are combined with temperature and humidity as model features.
- Model Training β An XGBoost classifier is trained on the engineered features with an 80/20 train-test split and standard scaling.
- Prediction β Given a state name, the system looks up the dominant nutrient profile, encodes it, scales the input alongside temperature and humidity, and runs it through the trained model to produce crop predictions with probabilities.
| Variable | Default | Description |
|---|---|---|
HOST |
0.0.0.0 |
Server bind address |
PORT |
8000 |
Server port |
DEBUG |
false |
Enable debug mode and auto-reload |
CORS_ORIGINS |
* |
Comma-separated list of allowed origins |
This project is licensed under the terms of the MIT License.