Skip to content

A comprehensive machine learning-powered web application that provides intelligent crop recommendations based on soil and environmental parameters. The system helps farmers and agricultural professionals make informed decisions about crop selection to optimize yield and sustainability.

Notifications You must be signed in to change notification settings

Amanbig/crop-recommendation-system

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

16 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌱 Crop Recommendation System

A comprehensive machine learning-powered web application that provides intelligent crop recommendations based on soil and environmental parameters. The system helps farmers and agricultural professionals make informed decisions about crop selection to optimize yield and sustainability.

πŸ“‹ Table of Contents

Deployed at:

Website Deployment

Docker images

To run docker containers first we need to pull the docker images

docker pull procoder588/crop_recommendation_frontend
docker pull procoder588/crop_recommendation_backend

To run the containers this is the command and be sure to update your backend and frontend urls

docker run -d --name CROPBackend -p 8000:8000 --network crop -e ALLOWED_URL=<your frontend url>  procoder588/crop_recommendation_backend
docker run -d --name CROPFrontend -p 3000:3000 --network crop -e BACKEND_URL=<your backend url> procoder588/crop_recommendation_frontend

Docker compose

You could also run it directly using docker compose

docker compose up -d

✨ Features

🎯 Core Features

  • Smart Crop Recommendations: ML-powered predictions based on soil and environmental data
  • Interactive Dataset Viewer: Browse and explore the training dataset
  • Jupyter Notebook Integration: View the complete ML model development process
  • Crop Gallery: Visual catalog of all supported crops with detailed information
  • Real-time Predictions: Instant crop recommendations with confidence scores

πŸ”§ Technical Features

  • Responsive Design: Works seamlessly on desktop, tablet, and mobile devices
  • Dark/Light Theme: Toggle between themes for better user experience
  • Error Handling: Comprehensive error handling with user-friendly messages
  • Health Monitoring: Built-in health checks for both frontend and backend
  • Docker Support: Containerized deployment for easy scaling
  • API-First Architecture: Clean separation between frontend and backend

πŸ›  Technology Stack

Backend

  • FastAPI: Modern, fast web framework for building APIs
  • Python 3.11: Core programming language
  • Scikit-learn: Machine learning library for model training and predictions
  • XGBoost: Gradient boosting framework for enhanced model performance
  • Pandas: Data manipulation and analysis
  • NumPy: Numerical computing library

Frontend

  • Next.js 14: React framework with App Router
  • TypeScript: Type-safe JavaScript development
  • Tailwind CSS: Utility-first CSS framework
  • Shadcn/ui: Modern UI component library
  • Axios: HTTP client for API requests
  • React Markdown: Markdown rendering for notebooks

DevOps & Deployment

  • Docker: Containerization platform
  • Docker Compose: Multi-container Docker applications
  • Uvicorn: ASGI server for FastAPI

πŸ— Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Frontend      β”‚    β”‚   Backend       β”‚    β”‚   ML Model      β”‚
β”‚   (Next.js)     │────│   (FastAPI)     │────│   (Scikit-learn)β”‚
β”‚   Port: 3000    β”‚    β”‚   Port: 8000    β”‚    β”‚   (XGBoost)     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                       β”‚                       β”‚
         β”‚              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”              β”‚
         └──────────────│   Docker        β”‚β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                        β”‚   Network       β”‚
                        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

The application follows a microservices architecture:

  1. Frontend: Next.js application serving the user interface
  2. Backend: FastAPI server handling ML predictions and data processing
  3. ML Model: Pre-trained models for crop recommendation

πŸ“‹ Prerequisites

  • Docker & Docker Compose: For containerized deployment
  • Node.js 18+: For local frontend development
  • Python 3.11+: For local backend development
  • Git: For version control

πŸš€ Quick Start with Docker

The fastest way to get the application running:

# Clone the repository
git clone <repository-url>
cd crop-recommendation-system

# Start the application
docker-compose up -d

# View logs (optional)
docker-compose logs -f

The application will be available at:

πŸ’» Development Setup

Backend Setup

cd backend

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Run the development server
python -m uvicorn app:app --reload --host 0.0.0.0 --port 8000

Frontend Setup

cd frontend

# Install dependencies
npm install

# Set up environment variables
cp .env.example .env.local

# Run the development server
npm run dev

πŸ“– Usage

Making Crop Predictions

  1. Navigate to the Recommendation page

  2. Enter the following parameters:

    • Nitrogen (N): 0-300 kg/ha
    • Phosphorous (P): 0-150 kg/ha
    • Potassium (K): 0-300 kg/ha
    • Temperature: -10 to 50Β°C
    • Humidity: 0-100%
    • pH Level: 0-14
    • Rainfall: 0-1000mm
  3. Click "Get Crop Recommendation"

  4. View the recommended crop with confidence score and additional information

