Skip to content

RitikOnWork/CropCast

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation


🌾 CropCast

AI-Powered Agricultural Yield Prediction — Built with XGBoost, FastAPI & React

OverviewFeaturesTech StackProject StructureGetting StartedAPI ReferenceModel Details


📖 Overview

CropCast is a full-stack web application that predicts agricultural crop yield (in tonnes/hectare) using a machine learning model trained on 10 years of ICAR (Indian Council of Agricultural Research) crop production data.

Users can select their State, District, Season, Crop, Year, and Area (in hectares) — and receive an instant AI-driven prediction for expected yield and total production.

CropCast outperforms a baseline Linear Regression model by up to 34% with an expected RMSE of ~0.18.


✨ Features

  • 🤖 XGBoost ML Model — High-accuracy gradient boosted regression for yield prediction
  • FastAPI Backend — Lightning-fast REST API with LRU caching for repeated predictions
  • ⚛️ React + Vite Frontend — Modern, responsive UI with glassmorphism design
  • 🗺️ India-wide Coverage — Supports all Indian states, districts, seasons, and crops
  • 📊 Dual Output — Predicted yield per hectare + estimated total production
  • 🔄 Dynamic Dropdowns — Fetched live from the API based on trained encoder classes
  • 🌑 Dark Mode UI — Sleek dark-themed interface with animated glow effects
  • 🐍 Streamlit Prototype — Included legacy Streamlit app for quick local testing

🛠️ Tech Stack

Backend

Technology Version Purpose
Python 3.10+ Core language
FastAPI 0.110.0 REST API framework
Uvicorn 0.28.0 ASGI server
XGBoost 2.0.3 ML model for yield prediction
scikit-learn 1.4.1 Label encoding & preprocessing
Pandas 2.2.1 Data manipulation
Joblib 1.3.2 Model serialization

Frontend

Technology Version Purpose
React 19.x UI framework
Vite 8.x Build tool & dev server
Vanilla CSS Custom glassmorphism styling

📁 Project Structure

CropCast/
│
├── backend/
│   ├── app.py                          # Streamlit app (legacy prototype)
│   ├── main.py                         # FastAPI server (main backend)
│   ├── train_model.py                  # Script to train & save the XGBoost model
│   ├── Crop prediction model .py       # Jupyter-style crop yield prediction notebook
│   ├── Crop recommendation .py        # Crop recommendation logic
│   ├── Crop_Recommendation_Testing_Final.py  # Final testing script
│   ├── Data Analysis & Visualization - crop yield dataset.py
│   ├── Crop_recommendation.csv         # Crop recommendation dataset
│   ├── crop_production.csv             # Main ICAR crop production dataset (10 years)
│   ├── xgb_model.pkl                   # Trained XGBoost model (serialized)
│   ├── le_dict.pkl                     # Label encoder dictionary (serialized)
│   └── requirements.txt               # Python dependencies
│
├── frontend/
│   ├── public/
│   │   ├── bg.png                      # Background image
│   │   └── favicon.svg
│   ├── src/
│   │   ├── App.jsx                     # Main React component
│   │   ├── App.css                     # Component styles
│   │   ├── index.css                   # Global styles & design system
│   │   └── main.jsx                    # React entry point
│   ├── .env                            # Environment variables (see setup)
│   ├── index.html
│   ├── package.json
│   └── vite.config.js
│
├── .gitignore
└── README.md

🚀 Getting Started

Prerequisites

  • Python 3.10 or higher
  • Node.js 18 or higher
  • npm 9 or higher

1. Clone the Repository

git clone https://github.com/RitikOnWork/CropCast.git
cd CropCast

2. Backend Setup

cd backend

# Create and activate a virtual environment (recommended)
python -m venv venv
venv\Scripts\activate        # Windows
# source venv/bin/activate   # macOS/Linux

# Install dependencies
pip install -r requirements.txt

Train the Model (if .pkl files are missing)

python train_model.py

Start the FastAPI Server

uvicorn main:app --reload --port 8000

The API will be available at: http://localhost:8000 Interactive docs at: http://localhost:8000/docs


3. Frontend Setup

cd ../frontend

# Install dependencies
npm install

Configure Environment Variables

Create a .env file in the frontend/ directory:

VITE_API_URL=http://localhost:8000

Start the Development Server

npm run dev

The app will be available at: http://localhost:5173


📡 API Reference

GET /api/options

Returns all available dropdown options loaded from the trained label encoders.

Response:

{
  "states": ["Andhra Pradesh", "Bihar", ...],
  "districts": ["Agra", "Aligarh", ...],
  "seasons": ["Kharif", "Rabi", "Whole Year", ...],
  "crops": ["Apple", "Banana", "Rice", "Wheat", ...]
}

POST /api/predict

Predicts crop yield for the given inputs.

Request Body:

{
  "state": "Andhra Pradesh",
  "district": "Guntur",
  "season": "Kharif",
  "crop": "Rice",
  "year": 2025,
  "area": 500.0
}

Response:

{
  "predicted_yield_per_hectare": 2.84,
  "estimated_production": 1420.0
}
Field Type Description
state string Indian state name
district string District within the state
season string Crop season (Kharif, Rabi, etc.)
crop string Crop name
year integer Crop year (1990–2030)
area float Area under cultivation (hectares)

🧠 Model Details

Property Value
Model Type XGBoost Regressor
Training Data ICAR Crop Production Dataset (10 years)
Target Variable Crop Yield (tonnes/hectare)
Features State, District, Season, Crop, Year, Area
Encoding Label Encoding (scikit-learn)
Serialization Joblib (.pkl)
RMSE ~0.18
Improvement over Baseline Up to 34% over Linear Regression

Feature Engineering

  • Categorical features (State_Name, District_Name, Season, Crop) are label encoded and stored in le_dict.pkl.
  • The model is cached at load time using @st.cache_resource (Streamlit) and lru_cache (FastAPI) to optimize repeated predictions.

🔮 Roadmap

  • Filter districts dynamically based on selected state
  • Add confidence intervals to predictions
  • Historical yield trend visualizations (charts)
  • Deploy backend on Render / Railway
  • Deploy frontend on Vercel / Netlify
  • Add crop recommendation feature to the UI

🤝 Contributing

Contributions are welcome! Please follow these steps:

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

📄 License

This project is licensed under the MIT License.


Made with ❤️ by Ritik

About

Trained an XGBoost regression model on 10 years of ICAR crop and weather data; achieved RMSE of 0.18 (tonnes/hectare) outperforming a baseline Linear Regression by 34%.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors