# milan-tj/disease-prediction-system architecture

> Architecture diagram of the GitHub repository milan-tj/disease-prediction-system, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/milan-tj/disease-prediction-system
- Repository: https://github.com/milan-tj/disease-prediction-system

## Overview

#### Purpose and Entry Points
This repository is a Flask-deployed disease prediction system. Users select symptoms, receive an ML disease prediction with ranked probabilities, disease context/precautions, and an optional Gemini-generated diet and lifestyle recommendation.

Primary runtime entry point: `app/app.py`. Core inference and GenAI integration are split between `app/predictor.py` and reusable `models/pipeline.py`.

#### Web Request and Presentation Layer
The Flask application serves a symptom-selection form and renders prediction results. It is the boundary between browser form data and model-ready symptom inputs.

- `app/app.py`
- `app/templates/home.html`
- `app/templates/result.html`

The UI submits one or more symptom identifiers; the result view displays selected symptoms, predicted disease, confidence/top predictions, description, precautions, and generated recommendations. Styling is isolated in `app/static/style.css`.

#### Inference Feature Contract
Inference depends on a fixed ordered set of 132 binary symptom features. Selected symptom names are converted to a zero-filled vector with enabled positions set to `1`; ordering must exactly match training-time feature order.

- `models/symptom_cols.json`
- `app/predictor.py`
- `models/pipeline.py`

This feature-schema artifact is a critical compatibility boundary: changing symptom names or ordering requires retraining and regenerating serialized model artifacts.

#### Model Serving and Ranking
The production model is serialized Bernoulli Naive Bayes, selected for small size and fast inference. The serving pipeline loads artifacts once at startup, invokes prediction and probability APIs, then maps class IDs to disease names and returns the highest-confidence result plus top-three candidates.

- `models/best_model.pkl`
- `models/bernoulli_nb.pkl`
- `models/disease_label_mapping.csv`

Alternative trained classifiers exist for evaluation or future model selection: `models/random_forest.pkl`, `models/svm_rbf.pkl`, `models/xgboost.pkl`, and `models/decision_tree.pkl`.

#### Disease Knowledge Enrichment
Prediction output is enriched using tabular reference data rather than generated solely from the classifier. Disease descriptions and precaution records are joined by disease name; severity is derived from selected symptoms and severity weights.

- `data/symptom_Description.csv`
- `data/symptom_precaution.csv`
- `data/Symptom-severity.csv`

These data files form the deterministic health-information layer feeding both UI results and the GenAI prompt.

#### Gemini Recommendation Boundary
The recommendation stage builds a structured prompt from predicted disease, selected symptoms, descriptions, precautions, and computed severity. It calls Google Gemini through the `google-genai` SDK, using `GEMINI_API_KEY` from the environment. Missing credentials, quota exhaustion, or API failure should resolve through the pipeline’s structured mock/fallback response rather than blocking core prediction.

- `app/predictor.py`
- `models/pipeline.py`

External dependency: Google Gemini (documented as Gemini Flash / configured model variant). This is the sole network-dependent stage; ML prediction and reference-data enrichment remain local.

#### Training Artifacts and Offline Lifecycle
The deployed artifacts originate from cleaned symptom datasets and model-development notebooks. Training, validation, deduplication analysis, and comparative model evaluation are offline concerns; deployment consumes only their persisted outputs.

- `data/Training_cleaned.csv`
- `data/Testing_cleaned.csv`
- `notebook/Ticket2_Model_Development.ipynb`

Operational runtime is Python with Flask, pandas, scikit-learn, joblib, and `google-genai`; dependencies are declared in `requirements.txt`.