Exploring the Dataset

  • Visit the Dataset page to browse the training data
  • Use the "Load More" button to paginate through records
  • Examine the relationship between input parameters and crop labels

Viewing the ML Process

  • Navigate to the Notebook page
  • Explore the complete machine learning pipeline
  • View code cells, outputs, and visualizations
  • Toggle between light and dark themes

πŸ“š API Documentation

Base URL

  • Development: http://localhost:8000
  • Production: https://your-domain.com/api

Endpoints

Get Crop Labels

GET /labels

Returns all available crop labels with associated images.

Get Dataset

GET /data?page=1&size=100

Retrieve paginated dataset records.

Make Prediction

POST /predict
Content-Type: application/json

{
  "N": 90,
  "P": 42,
  "K": 43,
  "temperature": 20.8,
  "humidity": 82.0,
  "ph": 6.5,
  "rainfall": 202.9
}

Get Notebook

GET /notebook

Retrieve the Jupyter notebook in JSON format.

Health Check

GET /api/health

Returns service health status.

πŸ“ Project Structure

crop-recommendation-system/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ app.py                 # FastAPI application
β”‚   β”œβ”€β”€ requirements.txt       # Python dependencies
β”‚   β”œβ”€β”€ Dockerfile            # Backend container configuration
β”‚   β”œβ”€β”€ model/               # ML models
β”‚   β”œβ”€β”€ dataset/             # Training data
β”‚   β”œβ”€β”€ notebook/            # Jupyter notebooks
β”‚   └── constants/           # Application constants
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ app/             # Next.js app router
β”‚   β”‚   β”œβ”€β”€ components/      # React components
β”‚   β”‚   β”œβ”€β”€ lib/            # Utility libraries
β”‚   β”‚   └── types/          # TypeScript type definitions
β”‚   β”œβ”€β”€ package.json        # Node.js dependencies
β”‚   β”œβ”€β”€ Dockerfile          # Frontend container configuration
β”‚   └── next.config.ts      # Next.js configuration
β”œβ”€β”€ docker-compose.yml      # Multi-container configuration
└── README.md              # This file

🀝 Contributing

We welcome contributions! Please follow these steps:

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

Development Guidelines

  • Follow existing code style and conventions
  • Add tests for new features
  • Update documentation as needed
  • Ensure Docker builds pass
  • Test both frontend and backend changes

Code Style

  • Backend: Follow PEP 8 Python style guide
  • Frontend: Use Prettier and ESLint configurations
  • Commits: Use conventional commit messages

πŸ”§ Environment Variables

Backend

PYTHONPATH=/app
PYTHONUNBUFFERED=1

Frontend

BACKEND_URL=http://localhost:8000
NEXT_PUBLIC_API_URL=http://localhost:3000
NODE_ENV=production

🐳 Docker Commands

# Build and start all services
docker-compose up --build

# Start in detached mode
docker-compose up -d

# Stop all services
docker-compose down

# View logs
docker-compose logs -f [service-name]

# Rebuild a specific service
docker-compose build [service-name]

# Scale services
docker-compose up --scale backend=2

🚨 Troubleshooting

Common Issues

  1. Port Already in Use

    # Kill processes on specific ports
    sudo lsof -t -i:3000 | xargs kill -9
    sudo lsof -t -i:8000 | xargs kill -9
  2. Docker Build Fails

    # Clean Docker cache
    docker system prune -a
    docker-compose build --no-cache
  3. API Connection Issues

    • Verify backend is running on port 8000
    • Check CORS settings in FastAPI
    • Ensure environment variables are set correctly

πŸ“„ License

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

πŸ‘₯ Authors

  • Development Team - Initial work and ongoing maintenance

πŸ™ Acknowledgments

  • Scikit-learn community for excellent ML tools
  • Next.js team for the amazing React framework
  • FastAPI creators for the intuitive API framework
  • Open source community for various libraries and tools

Happy Farming! 🚜🌾

For more information, please visit our documentation or contact our support team.

Buy Me a Coffee

About

A comprehensive machine learning-powered web application that provides intelligent crop recommendations based on soil and environmental parameters. The system helps farmers and agricultural professionals make informed decisions about crop selection to optimize yield and sustainability.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 75.9%
  • Jupyter Notebook 14.7%
  • Python 3.7%
  • CSS 3.6%
  • Dockerfile 2.0%
  • JavaScript 0.1%