Spaces:
Sleeping
Sleeping
| 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 | |