A FastAPI-based Machine Learning API that recommends the top 5 crops suitable for cultivation based on soil and weather parameters.
The model is powered by a RandomForest Classifier trained on the final preprocessed dataset (final_train_balanced
).
-
🚀 FastAPI backend with RESTful endpoints
-
🌾 RandomForest model trained on 7 key soil & climate features:
- Nitrogen (N)
- Phosphorus (P)
- Potassium (K)
- Temperature
- Humidity
- pH
- Rainfall
-
🔮 Returns Top-5 ranked crop recommendations with confidence scores
-
📦 Dockerized for easy deployment
-
🔑 JSON API for integration with frontend / Node.js backend
crop-recommendation-api/
│── rf.pkl # Trained RandomForest model
│── main.py # FastAPI app with endpoints
│── requirements.txt # Dependencies
│── Dockerfile # For containerization
│── README.md # Project documentation
git clone https://github.com/yourusername/crop-recommendation-api.git
cd crop-recommendation-api
python -m venv venv
source venv/bin/activate # For Linux/Mac
venv\Scripts\activate # For Windows
pip install -r requirements.txt
uvicorn main:app --reload
Server runs at 👉 http://127.0.0.1:8000
GET /
Response:
{"message": "Crop Recommendation API is running 🚀"}
POST /predict
{
"N": 90,
"P": 42,
"K": 43,
"temperature": 25.5,
"humidity": 80,
"ph": 6.5,
"rainfall": 200
}
{
"recommendations": [
{"crop": "rice", "probability": 0.65},
{"crop": "maize", "probability": 0.20},
{"crop": "banana", "probability": 0.07},
{"crop": "mango", "probability": 0.05},
{"crop": "cotton", "probability": 0.03}
]
}
docker build -t crop-api .
docker run -d -p 8000:8000 crop-api
Now API is available at 👉 http://localhost:8000
- Model: RandomForestClassifier (n_estimators=200, random_state=42)
- Dataset:
final_train_balanced
(7 numeric features + label) - Output:
rf.pkl
saved with feature names
- FastAPI – RESTful API framework
- Scikit-learn – Machine Learning (RandomForest)
- Joblib – Model persistence
- Uvicorn – ASGI server
- Docker – Containerization
- 🌍 Add state & district level recommendations
- 📈 Improve ranking with ensemble models
- 🌦️ Integrate real-time weather APIs