--- title: Plant Disease Prediction API emoji: 🌱 colorFrom: green colorTo: blue sdk: docker pinned: false license: apache-2.0 app_port: 7860 --- # Plant Disease Prediction API 🌱 A FastAPI-based web service that predicts plant diseases from leaf images using a deep learning model trained on plant pathology data. ## Features - **38 Disease Classes**: Supports detection of various diseases across multiple plant species including Apple, Corn, Grape, Tomato, Potato, and more - **Batch Processing**: Process up to 10 images simultaneously - **High Accuracy**: Uses a pre-trained CNN model for reliable disease classification - **Easy Integration**: RESTful API with comprehensive documentation - **Model Pre-warming**: Fast inference with pre-loaded model ## Supported Plants & Diseases ### Apple - Apple Scab, Black Rot, Cedar Apple Rust, Healthy ### Corn (Maize) - Cercospora Leaf Spot, Common Rust, Northern Leaf Blight, Healthy ### Tomato - Bacterial Spot, Early Blight, Late Blight, Leaf Mold, Septoria Leaf Spot, Spider Mites, Target Spot, Yellow Leaf Curl Virus, Mosaic Virus, Healthy ### Grape - Black Rot, Esca (Black Measles), Leaf Blight, Healthy ### Potato - Early Blight, Late Blight, Healthy ### Other Plants - Blueberry, Cherry, Orange, Peach, Bell Pepper, Raspberry, Soybean, Squash, Strawberry ## API Endpoints ### `POST /predict` Upload one or more images to get disease predictions. **Request:** - Content-Type: `multipart/form-data` - Body: Image files (jpg, png, bmp, gif, tiff, webp) **Response:** ```json { "success": true, "results": [ { "predicted_class": "Tomato___Late_blight", "clean_class_name": "Tomato - Late blight", "confidence": 0.95, "all_predictions": { "Apple - Apple scab": 0.001, "Tomato - Late blight": 0.95 // ... other classes } } ], "message": "Successfully processed 1 image(s)" } ``` ### `GET /health` Check API and model status. ### `GET /classes` Get all supported disease classes. ### `GET /` API information and status. ## Usage Examples ### Python ```python import requests # Single image prediction with open("leaf_image.jpg", "rb") as f: files = {"files": ("leaf_image.jpg", f, "image/jpeg")} response = requests.post("https://your-space-url/predict", files=files) result = response.json() print(f"Predicted: {result['results'][0]['clean_class_name']}") print(f"Confidence: {result['results'][0]['confidence']:.2%}") ``` ### cURL ```bash curl -X POST "https://your-space-url/predict" \ -F "files=@leaf_image.jpg" ``` ### JavaScript ```javascript const formData = new FormData(); formData.append("files", fileInput.files[0]); fetch("/predict", { method: "POST", body: formData, }) .then((response) => response.json()) .then((data) => console.log(data)); ``` ## Model Information - **Architecture**: Convolutional Neural Network (CNN) - **Input Size**: 300x300 RGB images - **Training Data**: PlantVillage dataset - **Classes**: 38 plant disease categories - **Framework**: TensorFlow/Keras ## Performance - **Batch Size**: Up to 10 images per request - **Response Time**: ~1-3 seconds per image - **Memory Usage**: ~2GB for model loading - **Accuracy**: >95% on validation set ## Limitations - Best results with clear, well-lit leaf images - Works optimally with images similar to training data - Single leaf per image recommended - Supported image formats: JPG, PNG, BMP, GIF, TIFF, WEBP ## Environment Variables - `HF_MODEL_REPO`: Hugging Face model repository (default: "yasyn14/smart-leaf-model") - `HF_MODEL_FILENAME`: Model file name (default: "best_model_32epochs.keras") - `HF_HOME`: Cache directory for models (default: "/tmp/huggingface") ## License Apache 2.0 License ## Support For issues and questions, please visit the [model repository](https://huggingface.co/yasyn14/smart-leaf-model) or create an issue in this space. --- _Built with FastAPI, TensorFlow, and 🤗 Transformers_