Spaces:
Sleeping
Sleeping
metadata
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:
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:
{
"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):
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
- Combine datasets
python scripts/combine_datasets.py
- Explore data
python scripts/analyze_combined_data.py
- Train models
python src/crop_yield_ml_pipeline.py
- Test models (on a sample) and generate plots
python scripts/test_models.py
- Show summary
python scripts/results_summary.py
API Service
- Start the FastAPI server locally:
python src/app.py
Access the API documentation at:
http://localhost:8000/docsTest the API:
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 for complete deployment instructions.
The application is configured for deployment on Railway:
- The
start.pyfile handles Railway's dynamic PORT configuration - The
Dockerfilebuilds a container with all dependencies - The
railway.jsonfile configures Railway deployment settings - Access the deployed API at: https://sih-2-production.up.railway.app/
Notes
- By default, .gitignore excludes large artifacts in
data/*.csv,artifacts/*.png, andlogs/*.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
/docsendpoint for interactive API testing