croppredict / README.md
LovnishVerma's picture
Update README.md
87c574c verified
|
Raw
History Blame Contribute Delete
7.84 kB
metadata
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.

Python Flask Scikit-learn License: MIT


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.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):

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

GitHub LinkedIn HuggingFace Portfolio


πŸ“„ 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 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 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. 🌾