Upload folder using huggingface_hub
Browse files- .gitattributes +35 -35
- Dockerfile +17 -0
- README.md +10 -0
- app.py +220 -0
- crop_disease_model.pkl +3 -0
- models_download.py +17 -0
- requirements.txt +10 -0
.gitattributes
CHANGED
|
@@ -1,35 +1,35 @@
|
|
| 1 |
-
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
-
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
-
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
-
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
-
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
-
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
-
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
-
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
-
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
-
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
-
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
-
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
-
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
-
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
-
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
-
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
-
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
-
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
-
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
-
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
-
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
-
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
-
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
-
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
-
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
-
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
-
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
-
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
-
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
-
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
-
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
-
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
-
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
-
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
-
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
Dockerfile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
# copy code and requirements
|
| 6 |
+
COPY . /app
|
| 7 |
+
RUN pip install --upgrade pip
|
| 8 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 9 |
+
|
| 10 |
+
# (optional) set env to tell uvicorn to use lower log/threads
|
| 11 |
+
ENV PYTHONUNBUFFERED=1
|
| 12 |
+
|
| 13 |
+
# Ports: Spaces expects the app to listen on 7860 by convention for UIs/APIs
|
| 14 |
+
EXPOSE 7860
|
| 15 |
+
|
| 16 |
+
# Start Uvicorn serving your FastAPI app module `app:app` (adjust if your module/file name differs)
|
| 17 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860", "--loop", "uvloop", "--workers", "1"]
|
README.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Crop Disease Detection
|
| 3 |
+
emoji: 👀
|
| 4 |
+
colorFrom: yellow
|
| 5 |
+
colorTo: blue
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import warnings
|
| 2 |
+
import io
|
| 3 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from fastai.learner import load_learner
|
| 7 |
+
import joblib
|
| 8 |
+
|
| 9 |
+
from models_download import download_models
|
| 10 |
+
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
learn = None
|
| 14 |
+
|
| 15 |
+
def read_imagefile(data):
|
| 16 |
+
return Image.open(io.BytesIO(data))
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def load_model_safely():
|
| 20 |
+
"""Load the FastAI model with proper warning handling"""
|
| 21 |
+
try:
|
| 22 |
+
with warnings.catch_warnings():
|
| 23 |
+
warnings.filterwarnings("ignore", category=UserWarning, message=".*load_learner.*")
|
| 24 |
+
learn = load_learner("crop_disease_model.pkl")
|
| 25 |
+
return learn
|
| 26 |
+
except FileNotFoundError:
|
| 27 |
+
print("Warning: Model file 'crop_disease_model.pkl' not found. Please ensure the model file exists.")
|
| 28 |
+
return None
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print(f"Error loading model: {e}")
|
| 31 |
+
return None
|
| 32 |
+
|
| 33 |
+
disease_info = {
|
| 34 |
+
"Wheat__Healthy": {
|
| 35 |
+
"crop": "Wheat",
|
| 36 |
+
"description": "Healthy wheat plant without any visible disease symptoms.",
|
| 37 |
+
"symptoms": [],
|
| 38 |
+
"treatment": [],
|
| 39 |
+
"prevention": [
|
| 40 |
+
"Maintain balanced soil fertility",
|
| 41 |
+
"Ensure proper irrigation",
|
| 42 |
+
"Use certified disease-free seeds"
|
| 43 |
+
]
|
| 44 |
+
},
|
| 45 |
+
"Wheat__Brown_Rust": {
|
| 46 |
+
"crop": "Wheat",
|
| 47 |
+
"description": "Brown rust is caused by Puccinia triticina, a major foliar disease of wheat.",
|
| 48 |
+
"symptoms": [
|
| 49 |
+
"Small, circular to oval brown pustules on leaves",
|
| 50 |
+
"Yellowing around lesions",
|
| 51 |
+
"Reduced grain yield and quality"
|
| 52 |
+
],
|
| 53 |
+
"treatment": [
|
| 54 |
+
"Apply fungicides such as Propiconazole or Tebuconazole",
|
| 55 |
+
"Early spray during initial infection"
|
| 56 |
+
],
|
| 57 |
+
"prevention": [
|
| 58 |
+
"Plant resistant wheat varieties",
|
| 59 |
+
"Destroy volunteer wheat plants (green bridge)",
|
| 60 |
+
"Rotate crops to break pathogen cycle"
|
| 61 |
+
]
|
| 62 |
+
},
|
| 63 |
+
|
| 64 |
+
"Rice__Healthy": {
|
| 65 |
+
"crop": "Rice",
|
| 66 |
+
"description": "Healthy rice plant with no disease or insect damage.",
|
| 67 |
+
"symptoms": [],
|
| 68 |
+
"treatment": [],
|
| 69 |
+
"prevention": [
|
| 70 |
+
"Use disease-free seeds",
|
| 71 |
+
"Ensure balanced fertilizer application",
|
| 72 |
+
"Maintain proper water management"
|
| 73 |
+
]
|
| 74 |
+
},
|
| 75 |
+
"Rice__Leaf_Blast": {
|
| 76 |
+
"crop": "Rice",
|
| 77 |
+
"description": "Leaf blast caused by Magnaporthe oryzae affects rice leaves.",
|
| 78 |
+
"symptoms": [
|
| 79 |
+
"Diamond-shaped lesions on leaves",
|
| 80 |
+
"Grayish centers with brown borders",
|
| 81 |
+
"Rapid drying of leaves in severe cases"
|
| 82 |
+
],
|
| 83 |
+
"treatment": [
|
| 84 |
+
"Apply fungicides such as Tricyclazole or Isoprothiolane",
|
| 85 |
+
"Remove infected plants to reduce spread"
|
| 86 |
+
],
|
| 87 |
+
"prevention": [
|
| 88 |
+
"Grow resistant rice varieties",
|
| 89 |
+
"Avoid excess nitrogen fertilizer",
|
| 90 |
+
"Ensure proper spacing to reduce humidity"
|
| 91 |
+
]
|
| 92 |
+
},
|
| 93 |
+
"Rice__Neck_Blast": {
|
| 94 |
+
"crop": "Rice",
|
| 95 |
+
"description": "Neck blast affects the panicle neck, caused by Magnaporthe oryzae.",
|
| 96 |
+
"symptoms": [
|
| 97 |
+
"Blackening and rotting at panicle neck",
|
| 98 |
+
"Panicles dry prematurely",
|
| 99 |
+
"Grain filling is reduced or absent"
|
| 100 |
+
],
|
| 101 |
+
"treatment": [
|
| 102 |
+
"Spray fungicides at heading stage (Tricyclazole, Carbendazim)",
|
| 103 |
+
"Destroy infected residues"
|
| 104 |
+
],
|
| 105 |
+
"prevention": [
|
| 106 |
+
"Cultivate resistant varieties",
|
| 107 |
+
"Balanced fertilizer use",
|
| 108 |
+
"Crop rotation with non-host crops"
|
| 109 |
+
]
|
| 110 |
+
},
|
| 111 |
+
|
| 112 |
+
"Potato__Healthy": {
|
| 113 |
+
"crop": "Potato",
|
| 114 |
+
"description": "Healthy potato leaf without disease or pest damage.",
|
| 115 |
+
"symptoms": [],
|
| 116 |
+
"treatment": [],
|
| 117 |
+
"prevention": [
|
| 118 |
+
"Plant certified seed potatoes",
|
| 119 |
+
"Follow crop rotation",
|
| 120 |
+
"Maintain field hygiene"
|
| 121 |
+
]
|
| 122 |
+
},
|
| 123 |
+
"Potato__Late_Blight": {
|
| 124 |
+
"crop": "Potato",
|
| 125 |
+
"description": "Late blight, caused by Phytophthora infestans, is a devastating potato disease.",
|
| 126 |
+
"symptoms": [
|
| 127 |
+
"Dark brown to black lesions on leaves with pale green halos",
|
| 128 |
+
"White fungal growth under moist conditions",
|
| 129 |
+
"Tuber rot in severe cases"
|
| 130 |
+
],
|
| 131 |
+
"treatment": [
|
| 132 |
+
"Apply fungicides such as Mancozeb or Metalaxyl",
|
| 133 |
+
"Remove and destroy infected plants"
|
| 134 |
+
],
|
| 135 |
+
"prevention": [
|
| 136 |
+
"Plant resistant potato varieties",
|
| 137 |
+
"Avoid overhead irrigation",
|
| 138 |
+
"Ensure proper crop rotation"
|
| 139 |
+
]
|
| 140 |
+
},
|
| 141 |
+
"Potato__Early_Blight": {
|
| 142 |
+
"crop": "Potato",
|
| 143 |
+
"description": "Early blight, caused by Alternaria solani, is a common fungal disease of potato.",
|
| 144 |
+
"symptoms": [
|
| 145 |
+
"Small dark brown spots with concentric rings on leaves",
|
| 146 |
+
"Premature leaf drop",
|
| 147 |
+
"Reduced tuber yield"
|
| 148 |
+
],
|
| 149 |
+
"treatment": [
|
| 150 |
+
"Apply fungicides like Chlorothalonil or Copper-based sprays",
|
| 151 |
+
"Remove infected leaves early"
|
| 152 |
+
],
|
| 153 |
+
"prevention": [
|
| 154 |
+
"Use resistant varieties",
|
| 155 |
+
"Practice crop rotation",
|
| 156 |
+
"Avoid excessive nitrogen fertilizer"
|
| 157 |
+
]
|
| 158 |
+
}
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
@app.on_event("startup")
|
| 163 |
+
def startup_event():
|
| 164 |
+
global learn
|
| 165 |
+
download_models()
|
| 166 |
+
learn = load_model_safely()
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
@app.get("/")
|
| 170 |
+
def read_root():
|
| 171 |
+
return {
|
| 172 |
+
"message": "Welcome to AgroAI - Crop Disease Detection API",
|
| 173 |
+
"status": "running",
|
| 174 |
+
"model_loaded": learn is not None
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
@app.post("/predict/")
|
| 179 |
+
async def predict(file: UploadFile = File(...)):
|
| 180 |
+
if learn is None:
|
| 181 |
+
raise HTTPException(
|
| 182 |
+
status_code=503,
|
| 183 |
+
detail="Model not available. Please check server configuration."
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
if not file.content_type or not file.content_type.startswith("image/"):
|
| 187 |
+
raise HTTPException(
|
| 188 |
+
status_code=400,
|
| 189 |
+
detail="File must be an image"
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
try:
|
| 193 |
+
# Read and process the image
|
| 194 |
+
image_data = await file.read()
|
| 195 |
+
img = read_imagefile(image_data)
|
| 196 |
+
|
| 197 |
+
# Make prediction
|
| 198 |
+
pred, pred_idx, probs = learn.predict(img)
|
| 199 |
+
pred_str = str(pred)
|
| 200 |
+
|
| 201 |
+
info = disease_info.get(pred_str, {
|
| 202 |
+
"crop": "",
|
| 203 |
+
"description": "",
|
| 204 |
+
"symptoms": [pred, pred_idx],
|
| 205 |
+
"treatment": [],
|
| 206 |
+
"prevention": []
|
| 207 |
+
})
|
| 208 |
+
0
|
| 209 |
+
return {
|
| 210 |
+
"prediction": pred_str,
|
| 211 |
+
"confidence": float(probs[pred_idx]),
|
| 212 |
+
"filename": file.filename,
|
| 213 |
+
**info
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
except Exception as e:
|
| 217 |
+
raise HTTPException(
|
| 218 |
+
status_code=500,
|
| 219 |
+
detail=f"Error processing image: {str(e)}"
|
| 220 |
+
)
|
crop_disease_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2d21c3703fe3cda9309ddcc59f504d7d60a3bc7e212058751eb0145891199e17
|
| 3 |
+
size 103106567
|
models_download.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import hf_hub_download
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
def download_models():
|
| 5 |
+
|
| 6 |
+
files = [
|
| 7 |
+
("Bhavesh003/crop-disease-model", "crop_disease_model.pkl")
|
| 8 |
+
]
|
| 9 |
+
|
| 10 |
+
for repo, fname in files:
|
| 11 |
+
if not os.path.exists(fname):
|
| 12 |
+
hf_hub_download(
|
| 13 |
+
repo_id=repo,
|
| 14 |
+
filename=fname,
|
| 15 |
+
local_dir=".",
|
| 16 |
+
force_filename=fname
|
| 17 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn[standard]
|
| 3 |
+
pydantic
|
| 4 |
+
joblib
|
| 5 |
+
scikit-learn
|
| 6 |
+
fastai
|
| 7 |
+
torch
|
| 8 |
+
pillow
|
| 9 |
+
huggingface-hub
|
| 10 |
+
python-multipart
|