Skip to content

CaueGrassi7/smart-crop-prediction

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

15 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌾 Smart Crop Prediction

An end-to-end AgroTech Machine Learning application that recommends the optimal crop to plant based on soil and environmental conditions.

Python Next.js FastAPI Docker License

Smart Crop Prediction Demo


πŸ“– About the Project

Making the right crop choice is crucial for farmers to maximize yield and optimize resource usage. Smart Crop Prediction addresses this challenge by leveraging machine learning to analyze soil nutrients and environmental factors, providing data-driven recommendations for the best crop to plant.

🎯 Problem Solved

Traditional crop selection often relies on intuition or outdated practices, leading to suboptimal harvests and resource waste. This application empowers farmers and agricultural professionals with:

  • Instant predictions based on soil composition (N, P, K levels)
  • Environmental analysis considering temperature, humidity, and rainfall
  • Scientific backing through a trained ML model with 99.32% accuracy

πŸ€– Machine Learning Model

  • Algorithm: Random Forest Classifier (100 estimators)
  • Training Data: 2,200 samples across 22 different crop types
  • Features: Nitrogen (N), Phosphorus (P), Potassium (K), Temperature, Humidity, pH, Rainfall
  • Accuracy: 99.32% on test set

πŸ“Έ Screenshots

Initial Form Prediction Result
Initial Form Prediction Result

πŸ› οΈ Tech Stack

Layer Technology
Frontend Next.js 16, React 19, Tailwind CSS 4, Axios
Backend FastAPI, Python 3.11+, Uvicorn
Machine Learning Scikit-Learn, Pandas, NumPy, Joblib
DevOps Docker, Docker Compose
Data Visualization Matplotlib, Seaborn (Jupyter Notebooks)

πŸš€ How to Run

Option A: Docker (Recommended) 🐳

The easiest way to get started. Just make sure you have Docker installed.

# Clone the repository
git clone https://github.com/cauegrassi7/smart-crop-prediction.git
cd smart-crop-prediction

# Build and run with Docker Compose
docker-compose up --build

Once running:


Option B: Manual Setup πŸ”§

1. Backend Setup

# Navigate to backend directory
cd backend

# Create virtual environment
python -m venv venv

# Activate virtual environment
# On macOS/Linux:
source venv/bin/activate
# On Windows:
# venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

2. Train the Model (Optional)

The model is pre-trained and included in the repository. If you want to retrain it:

# From the backend directory
python train_model.py

This will generate the crop_prediction_model.pkl file in the models/ folder.

3. Start the Backend Server

# From the backend directory
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000

4. Frontend Setup

Open a new terminal:

# Navigate to frontend directory
cd frontend

# Install dependencies
npm install

# Start development server
npm run dev

πŸ“ Project Structure

smart-crop-prediction/
β”‚
β”œβ”€β”€ πŸ“‚ backend/                    # FastAPI Backend
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ main.py               # API routes & FastAPI app
β”‚   β”‚   └── schemas.py            # Pydantic data models
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   └── crop_prediction_model.pkl  # Trained ML model
β”‚   β”œβ”€β”€ train_model.py            # Model training script
β”‚   β”œβ”€β”€ requirements.txt          # Python dependencies
β”‚   └── Dockerfile
β”‚
β”œβ”€β”€ πŸ“‚ frontend/                   # Next.js Frontend
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ page.tsx              # Main application page
β”‚   β”‚   β”œβ”€β”€ layout.tsx            # Root layout
β”‚   β”‚   └── globals.css           # Global styles
β”‚   β”œβ”€β”€ public/                   # Static assets
β”‚   β”œβ”€β”€ package.json
β”‚   └── Dockerfile
β”‚
β”œβ”€β”€ πŸ“‚ data/                       # Dataset
β”‚   └── Crop_recommendation.csv   # Training data (2,200 samples)
β”‚
β”œβ”€β”€ πŸ“‚ notebooks/                  # Jupyter Notebooks
β”‚   └── 01_model_training.ipynb   # EDA & model development
β”‚
β”œβ”€β”€ docker-compose.yml            # Multi-container orchestration
└── README.md

πŸ“‘ API Documentation

The API documentation is automatically generated and available via Swagger UI when the backend is running:

πŸ”— http://localhost:8000/docs

Swagger UI Documentation

Main Endpoints

Method Endpoint Description
GET / Health check & welcome message
GET /health Health check for Docker/K8s
POST /predict Get crop recommendation

Example Request

curl -X POST "http://localhost:8000/predict" \
  -H "Content-Type: application/json" \
  -d '{
    "N": 90,
    "P": 42,
    "K": 43,
    "temperature": 20.8,
    "humidity": 82.0,
    "ph": 6.5,
    "rainfall": 202.9
  }'

Example Response

{
  "input": {
    "N": 90.0,
    "P": 42.0,
    "K": 43.0,
    "temperature": 20.8,
    "humidity": 82.0,
    "ph": 6.5,
    "rainfall": 202.9
  },
  "recommended_crop": "rice"
}

🌱 Input Features

Feature Description Unit
N Nitrogen content in soil kg/ha
P Phosphorus content in soil kg/ha
K Potassium content in soil kg/ha
Temperature Average temperature Β°C
Humidity Relative humidity %
pH Soil pH level 0-14
Rainfall Annual rainfall mm

🌾 Supported Crops

The model can predict among 22 different crop types, including:

Rice, Wheat, Maize, Chickpea, Kidney Beans, Pigeon Peas, Moth Beans, Mung Bean, Black Gram, Lentil, Pomegranate, Banana, Mango, Grapes, Watermelon, Muskmelon, Apple, Orange, Papaya, Coconut, Cotton, Jute, Coffee


πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


🀝 Contributing

Contributions are welcome! Feel free to open issues or submit pull requests.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Made with πŸ’š for sustainable agriculture

About

🌾 End-to-end AgroTech ML application that recommends optimal crops based on soil metrics using FastAPI, Next.js, and Docker.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors