metadata
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 informationGET /health
- Health check endpointGET /model-info
- Model performance metrics and detailsGET /crops
- List of supported cropsGET /fertilizers
- List of available fertilizers
Prediction Endpoints
POST /predict
- Single fertilizer recommendationPOST /batch-predict
- Batch processing for multiple samples
Utility Endpoints
GET /docs
- Interactive Swagger documentationGET /redoc
- Alternative API documentationGET /debug/files
- Debug information
π§ Usage Examples
Single Prediction
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