--- 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