SIH-Crop-Yield-API / README.md
AshrafGalibSk's picture
Upload folder using huggingface_hub
bbd5f9c verified
|
Raw
History Blame Contribute Delete
5.49 kB
---
title: SIH Crop Yield Prediction API
emoji: 🌾
colorFrom: green
colorTo: blue
sdk: docker
pinned: false
license: mit
---
# 🌾 SIH Crop Yield Prediction API
A FastAPI-based machine learning application that predicts crop yields using Random Forest models, deployed on Hugging Face Spaces.
## πŸš€ Quick Start - API Usage
### Making Predictions
**POST /predict** - Main prediction endpoint:
```bash
curl -X POST https://your-space-url/predict \
-H "Content-Type: application/json" \
-d '{
"year": 2024,
"state": "Punjab",
"crop": "Rice",
"season": "Kharif",
"area": 10.0,
"production": 25.0,
"rainfall": 1200,
"fertilizer": 75,
"pesticide": 8
}'
```
**Response:**
```json
{
"model": "Random Forest",
"predicted_yield": "2017.7 kg/hectare",
"total_expected_production": "20.18 tons",
"assessment": "Good yield expected"
}
```
### Other Endpoints
- **GET /** - API information
- **GET /health** - Health check
- **GET /docs** - Interactive documentation
- **GET /available-options** - Supported crops, states, seasons
---
# πŸ“ Development Documentation
This repository contains a complete pipeline to combine datasets, train multiple models for crop yield prediction, and serve predictions via a FastAPI web service.
## Project Structure
```
.
β”œβ”€β”€ src/ # Main application code
β”‚ β”œβ”€β”€ app.py # FastAPI web service
β”‚ β”œβ”€β”€ crop_yield_ml_pipeline.py # ML training pipeline
β”‚ β”œβ”€β”€ crop_yield_predictor.py # Prediction utilities
β”‚ └── test_api.py # API testing utilities
β”œβ”€β”€ scripts/ # Utility scripts
β”‚ β”œβ”€β”€ combine_datasets.py # Data combining script
β”‚ β”œβ”€β”€ analyze_combined_data.py # Data analysis script
β”‚ β”œβ”€β”€ results_summary.py # Results summary script
β”‚ └── test_models.py # Model evaluation script
β”œβ”€β”€ data/ # Datasets and CSV files
β”‚ β”œβ”€β”€ crop_yield.csv # Historical crop yield data
β”‚ β”œβ”€β”€ combined_crop_data.csv # Generated merged dataset
β”‚ └── sample_batch.csv # Sample data for testing
β”œβ”€β”€ models/ # Trained models and preprocessors
β”‚ β”œβ”€β”€ preprocessor.pkl # Data preprocessor
β”‚ β”œβ”€β”€ random_forest_model.pkl # Random Forest model
β”‚ β”œβ”€β”€ xgboost_model.json # XGBoost model
β”‚ └── pytorch_model.pth # PyTorch model
β”œβ”€β”€ docs/ # Documentation
β”‚ β”œβ”€β”€ API_DOCUMENTATION.md # API usage guide
β”‚ β”œβ”€β”€ DEPLOYMENT_GUIDE.md # Deployment instructions
β”‚ β”œβ”€β”€ CLI_USAGE.md # CLI usage guide
β”‚ └── PROJECT_SUMMARY.md # Project overview
β”œβ”€β”€ tests/ # Test files
β”‚ β”œβ”€β”€ test_predictor.py # Predictor tests
β”‚ └── test_server.py # Server tests
β”œβ”€β”€ deployment/ # Deployment configurations
β”‚ β”œβ”€β”€ docker-compose.yml # Docker Compose setup
β”‚ └── render.yaml # Render deployment config
β”œβ”€β”€ logs/ # Application logs
β”œβ”€β”€ artifacts/ # Generated plots and visualizations
└── README.md # This file
```
## Requirements
We recommend using conda.
- Python 3.9
- pandas, numpy, scikit-learn
- xgboost (CPU), seaborn, matplotlib
- pytorch with CUDA (for GPU training)
Example installation (Linux, CUDA 11.8):
```bash
conda install -y pandas numpy scikit-learn xgboost seaborn matplotlib
conda install -y pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
```
## Usage
### Data Pipeline
1) Combine datasets
```bash
python scripts/combine_datasets.py
```
2) Explore data
```bash
python scripts/analyze_combined_data.py
```
3) Train models
```bash
python src/crop_yield_ml_pipeline.py
```
4) Test models (on a sample) and generate plots
```bash
python scripts/test_models.py
```
5) Show summary
```bash
python scripts/results_summary.py
```
### API Service
1) Start the FastAPI server locally:
```bash
python src/app.py
```
2) Access the API documentation at: `http://localhost:8000/docs`
3) Test the API:
```bash
python src/test_api.py
```
### Deployment
βœ… **RAILWAY DEPLOYMENT STATUS: READY**
All critical deployment issues have been resolved:
- **Model Loading**: βœ… Intelligent fallback system implemented
- **Port Configuration**: βœ… Dynamic PORT variable support
- **API Endpoints**: βœ… All working correctly (/health, /predict, etc.)
- **Error Handling**: βœ… Graceful failure recovery
**See [DEPLOYMENT_FIX_GUIDE.md](DEPLOYMENT_FIX_GUIDE.md) for complete deployment instructions.**
The application is configured for deployment on Railway:
1) The `start.py` file handles Railway's dynamic PORT configuration
2) The `Dockerfile` builds a container with all dependencies
3) The `railway.json` file configures Railway deployment settings
4) Access the deployed API at: https://sih-2-production.up.railway.app/
## Notes
- By default, .gitignore excludes large artifacts in `data/*.csv`, `artifacts/*.png`, and `logs/*.log`. Remove those rules if you want to commit them.
- Model files in `models/` are included for deployment (required for Railway)
- XGBoost runs with CPU (hist). PyTorch uses CUDA if available.
- The FastAPI service is production-ready and deployed on Railway
- Use the `/docs` endpoint for interactive API testing