๐ŸŸข v0.1.1 โ€” Live on PyPI

๐Ÿฅญ Mango Disease
AI โ€” Complete Docs

AI-powered Amropali mango disease detection. Classify 7 diseases, generate Grad-CAM heatmaps & PDF reports. Use as a Python library or REST API.

7
Disease Classes
94.7%
Top Accuracy
5
API Endpoints
0.1.1
Latest Version

Install in 30 Seconds

Install from PyPI โ€” the official Python package index. Works on Windows, Mac, Linux.

1

Open PowerShell / Terminal

Press Windows Key โ†’ type PowerShell โ†’ press Enter

2

Install the package

Copy and paste the command on the right โ†’ press Enter

3

Wait for installation

Takes ~30 seconds. You'll see: Successfully installed mango-disease-ai-0.1.1

4

Install API server tools

Run the second command to install FastAPI server tools

5

First run note โš ๏ธ

The CLIP model (~600 MB) downloads automatically once. Takes 1-2 min. All future runs are instant.

PowerShell
# Install the PyPI library
PS> pip install mango-disease-ai
โœ“ Successfully installed mango-disease-ai-0.1.1
# Install REST API server tools
PS> pip install fastapi "uvicorn[standard]" python-multipart
โœ“ All API dependencies installed
# Verify installation
PS> python -c "import mango_disease_ai; print(mango_disease_ai.__version__)"
0.1.1
โšก For Anaconda users (Windows)
PS> & "E:\Anaconda\python.exe" -m pip install mango-disease-ai

PyPI Library โ€” Python Usage

Import directly in your Python code. Just 2 functions: analyze() and generate_pdf()

Basic Usage
Full Output
PDF Report
Fast Mode
my_script.py
from mango_disease_ai import analyze
# Analyze any mango image
result = analyze("path/to/mango_leaf.jpg")
# Check if it's a mango
if result["is_mango"]:
    print(result["predicted_class"])
    print(result["confidence"])
๐Ÿ“ค Console Output
# First run (downloads CLIP ~600MB once)
Loading weights: 100%|โ–ˆโ–ˆโ–ˆโ–ˆ| 398/398
Healthy
0.9470
๐ŸŽฏ Returns disease name + confidence score (0.0 to 1.0)
full_analysis.py
from mango_disease_ai import analyze
result = analyze("mango.jpg")
if result["is_mango"]:
  # All 7 class scores
  for s in result["all_scores"]:
    print(s['class'], s['score'])
  # Disease info
  info = result["disease_info"]
  print(info["scientific_name"])
  print(info["symptoms"])
  print(info["remedies"])
๐Ÿ“Š Full Output Preview
๐Ÿฅญ Disease: Healthy (94.7%)
All 7 class scores:
Scientific name:
Mangifera indica (Normal)
Symptoms:
โ€ข Vibrant green leaf coloration
โ€ข Smooth and glossy leaf surface
โ€ข No spots or lesions
generate_report.py
from mango_disease_ai import analyze, generate_pdf
import base64
result = analyze("mango.jpg")
if result["is_mango"]:
  # Save heatmap PNG
  heatmap = base64.b64decode(result["gradcam_base64"])
  with open("heatmap.png", "wb") as f:
    f.write(heatmap)
  # Generate PDF
  pdf = generate_pdf(result, user_name="Dr. Rahman")
  with open("report.pdf", "wb") as f:
    f.write(pdf)
๐Ÿ“„ Output Files Generated
๐ŸŒก๏ธ heatmap.png
Grad-CAM++ overlay showing where the AI focused. Red = high attention areas. ~50-100 KB PNG at 224ร—224px.
๐Ÿ“‹ report.pdf
Professional PDF with your name, date, disease name, all scores, original + heatmap images, symptoms & treatment. ~180 KB.
fast_mode.py
from mango_disease_ai import analyze
# Fast mode โ€” skip heatmap generation
# ~2 seconds vs ~8 seconds with heatmap
result = analyze("mango.jpg", include_gradcam=False)
print(result["predicted_class"]) # works!
print(result["gradcam_base64"]) # None
โšก Speed Comparison
Fast mode (no heatmap)~2 sec
Full mode (with heatmap)~8 sec
๐Ÿ’ก Use fast mode when you only need the disease name and don't need the heatmap visualization

REST API โ€” HTTP Endpoints

Use the API from any language: Python, JavaScript, Java, Kotlin, Swift, PHP โ€” anything that can send HTTP requests.

