Spaces:
Running
title: Crop Guard
emoji: πΎ
colorFrom: green
colorTo: green
sdk: docker
sdk_version: '1.0'
app_file: main.py
pinned: false
Crop Disease Detection API Server
A FastAPI server hosting multiple Xceptionβbased deep learning models for detecting diseases in maize, cassava, cashew, and tomato leaves. Send images via HTTP and receive back the predicted disease, confidence score, and recommended treatment.
π Features
- Onβdemand model loading: Each model is loaded the first time itβs requested and then cached.
- Unified image pipeline: Shared resizing, normalization, and inference logic.
- Interactive docs: Builtβin Swagger UI at
/docs
. - CORS enabled: Accepts requests from any origin (customize as needed).
π Quick Start
1. Clone & Prepare
git clone https://github.com/Bawah-Joy/Ghana-Hack-AI.git
cd Ghana-Hack-AI/crop-guard-backend
python -m venv venv
source venv/bin/activate # macOS/Linux
venv\Scripts\activate # Windows
pip install --upgrade pip
pip install -r requirements.txt
2. Download & Place Models
Create a model/
directory in the project root and download the pre-trained .keras
files (examples below) into it:
model/
ββ xception_maize.keras
ββ xception_cassava.keras
ββ xception_cashew.keras
ββ xception_tomato.keras
Download links (example Google Drive):
3. Run Locally
Start the API
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
(Optional) Ngrok Tunneling
ngrok http 8000
Copy the HTTPS URL (e.g. https://abcd1234.ngrok.io
) and use it in your frontend or API clients.
βοΈ Configuration
No secrets required by default.
CORS is enabled for all origins in
app/main.py
via:app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"], )
π API Reference
Interactive Docs
Browse and test all endpoints at:
http://localhost:8000/docs
/predict
Method:
POST
Content-Type:
multipart/form-data
Fields:
file
(binary image file, e.g. JPEG/PNG)model_name
(string; one ofxception_maize
,xception_cassava
,xception_cashew
,xception_tomato
)
Example curl
curl -X POST "https://<YOUR_NGROK>.ngrok.io/predict" \
-F "file=@/path/to/leaf.jpg;type=image/jpeg" \
-F "model_name=xception_maize"
Sample Response
{
"label": "leaf blight",
"confidence": 0.87,
"details": {
"description": "Fungal disease that causes dead streaks on leaves.",
"symptoms": ["Long, greyish lesions", "Yellowing and dying leaves"],
"treatment": "Apply fungicides like Mancozeb. Ensure good air circulation.",
"prevention": "Avoid overhead watering; plant in well-spaced rows.",
"message": "Maize leaf blight detected. Spray fungicide and avoid wetting leaves during irrigation."
}
}
π Model Loading & Inference Flow
All contained in app/api/predict.py
:
Load model on first request
def load_model(name): path = MODEL_DIR / f"{name}.keras" return keras_load_model(path)
Preprocess & predict
def predict_image(file_bytes, model, model_name): img = Image.open(BytesIO(file_bytes)).convert("RGB") img = img.resize((299, 299)) arr = preprocess_input(np.expand_dims(np.array(img), 0)) preds = model.predict(arr) idx = int(np.argmax(preds)) label = CLASS_NAMES[model_name][idx] conf = float(np.max(preds)) return label, conf, DISEASE_DATA[model_name][label]
Return JSON with
label
,confidence
, and richdetails
.
βοΈ Deployment on Render
Ensure
render.yml
is present in the project root.Push to GitHub and connect the repo in Render.
Render will:
Install dependencies:
pip install -r requirements.txt
Launch the app using:
uvicorn app.main:app --host 0.0.0.0 --port $PORT
ποΈ Project Structure
crop-guard-backend/
ββ app/
β ββ api/
β β ββ predict.py # Routes & inference logic
β ββ core/
β β ββ model.py # Model manager & loader
β ββ pipelines/
β β ββ main_pipeline.py # Shared preprocessing flow
β ββ schemas/
β β ββ predict.py # Request/response Pydantic models
β ββ main.py # FastAPI app setup + CORS
ββ model/ # Place downloaded .keras files here
ββ requirements.txt # Python dependencies
ββ render.yml # Render deployment config
ββ README.md # This file