Main entry points: [`models/pipeline.py`](https://github.com/milan-tj/disease-prediction-system/blob/main/models/pipeline.py) (Inference pipeline), [`app/app.py`](https://github.com/milan-tj/disease-prediction-system/blob/main/app/app.py) (Flask application), [`app/predictor.py`](https://github.com/milan-tj/disease-prediction-system/blob/main/app/predictor.py) (Prediction adapter), [`notebook/Ticket2_Model_Development.ipynb`](https://github.com/milan-tj/disease-prediction-system/blob/main/notebook/Ticket2_Model_Development.ipynb) (Model development)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["Web interface"]
  node_browser["Symptom-selection browser<br/>user interface"]
  node_home_template["Symptom form<br/>Flask template<br/>[home.html]"]
  node_result_template["Prediction results<br/>Flask template<br/>[result.html]"]
  node_flask_app{{"Flask application<br/>web entry point<br/>[app.py]"}}
end

subgraph group_serving["Local serving pipeline"]
  node_predictor["Prediction adapter<br/>application service<br/>[predictor.py]"]
  node_pipeline["Inference pipeline<br/>model serving pipeline<br/>[pipeline.py]"]
  node_feature_schema["132-symptom feature order<br/>feature schema<br/>[symptom_cols.json]"]
  node_production_model[("Bernoulli NB model<br/>serialized classifier<br/>[best_model.pkl]")]
  node_label_mapping["Disease label mapping<br/>class mapping"]
end

subgraph group_knowledge["Health knowledge"]
  node_knowledge_data[("Disease descriptions &amp; precautions<br/>reference data")]
  node_severity_data[("Symptom severity weights<br/>reference data")]
end

subgraph group_offline["Offline model lifecycle"]
  node_training_data[("Cleaned train/test data<br/>training datasets")]
  node_model_notebook["Model development<br/>training notebook"]
  node_candidate_models[("Alternative classifiers<br/>evaluation artifacts<br/>[random_forest.pkl]")]
end

node_gemini{{"Google Gemini<br/>external GenAI API"}}

node_browser -->|"submits symptoms"| node_flask_app
node_flask_app -->|"renders form"| node_home_template
node_flask_app -->|"prediction request"| node_predictor
node_predictor -->|"selected symptoms"| node_pipeline
node_pipeline -->|"vectorizes in fixed order"| node_feature_schema
node_pipeline -->|"predicts probabilities"| node_production_model
node_pipeline -->|"maps class to disease"| node_label_mapping
node_pipeline -->|"joins description and precautions"| node_knowledge_data
node_pipeline -->|"computes severity"| node_severity_data
node_pipeline -->|"optional structured prompt"| node_gemini
node_predictor -->|"enriched result"| node_flask_app
node_flask_app -->|"renders result"| node_result_template
node_training_data -->|"offline training and validation"| node_model_notebook
node_model_notebook -->|"selected deployment artifact"| node_production_model
node_model_notebook -->|"comparative artifacts"| node_candidate_models
node_feature_schema -.->|"training-serving contract"| node_production_model

click node_home_template "https://github.com/milan-tj/disease-prediction-system/blob/main/app/templates/home.html"
click node_result_template "https://github.com/milan-tj/disease-prediction-system/blob/main/app/templates/result.html"
click node_flask_app "https://github.com/milan-tj/disease-prediction-system/blob/main/app/app.py"
click node_predictor "https://github.com/milan-tj/disease-prediction-system/blob/main/app/predictor.py"
click node_pipeline "https://github.com/milan-tj/disease-prediction-system/blob/main/models/pipeline.py"
click node_feature_schema "https://github.com/milan-tj/disease-prediction-system/blob/main/models/symptom_cols.json"
click node_production_model "https://github.com/milan-tj/disease-prediction-system/blob/main/models/best_model.pkl"
click node_label_mapping "https://github.com/milan-tj/disease-prediction-system/blob/main/models/disease_label_mapping.csv"
click node_knowledge_data "https://github.com/milan-tj/disease-prediction-system/blob/main/data/symptom_Description.csv"
click node_severity_data "https://github.com/milan-tj/disease-prediction-system/blob/main/data/Symptom-severity.csv"
click node_training_data "https://github.com/milan-tj/disease-prediction-system/blob/main/data/Training_cleaned.csv"
click node_model_notebook "https://github.com/milan-tj/disease-prediction-system/blob/main/notebook/Ticket2_Model_Development.ipynb"
click node_candidate_models "https://github.com/milan-tj/disease-prediction-system/blob/main/models/random_forest.pkl"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_browser,node_home_template,node_result_template,node_flask_app toneBlue
class node_predictor,node_pipeline,node_feature_schema,node_production_model,node_label_mapping toneAmber
class node_knowledge_data,node_severity_data toneMint
class node_training_data,node_model_notebook,node_candidate_models toneRose
class node_gemini toneNeutral
```

## Components

### Web interface

- **Symptom-selection browser**: user interface
- [Symptom form](https://github.com/milan-tj/disease-prediction-system/blob/main/app/templates/home.html): Flask template · `app/templates/home.html`
- [Prediction results](https://github.com/milan-tj/disease-prediction-system/blob/main/app/templates/result.html): Flask template · `app/templates/result.html`
- [Flask application](https://github.com/milan-tj/disease-prediction-system/blob/main/app/app.py): web entry point · Translates form selections into prediction requests and renders responses. · `app/app.py`

### Local serving pipeline

- [Prediction adapter](https://github.com/milan-tj/disease-prediction-system/blob/main/app/predictor.py): application service · `app/predictor.py`
- [Inference pipeline](https://github.com/milan-tj/disease-prediction-system/blob/main/models/pipeline.py): model serving pipeline · Loads artifacts once and coordinates inference, enrichment, and recommendation fallback. · `models/pipeline.py`
- [132-symptom feature order](https://github.com/milan-tj/disease-prediction-system/blob/main/models/symptom_cols.json): feature schema · Ordered binary-feature compatibility contract. · `models/symptom_cols.json`
- [Bernoulli NB model](https://github.com/milan-tj/disease-prediction-system/blob/main/models/best_model.pkl): serialized classifier · `models/best_model.pkl`
- [Disease label mapping](https://github.com/milan-tj/disease-prediction-system/blob/main/models/disease_label_mapping.csv): class mapping · `models/disease_label_mapping.csv`

### Health knowledge

- [Disease descriptions & precautions](https://github.com/milan-tj/disease-prediction-system/blob/main/data/symptom_Description.csv): reference data · Deterministic disease context joined by disease name. · `data/symptom_Description.csv`
- [Symptom severity weights](https://github.com/milan-tj/disease-prediction-system/blob/main/data/Symptom-severity.csv): reference data · `data/Symptom-severity.csv`

### Offline model lifecycle

- [Cleaned train/test data](https://github.com/milan-tj/disease-prediction-system/blob/main/data/Training_cleaned.csv): training datasets · `data/Training_cleaned.csv`
- [Model development](https://github.com/milan-tj/disease-prediction-system/blob/main/notebook/Ticket2_Model_Development.ipynb): training notebook · Trains and compares candidate classifiers offline. · `notebook/Ticket2_Model_Development.ipynb`
- [Alternative classifiers](https://github.com/milan-tj/disease-prediction-system/blob/main/models/random_forest.pkl): evaluation artifacts · Random forest, SVM, XGBoost, and decision tree artifacts. · `models/random_forest.pkl`

### Other components

- **Google Gemini**: external GenAI API · Optional network recommendation provider.

## Connections

- Symptom-selection browser → Flask application: submits symptoms
- Flask application → Symptom form: renders form
- Flask application → Prediction adapter: prediction request
- Prediction adapter → Inference pipeline: selected symptoms
- Inference pipeline → 132-symptom feature order: vectorizes in fixed order
- Inference pipeline → Bernoulli NB model: predicts probabilities
- Inference pipeline → Disease label mapping: maps class to disease
- Inference pipeline → Disease descriptions & precautions: joins description and precautions
- Inference pipeline → Symptom severity weights: computes severity
- Inference pipeline → Google Gemini: optional structured prompt · Uses environment API key; failures return a local fallback.
- Prediction adapter → Flask application: enriched result
- Flask application → Prediction results: renders result
- Cleaned train/test data → Model development: offline training and validation
- Model development → Bernoulli NB model: selected deployment artifact
- Model development → Alternative classifiers: comparative artifacts
- 132-symptom feature order → Bernoulli NB model: training-serving contract