Start the API Server
# Step 1: Go to the project folder
PS> cd "E:\AIUB R&D ICCA\Fully Final Mango api , without api\Mango API"
# Step 2: Start the server
PS> & "E:\Anaconda\python.exe" start_api.py
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8000
# Step 3: Open browser โ†’ http://localhost:8000/docs
๐ŸŒ Browser (Swagger)
๐Ÿ Python
โšก JavaScript
๐Ÿ”ง curl

Testing in the Browser (Swagger UI)

1

Open: http://localhost:8000/docs

You see all 5 endpoints listed with green (GET) and yellow (POST) badges

2

Click any endpoint to expand it

E.g. click POST /api/analyze โ€” it opens a full form

3

Click "Try it out"

This activates the form so you can fill it in

4

Upload your image + click "Execute"

Click "Choose File" โ†’ select your mango image โ†’ click the blue Execute button

5

See the live JSON response

Scroll down โ€” you see the full JSON with disease name, confidence, heatmap, everything

api_test.py
import requests
# โ”€โ”€ 1. Health Check โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
r = requests.get("http://localhost:8000/api/health")
print(r.json()) # {'status': 'ok', 'version': '0.1.1'}
# โ”€โ”€ 2. Analyze Image โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
with open("mango.jpg", "rb") as f:
    r = requests.post(
        "http://localhost:8000/api/analyze",
        files={"image": ("mango.jpg", f, "image/jpeg")},
        data={"include_gradcam": "false"},
    )
result = r.json()
print("Disease:", result["predicted_class"])
print("Confidence:", f"{result['confidence']:.1%}")
# โ”€โ”€ 3. Download PDF โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
with open("mango.jpg", "rb") as f:
    r = requests.post("http://localhost:8000/api/report",
        files={"image": f}, data={"user_name": "Dr. Rahman"})
open("report.pdf", "wb").write(r.content)
app.js
async function analyzeMango(imageFile) {
  const formData = new FormData();
  formData.append("image", imageFile);
  formData.append("include_gradcam", "false");
  const response = await fetch(
    "http://localhost:8000/api/analyze",
    { method: "POST", body: formData }
  );
  // Handle non-mango images (HTTP 422)
  if (response.status === 422) {
    alert("Not a mango! Please upload a mango image.");
    return;
  }
  const result = await response.json();
  console.log("Disease:", result.predicted_class);
  console.log("Confidence:", (result.confidence * 100).toFixed(1) + "%");
  // Display heatmap in <img> tag
  document.getElementById("heatmap").src =
    "data:image/png;base64," + result.gradcam_base64;
}
Terminal
# Health check
$ curl http://localhost:8000/api/health
{"status":"ok","version":"0.1.1"}
# Analyze image
$ curl -X POST http://localhost:8000/api/analyze \
  -F "image=@mango.jpg" \
  -F "include_gradcam=false"
# Download PDF
$ curl -X POST http://localhost:8000/api/report \
  -F "image=@mango.jpg" \
  -F "user_name=Dr. Rahman" \
  --output report.pdf

5 API Endpoints

Every endpoint, what it does, and exactly what it returns.

GET /api/health ~10ms
Check if the server is alive and running. Use this before sending image requests.
Response: {"status": "ok", "version": "0.1.1"}
GET /api/diseases ~5ms
Get full information about all 7 detectable diseases: scientific name, description, symptoms, and remedies.
{
  "diseases": {
    "Anthracnose": {
      "scientific_name": "Colletotrichum gloeosporioides",
      "symptoms": ["Dark brown spots", "..."],
      "remedies": ["Apply copper fungicide", "..."]
    },
    "Bacterial Canker": { ... },
    // ... all 7 diseases
  }
}
POST /api/analyze ~2-8 sec
Upload a mango image โ†’ get full disease diagnosis. Body: image (file) + include_gradcam (bool). Returns 422 if not a mango image.
{
  "is_mango": true,
  "mango_confidence": 0.82,
  "predicted_class": "Healthy",
  "confidence": 0.9470,
  "all_scores": [
    { "class": "Healthy", "score": 0.947 },
    { "class": "Anthracnose", "score": 0.012 }, ...
  ],
  "disease_info": { "scientific_name": "...", "symptoms": [...] },
  "gradcam_base64": "iVBORw0KGgoAAAANSUhEUg..." // PNG image
}
POST /api/report ~10-15 sec
Upload image + your name โ†’ download a professional PDF report directly. Body: image (file) + user_name (string).
Response: application/pdf binary file (~180 KB)
Header: Content-Disposition: attachment; filename="mango_diagnosis_Dr_Rahman.pdf"
GET /docs Swagger UI
Interactive Swagger UI โ€” test all endpoints directly in the browser. Auto-generated from the code.

Predicted Output Types

Here's exactly what the AI returns for every disease class.

