Spaces:
Sleeping
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. | |
| [](https://python.org) | |
| [](https://flask.palletsprojects.com) | |
| [](https://scikit-learn.org) | |
| [](LICENSE) | |
| --- | |
| **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](https://raw.githubusercontent.com/lovnishverma/datasets/refs/heads/main/Crop.csv). 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`](classification.ipynb) for the full training walkthrough. | |
| --- | |
| ## π Getting Started | |
| ### 1. Clone the repository | |
| ```bash | |
| git clone https://github.com/lovnishverma/crop-predict.git | |
| cd crop-predict | |
| ``` | |
| ### 2. Install dependencies | |
| ```bash | |
| pip install -r requirements.txt | |
| ``` | |
| ### 3. Run the app | |
| ```bash | |
| python app.py | |
| ``` | |
| Open your browser at **http://127.0.0.1:5000** | |
| > **Note:** The pre-trained `crop_recommendation_model.joblib` is already included. If you want to retrain, open `classification.ipynb` in Google Colab and run all cells β it will regenerate the `.joblib` file. | |
| --- | |
| ## π API Reference | |
| ### `POST /predict` | |
| Accepts `multipart/form-data` with all 11 soil parameters. | |
| **Request (curl example):** | |
| ```bash | |
| 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:** | |
| ```json | |
| { | |
| "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`](https://github.com/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 | |
| [](https://github.com/lovnishverma) | |
| [](https://linkedin.com/in/lovnishverma) | |
| [](https://huggingface.co/lovnishverma) | |
| [](https://lovnishverma.in) | |
| --- | |
| ## π 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. | |
| 1. Create an account on [Render](https://render.com/) and go to your Dashboard. | |
| 2. Click **New** and select **Web Service**. | |
| 3. Connect your GitHub account and select the `crop-predict` repository. | |
| 4. Configure the service with the following settings: | |
| * **Runtime:** Python | |
| * **Build Command:** `pip install -r requirements.txt` | |
| * **Start Command:** `gunicorn app:app` | |
| 5. **β οΈ 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-compiled `scikit-learn` binaries instead of taking 30+ minutes to build from source).* | |
| 6. 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. | |
| 1. Create an account on [Hugging Face](https://huggingface.co/) and navigate to **Spaces**. | |
| 2. Click **Create new Space**. | |
| 3. 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) | |
| 4. Click **Create Space**. | |
| 5. 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.py` | |
| * `requirements.txt` | |
| * `crop_recommendation_model.joblib` | |
| * `templates/` and `static/` folders | |
| 6. 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. πΎ | |