medical-ai-api / DOCUMENTATION.md
morefaat69's picture
Update DOCUMENTATION.md
3666996 verified
|
Raw
History Blame Contribute Delete
13.3 kB

Medical AI API โ€” Technical Documentation

Version: 1.0.0
Base URL: https://morefaat69-medical-ai-api.hf.space
Swagger UI: https://morefaat69-medical-ai-api.hf.space/docs


Overview

The Medical AI API is a unified REST API that wraps 7 Deep Learning models for medical image diagnosis. Each model accepts an image file and returns a prediction with confidence score and Arabic medical recommendations.

All endpoints use multipart/form-data and accept the following optional patient fields:

Field Type Description
file File Medical image (required)
age Integer Patient age (optional)
gender String male or female (optional)
symptoms String Patient symptoms in Arabic or English (optional)

When age, gender, and symptoms are provided, the API generates a personalized Arabic medical report via Gemini AI. Otherwise, it falls back to a predefined report.


๐Ÿ” General Endpoints

GET /

Returns API info and list of loaded models.

Response:

{
  "name": "Medical AI API",
  "version": "1.0.0",
  "loaded_models": ["skin", "breast_seg", "breast_cls", "eye", "brain", "heart", "lung", "kidney"]
}

GET /health

Health check endpoint.

Response:

{
  "status": "ok"
}

๐Ÿฉบ 1. Skin Cancer โ€” /predict/skin

Method: POST
Content-Type: multipart/form-data
Image: Dermatoscopy image (RGB)
Input Size: 28ร—28 (auto-resized)
Model: Custom CNN โ€” TensorFlow
Dataset: 25,000 images โ€” ISIC Dataset
Accuracy: 96.2%

Classes

Class Full Name
AK Actinic Keratosis
BCC Basal Cell Carcinoma
BKL Benign Keratosis-like Lesions
DF Dermatofibroma
MEL Melanoma
NV Melanocytic Nevi
SCC Squamous Cell Carcinoma
VASC Vascular Lesions

Request Example (Postman / curl)

curl -X POST https://morefaat69-medical-ai-api.hf.space/predict/skin \
  -F "file=@skin.jpg" \
  -F "age=45" \
  -F "gender=male" \
  -F "symptoms=ุชุบูŠุฑ ููŠ ู„ูˆู† ุงู„ุฌู„ุฏ ูˆุญูƒุฉ"

Response Example

{
  "model": "skin",
  "predicted_class": "MEL",
  "predicted_name": "Melanoma",
  "confidence": 94.32,
  "severity": "high",
  "all_probabilities": {
    "AK": 0.5,
    "BCC": 1.2,
    "BKL": 0.8,
    "DF": 0.3,
    "MEL": 94.32,
    "NV": 2.1,
    "SCC": 0.6,
    "VASC": 0.18
  },
  "disease_info": {
    "disease_name": "Melanoma โ€” ูˆุฑู… ุงู„ุฎู„ุงูŠุง ุงู„ู…ูŠู„ุงู†ูŠู†ูŠุฉ",
    "description": "...",
    "recommendations": ["...", "..."],
    "emergency_signs": ["...", "..."],
    "prevention_tips": ["...", "..."]
  }
}

๐Ÿซ€ 2. Breast Cancer โ€” /predict/breast

Method: POST
Content-Type: multipart/form-data
Image: Ultrasound image (RGB)
Model: U-Net (Segmentation) + MobileNetV2 (Classification) โ€” TensorFlow
Dataset: 15,000 images โ€” BUSI Dataset
Accuracy: 96.7%

Classes

Class Description
benign ุญู…ูŠุฏ
malignant ุฎุจูŠุซ
normal ุทุจูŠุนูŠ

Response Example

{
  "model": "breast",
  "predicted_class": "malignant",
  "confidence": 91.5,
  "all_probabilities": {
    "benign": 5.2,
    "malignant": 91.5,
    "normal": 3.3
  },
  "segmentation": {
    "mask_mean_activation": 0.6231,
    "lesion_coverage_percent": 18.4,
    "mask_image_base64": "iVBORw0KGgo...",
    "overlay_image_base64": "iVBORw0KGgo..."
  },
  "disease_info": {
    "disease_name": "Malignant โ€” ูˆุฑู… ุฎุจูŠุซ",
    "description": "...",
    "recommendations": ["...", "..."]
  }
}

Note: mask_image_base64 and overlay_image_base64 are PNG images encoded in Base64. To display them in the frontend:

<img src="data:image/png;base64,{mask_image_base64}" />

๐Ÿ‘๏ธ 3. Eye Diseases โ€” /predict/eye

Method: POST
Content-Type: multipart/form-data
Image: Fundus image (RGB)
Input Size: 224ร—224 (auto-resized)
Model: EfficientNetB3 + Dense layers โ€” TensorFlow
Dataset: 15,000 images
Accuracy: 97.8%

Classes

