Spaces:
Running
Running
| # 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:** | |
| ```json | |
| { | |
| "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:** | |
| ```json | |
| { | |
| "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) | |
| ```bash | |
| 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 | |
| ```json | |
| { | |
| "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 | |
| ```json | |
| { | |
| "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: | |
| > ```html | |
| > <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 | |
| ```json | |
| { | |
| "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 | |
| ```json | |
| { | |
| "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 | |
| ```json | |
| { | |
| "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 | |
| ```json | |
| { | |
| "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 | |
| ```json | |
| { | |
| "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 | |
| ```json | |
| { | |
| "detail": "Model 'eye' is not loaded. Check weight file." | |
| } | |
| ``` | |
| ### 400 โ Invalid Image | |
| ```json | |
| { | |
| "detail": "Could not read image file." | |
| } | |
| ``` | |
| --- | |
| ## ๐ง Frontend Integration Guide | |
| ### JavaScript / Fetch | |
| ```javascript | |
| 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) | |
| ```javascript | |
| 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) | |
| ```javascript | |
| // ุจุนุฏ ู ุง ุชุฌูุจ ุงูู 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 | |
| ```bash | |
| # 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* | |