--- title: Smart Fertilizer Recommender API emoji: 🌾 colorFrom: green colorTo: blue sdk: docker pinned: false license: mit short_description: AI-powered fertilizer recommendations for crops --- # 🌾 Smart Fertilizer Recommender API A FastAPI-based REST API that provides intelligent fertilizer recommendations using a Random Forest machine learning model. The system analyzes soil nutrient parameters and crop type to suggest the most suitable fertilizer. ## 🚀 Features - **Intelligent Recommendations**: ML-powered fertilizer suggestions based on soil analysis - **Multi-Crop Support**: Recommendations for various crop types - **Confidence Scoring**: Each recommendation comes with a confidence percentage - **Input Validation**: Automatic validation of soil parameter ranges - **Batch Processing**: Process multiple soil samples at once - **Interactive Documentation**: Built-in Swagger UI for easy testing ## 🔬 Model Information - **Algorithm**: Random Forest Classifier - **Features**: Nitrogen %, Phosphorus (Olsen) ppm, Potassium meq%, Soil pH, Crop Type - **Training Accuracy**: ~95%+ - **Cross-Validation**: 5-fold CV for robust evaluation ## 📊 API Endpoints ### Core Endpoints - `GET /` - Welcome message and API information - `GET /health` - Health check endpoint - `GET /model-info` - Model performance metrics and details - `GET /crops` - List of supported crops - `GET /fertilizers` - List of available fertilizers ### Prediction Endpoints - `POST /predict` - Single fertilizer recommendation - `POST /batch-predict` - Batch processing for multiple samples ### Utility Endpoints - `GET /docs` - Interactive Swagger documentation - `GET /redoc` - Alternative API documentation - `GET /debug/files` - Debug information ## 🔧 Usage Examples ### Single Prediction ```python import requests # API endpoint url = "https://OwinoGoddy.hf.space/predict" # Soil parameters data = { "nitrogen_percent": 0.08, "phosphorus_ppm": 23.0, "soil_ph": 5.76, "potassium_meq_percent": 1.36, "crop": "maize" } # Make prediction response = requests.post(url, json=data) result = response.json() print(f"Recommended Fertilizer: {result['primary_recommendation']}") print(f"Confidence: {result['confidence']}%") Response Example json{ "primary_recommendation": "NPK 20-10-10", "confidence": 87.5, "all_recommendations": [ { "fertilizer": "NPK 20-10-10", "confidence": 87.5 }, { "fertilizer": "DAP", "confidence": 8.2 }, { "fertilizer": "Urea", "confidence": 3.1 } ], "input_parameters": { "nitrogen_percent": 0.08, "phosphorus_ppm": 23.0, "soil_ph": 5.76, "potassium_meq_percent": 1.36, "crop": "maize" }, "validation_warnings": [] } 📝 Input Parameters ParameterTypeRangeDescriptionnitrogen_percentfloat0-5%Total Nitrogen percentagephosphorus_ppmfloat0-200 ppmPhosphorus Olsen in ppmsoil_phfloat3-10Soil pH levelpotassium_meq_percentfloat0-10%Potassium in meq%cropstring-Crop name (see /crops endpoint) 🌱 Supported Crops The system supports various crop types including: Maize Beans Wheat Rice Sorghum And many more... Use the /crops endpoint to get the complete list of supported crops. 🔍 Model Validation The API includes automatic input validation: Range checking for all numerical parameters Statistical validation against training data Crop name validation Warning system for unusual values 🚀 Getting Started Access the API: Visit the Hugging Face Space URL Interactive Testing: Use /docs for Swagger UI Get Crops: Check /crops for supported crop types Make Predictions: Send POST requests to /predict 📈 Performance Metrics Accuracy: High accuracy on test data Speed: Fast predictions (<100ms typical) Reliability: Robust error handling and validation Scalability: Supports batch processing 🛠️ Technical Stack Backend: FastAPI ML Framework: scikit-learn Model: Random Forest Classifier Validation: Pydantic Documentation: Swagger/OpenAPI Deployment: Docker + Hugging Face Spaces 📞 Support For questions, issues, or feature requests, please visit the Hugging Face Space discussion tab. 📄 License MIT License - feel free to use this API for your agricultural applications! Built with ❤️ for sustainable agriculture