Class Description
Cataract ุงู„ู…ุงุก ุงู„ุฃุจูŠุถ
Diabetic Retinopathy ุงุนุชู„ุงู„ ุงู„ุดุจูƒูŠุฉ ุงู„ุณูƒุฑูŠ
Glaucoma ุงู„ุฌู„ูˆูƒูˆู…ุง
Normal ุทุจูŠุนูŠ

Response Example

{
  "model": "eye",
  "predicted_class": "Glaucoma",
  "confidence": 88.7,
  "all_probabilities": {
    "Cataract": 3.1,
    "Diabetic Retinopathy": 5.4,
    "Glaucoma": 88.7,
    "Normal": 2.8
  },
  "disease_info": {
    "disease_name": "Glaucoma โ€” ุงู„ุฌู„ูˆูƒูˆู…ุง",
    "description": "...",
    "recommendations": ["...", "..."]
  }
}

๐Ÿง  4. Brain Tumor โ€” /predict/brain

Method: POST
Content-Type: multipart/form-data
Image: MRI image (RGB)
Input Size: 224ร—224 (auto-resized)
Model: VGG-like CNN โ€” TensorFlow
Dataset: 7,000 images
Accuracy: 98.1%

Classes

Class Description
glioma ูˆุฑู… ุงู„ุบู„ูŠูˆู…ุง
meningioma ูˆุฑู… ุงู„ุณุญุงูŠุง
notumor ู„ุง ูŠูˆุฌุฏ ูˆุฑู…
pituitary ูˆุฑู… ุงู„ุบุฏุฉ ุงู„ู†ุฎุงู…ูŠุฉ

Response Example

{
  "model": "brain",
  "predicted_class": "glioma",
  "confidence": 95.1,
  "all_probabilities": {
    "glioma": 95.1,
    "meningioma": 2.3,
    "notumor": 1.8,
    "pituitary": 0.8
  },
  "disease_info": {
    "disease_name": "Glioma โ€” ูˆุฑู… ุงู„ุบู„ูŠูˆู…ุง",
    "description": "...",
    "recommendations": ["...", "..."]
  }
}

โค๏ธ 5. Heart Segmentation โ€” /predict/heart

Method: POST
Content-Type: multipart/form-data
Image: Echocardiography image (Grayscale)
Input Size: 128ร—128 (auto-resized)
Model: U-Net โ€” TensorFlow
Dataset: 13,000 images
Accuracy: 97.3%

Assessments

Assessment Description
normal ุญุฌู… ุทุจูŠุนูŠ
slightly_large ูƒุจูŠุฑ ู‚ู„ูŠู„ุงู‹
abnormally_large ูƒุจูŠุฑ ุจุดูƒู„ ุบูŠุฑ ุทุจูŠุนูŠ
not_detected ู„ู… ูŠุชู… ุงูƒุชุดุงู ุงู„ู‚ู„ุจ

Response Example

{
  "model": "heart",
  "task": "segmentation",
  "heart_area_ratio_percent": 22.5,
  "assessment": "slightly_large",
  "mask_stats": {
    "mean_activation": 0.4821,
    "max_activation": 0.9931,
    "detected_pixels": 3686
  },
  "segmentation": {
    "mask_image_base64": "iVBORw0KGgo...",
    "overlay_image_base64": "iVBORw0KGgo..."
  },
  "disease_info": {
    "disease_name": "Slightly Large Heart",
    "description": "...",
    "recommendations": ["...", "..."]
  }
}

๐Ÿซ 6. Chest X-Ray โ€” /predict/lung

Method: POST
Content-Type: multipart/form-data
Image: X-Ray image (RGB)
Input Size: 224ร—224 (auto-resized)
Model: Hybrid ResNet-152 + EfficientNetB5 + Attention โ€” PyTorch
Dataset: 22,000 images
Accuracy: 96.9%

Classes

Class Description
Covid-19 ูƒูˆููŠุฏ-19
Pneumonia-Viral ุงู„ุชู‡ุงุจ ุฑุฆูˆูŠ ููŠุฑูˆุณูŠ
Pneumonia-Bacterial ุงู„ุชู‡ุงุจ ุฑุฆูˆูŠ ุจูƒุชูŠุฑูŠ
Normal ุทุจูŠุนูŠ
Emphysema ุงู†ุชูุงุฎ ุงู„ุฑุฆุฉ
Tuberculosis ุงู„ุณู„ ุงู„ุฑุฆูˆูŠ

Response Example

{
  "model": "lung",
  "predicted_class": "Covid-19",
  "confidence": 87.3,
  "all_probabilities": {
    "Covid-19": 87.3,
    "Emphysema": 4.1,
    "Normal": 3.2,
    "Pneumonia-Bacterial": 2.9,
    "Pneumonia-Viral": 1.8,
    "Tuberculosis": 0.7
  },
  "disease_info": {
    "disease_name": "Covid-19 โ€” ูƒูˆููŠุฏ-19",
    "description": "...",
    "recommendations": ["...", "..."]
  }
}

๐Ÿซ˜ 7. Kidney Disease โ€” /predict/kidney

