Spaces:
Sleeping
title: Croppredict
emoji: π
colorFrom: blue
colorTo: pink
sdk: docker
license: mit
short_description: CropSense β Crop Recommendation System
pinned: false
π± CropSense β Crop Recommendation System
A Flask web application that predicts the most suitable crop to grow based on soil nutrient parameters. Built with Scikit-learn and deployed with a clean, responsive UI.
Live Demo: https://crop-predict-wgrr.onrender.com/
Alternate Link: https://lovnishverma-croppredict.hf.space/
HF Space: https://huggingface.co/spaces/LovnishVerma/croppredict
π Overview
CropSense takes 11 soil parameters as input and recommends the most suitable crop using a Logistic Regression model trained on the Crop dataset. It also returns the top-3 predictions with confidence percentages.
This project was developed as a teaching artifact for the IndiaAI Foundations of Artificial Intelligence programme at NIELIT Ropar, demonstrating the end-to-end ML workflow: data β model β deployment.
ποΈ Project Structure
crop-predict/
βββ app.py # Flask application
βββ classification.ipynb # Model training notebook (Colab)
βββ crop_recommendation_model.joblib # Saved trained model
βββ requirements.txt
βββ README.md # This File (Documentation)
βββ static/
β βββ css/ # Stylesheets
βββ templates/
βββ index.html # Frontend UI
π§ͺ Input Features
The model uses 11 soil parameters:
| Feature | Description | Unit |
|---|---|---|
| N | Nitrogen | kg/ha |
| P | Phosphorus | kg/ha |
| K | Potassium | kg/ha |
| ph | pH Level | β |
| EC | Electrical Conductivity | dS/m |
| S | Sulfur | mg/kg |
| Cu | Copper | mg/kg |
| Fe | Iron | mg/kg |
| Mn | Manganese | mg/kg |
| Zn | Zinc | mg/kg |
| B | Boron | mg/kg |
Sample values (pomegranate): 175, 36, 216, 5.9, 0.15, 0.28, 15.69, 114.20, 56.87, 31.28, 28.62
βοΈ ML Pipeline
Dataset (Crop.csv)
β
Feature/Target Split (X = 11 soil params, y = crop label)
β
Train-Test Split (80/20, random_state=42)
β
Logistic Regression (sklearn)
β
Evaluation (accuracy_score, classification_report, confusion matrix)
β
Model Serialization (joblib β .joblib file)
β
Flask REST API (/predict)
See classification.ipynb for the full training walkthrough.
π Getting Started
1. Clone the repository
git clone https://github.com/lovnishverma/crop-predict.git
cd crop-predict
2. Install dependencies
pip install -r requirements.txt
3. Run the app
python app.py
Open your browser at http://127.0.0.1:5000
Note: The pre-trained
crop_recommendation_model.joblibis already included. If you want to retrain, openclassification.ipynbin Google Colab and run all cells β it will regenerate the.joblibfile.
π API Reference
POST /predict
Accepts multipart/form-data with all 11 soil parameters.
Request (curl example):
curl -X POST http://127.0.0.1:5000/predict \
-F "N=175" -F "P=36" -F "K=216" -F "ph=5.9" -F "EC=0.15" \
-F "S=0.28" -F "Cu=15.69" -F "Fe=114.20" -F "Mn=56.87" \
-F "Zn=31.28" -F "B=28.62"
Response:
{
"crop": "pomegranate",
"top3": [
{ "crop": "pomegranate", "prob": 87.3 },
{ "crop": "mango", "prob": 7.1 },
{ "crop": "grapes", "prob": 3.2 }
]
}
π¦ Requirements
flask>=2.3
scikit-learn>=1.3
pandas>=2.0
joblib>=1.3
numpy>=1.24
gunicorn>=20.1.0
π Dataset
- Source:
lovnishverma/datasetsβCrop.csv - Features: 11 soil nutrient columns
- Target:
labelβ crop name (e.g. pomegranate, wheat, rice, maize, etc.)
π Author
Lovnish Verma Project Engineer, NIELIT Ropar (Deemed University), Punjab
π License
This project is licensed under the MIT License.
βοΈ Deployment Guides
This app is designed to be easily deployed to modern cloud platforms. Below are the steps for deploying to Render and Hugging Face Spaces.
π Deploy on Render
Render provides a seamless way to deploy Python web services directly from your GitHub repository.
- Create an account on Render and go to your Dashboard.
- Click New and select Web Service.
- Connect your GitHub account and select the
crop-predictrepository. - Configure the service with the following settings:
- Runtime: Python
- Build Command:
pip install -r requirements.txt - Start Command:
gunicorn app:app
- β οΈ Crucial Step: Scroll down to Environment Variables, click Add Environment Variable, and add the following:
- Key:
PYTHON_VERSION - Value:
3.11.0(Note: This forces Render to use Python 3.11, allowing it to instantly download pre-compiledscikit-learnbinaries instead of taking 30+ minutes to build from source).
- Key:
- Click Deploy Web Service. Your app will be live in a few minutes!
Docs: Docs on specifying a Python version: https://render.com/docs/python-version
π€ Deploy on Hugging Face Spaces
Hugging Face Spaces is an excellent platform for hosting machine learning demos using Docker.
- Create an account on Hugging Face and navigate to Spaces.
- Click Create new Space.
- Fill in the configuration:
- Space name:
croppredict(or your preferred name) - License: MIT
- Select the Space SDK: Choose Docker -> Blank.
- Space hardware: Free (CPU basic)
- Space name:
- Click Create Space.
- You can now upload your project files directly via the Files tab in your browser, or push them using Git. Ensure the following files are included in the root directory:
Dockerfile(HF uses this to build your environment)app.pyrequirements.txtcrop_recommendation_model.joblibtemplates/andstatic/folders
- Once the files are uploaded, Hugging Face will automatically read the
Dockerfile, build the container, and launch your Flask app!
Built for IndiaAI students at NIELIT Ropar Β· No brainrot. Just facts. πΎ