โœ… HEALTHY LEAF
Healthy
Mangifera indica (Normal)
Confidence94.7%
โœ“ No treatment needed
โœ“ Continue regular monitoring
๐Ÿ”ด DISEASED LEAF
Anthracnose
Colletotrichum gloeosporioides
Confidence93.1%
โš  Dark brown spots on leaves
โ†’ Apply copper-based fungicide
โš ๏ธ NOT A MANGO
Rejected
HTTP 422 Error Response
Mango confidence13.4%
โ†’ Upload a clearer mango photo
โ†’ API returns HTTP 422

All 7 Disease Classes

System Architecture

The AI pipeline that runs every time you analyze a mango image.

๐Ÿ“ธ Input: Image (JPG/PNG/WebP/BMP)
Any mango leaf or fruit photo
โ†“
๐Ÿ” CLIP Validation
Checks: "Is this a mango?"
โŒ Not mango
โ†’ HTTP 422
โ†“ โœ… Is mango
๐Ÿง  AA-ENet Classification
EfficientNet-B0 + CBAM + Transformer
โ†“
๐Ÿ“Š Disease + Scores
7 class probabilities
๐ŸŒก๏ธ Grad-CAM++
Heatmap visualization
โ†“
โœ… Result JSON / PDF Report
Disease + Confidence + Heatmap + Info
๐Ÿง  AA-ENet Model
Custom EfficientNet-B0 backbone with CBAM attention and Transformer fusion. Trained on Amropali mango dataset.
EfficientNet-B0CBAMTransformer
๐Ÿ” CLIP Validation
OpenAI CLIP (ViT-B/32) validates that the uploaded image is actually a mango โ€” prevents false diagnoses on random photos.
openai/clip-vit-base-patch32Zero-shot
๐ŸŒก๏ธ Grad-CAM++ Heatmap
Shows exactly where the AI looked to make its decision. Red = high focus, Blue = ignored. Useful for research validation.
Grad-CAM++CBAM layer
๐Ÿ“„ PDF Report Engine
fpdf2-based report generator creates professional diagnosis reports with images, scores, symptoms & treatment.
fpdf2180KB PDF

File Structure

Mango API/
๐Ÿ“ Mango API/
  โ”œโ”€โ”€ run_mango.py โ† Test the PyPI library
  โ”œโ”€โ”€ start_api.py โ† One-click API launcher
  โ”œโ”€โ”€ test_api.py โ† Full 6-test suite
  โ”œโ”€โ”€ single_test.py โ† Test ONE endpoint
  โ”œโ”€โ”€ USAGE_GUIDE.md โ† Full documentation
  โ”œโ”€โ”€ Guide For install.txt โ† Quick start
  โ””โ”€โ”€ ๐Ÿ“ api/
      โ”œโ”€โ”€ main.py โ† FastAPI application
      โ”œโ”€โ”€ schemas.py โ† Pydantic models
      โ”œโ”€โ”€ __init__.py โ† Package init
      โ””โ”€โ”€ requirements.txt โ† API deps

Technology Stack

๐Ÿ”ฌ AI/ML
PyTorchtimmCLIPOpenCV
๐ŸŒ API
FastAPIUvicornPydantic
๐Ÿ“ฆ Package
PyPIsetuptoolstwine
๐Ÿ“„ Report
fpdf2PillowNumPy

All Commands

Quick Reference โ€” Copy Any Command
# โ”€โ”€ Install โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
PS> & "E:\Anaconda\python.exe" -m pip install mango-disease-ai
PS> & "E:\Anaconda\python.exe" -m pip install fastapi "uvicorn[standard]" python-multipart
# โ”€โ”€ Navigate to project โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
PS> cd "E:\AIUB R&D ICCA\Fully Final Mango api , without api\Mango API"
# โ”€โ”€ Test PyPI library โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
PS> & "E:\Anaconda\python.exe" run_mango.py
# โ”€โ”€ Start REST API server โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
PS> & "E:\Anaconda\python.exe" start_api.py
# โ”€โ”€ Run ALL API tests (6 tests) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
PS> & "E:\Anaconda\python.exe" test_api.py
# โ”€โ”€ Test ONE specific endpoint โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
PS> & "E:\Anaconda\python.exe" single_test.py health
PS> & "E:\Anaconda\python.exe" single_test.py analyze
PS> & "E:\Anaconda\python.exe" single_test.py report
# โ”€โ”€ Open Swagger UI in browser โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
PS> start http://localhost:8000/docs
# โ”€โ”€ Check installed version โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
PS> & "E:\Anaconda\python.exe" -c "import mango_disease_ai; print(mango_disease_ai.__version__)"