Method: POST
Content-Type: multipart/form-data
Image: CT Scan image (Grayscale)
Input Size: 200ร—200 (auto-resized)
Model: Custom CNN โ€” TensorFlow
Dataset: 8,000 images
Accuracy: 97.5%

Classes

Class Description
Cyst ูƒูŠุณ ุงู„ูƒู„ู‰
Normal ุทุจูŠุนูŠ
Stone ุญุตูˆุฉ ุงู„ูƒู„ู‰
Tumor ูˆุฑู… ุงู„ูƒู„ู‰

Response Example

{
  "model": "kidney",
  "predicted_class": "Stone",
  "confidence": 92.8,
  "all_probabilities": {
    "Cyst": 2.1,
    "Normal": 3.4,
    "Stone": 92.8,
    "Tumor": 1.7
  },
  "disease_info": {
    "disease_name": "Stone โ€” ุญุตูˆุฉ ุงู„ูƒู„ู‰",
    "description": "...",
    "recommendations": ["...", "..."]
  }
}

โš ๏ธ Error Responses

503 โ€” Model Not Loaded

{
  "detail": "Model 'eye' is not loaded. Check weight file."
}

400 โ€” Invalid Image

{
  "detail": "Could not read image file."
}

๐Ÿ”ง Frontend Integration Guide

JavaScript / Fetch

const formData = new FormData();
formData.append("file", imageFile);         // required
formData.append("age", 35);                 // optional
formData.append("gender", "male");          // optional
formData.append("symptoms", "ุญูƒุฉ ูˆุฃู„ู…");   // optional

const response = await fetch(
  "https://morefaat69-medical-ai-api.hf.space/predict/skin",
  {
    method: "POST",
    body: formData,
  }
);

const result = await response.json();
console.log(result.predicted_class);        // e.g. "MEL"
console.log(result.confidence);             // e.g. 94.32
console.log(result.disease_info);           // Arabic report

Axios (React / Vue)

import axios from "axios";

const BASE_URL = "https://morefaat69-medical-ai-api.hf.space";

export const predictDisease = async (model, imageFile, patientData = {}) => {
  const formData = new FormData();
  formData.append("file", imageFile);

  if (patientData.age)      formData.append("age", patientData.age);
  if (patientData.gender)   formData.append("gender", patientData.gender);
  if (patientData.symptoms) formData.append("symptoms", patientData.symptoms);

  const { data } = await axios.post(`${BASE_URL}/predict/${model}`, formData, {
    headers: { "Content-Type": "multipart/form-data" },
  });

  return data;
};

// Usage
const result = await predictDisease("brain", mriFile, {
  age: 42,
  gender: "female",
  symptoms: "ุตุฏุงุน ุดุฏูŠุฏ ูˆุฏูˆุฎุฉ",
});

Display Segmentation Images (Breast & Heart)

// ุจุนุฏ ู…ุง ุชุฌูŠุจ ุงู„ู€ response
const { segmentation } = result;

// ุนุฑุถ ุงู„ู€ mask
maskImg.src = `data:image/png;base64,${segmentation.mask_image_base64}`;

// ุนุฑุถ ุงู„ู€ overlay
overlayImg.src = `data:image/png;base64,${segmentation.overlay_image_base64}`;

Architecture Overview

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚              FastAPI Server             โ”‚   
โ”‚                 main.py                 โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  /skin   โ”‚  /eye    โ”‚  /brain  โ”‚  /kidneyโ”‚
โ”‚  /breast โ”‚  /heart  โ”‚  /lung   โ”‚          โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚           Model Loading (startup)            โ”‚
โ”‚   HuggingFace Hub โ†’ weights/ folder          โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚         TensorFlow (6 models)                โ”‚
โ”‚         PyTorch   (1 model โ€” lung)           โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚         Gemini AI (Arabic Reports)           โ”‚
โ”‚         Fallback: Hardcoded Dictionary       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿš€ Local Development

# 1. Clone the repo
git clone https://github.com/Refaat62/medical-project
cd medical-project

# 2. Install dependencies
pip install -r requirements.txt

# 3. Set Gemini API Key
set GEMINI_API_KEY=AIza...          # Windows CMD
$env:GEMINI_API_KEY="AIza..."       # PowerShell

# 4. Run the server
uvicorn main:app --reload --host 0.0.0.0 --port 8000

# 5. Open Swagger UI
# http://localhost:8000/docs

๐Ÿ“ฆ Model Weights

All weights are hosted on HuggingFace and downloaded automatically at startup:

File Size Model
skin_model.h5 5.3 MB Skin CNN
Final Final Breast Cancer Segmentation.h5 26.3 MB Breast U-Net
bes__model.h5 24.4 MB Breast Classifier
eye_model_fixed.h5 135 MB Eye EfficientNetB3
brain_model.h5 254 MB Brain CNN
heart_segmentation_model.h5 23.5 MB Heart U-Net
final_ChestX6_hybrid_model.pth 368 MB Lung Hybrid PyTorch
kidney_model1.h5 4.33 MB Kidney CNN

Total: ~841 MB


Built with โค๏ธ โ€” Ahmed Refaat