# mahamudul-hasan-cse/mental-health-prediction-mlapp architecture

> Architecture diagram of the GitHub repository mahamudul-hasan-cse/mental-health-prediction-mlapp, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mahamudul-hasan-cse/mental-health-prediction-mlapp
- Repository: https://github.com/mahamudul-hasan-cse/mental-health-prediction-mlapp

## Overview

1. This repository is a small Python ML application with two clear phases: offline model training and online prediction. The runtime app is a Streamlit-based UI that loads a pre-trained classifier from disk and returns a mental-health risk category.

2. The main entry points are:
   - `train_model.py` for building the model artifact
   - `app.py` for the Streamlit user interface
   - `mental_health.py` for prediction/helper logic used by the app
   The notebook `mental_health.ipynb` appears to be exploratory/experimental rather than part of production flow.

3. The core data source is `mental_health_dataset.csv`. It contains structured tabular features such as age, gender, employment status, work environment, stress, sleep, anxiety, depression, support, and productivity, with the target label `mental_health_risk` taking values like Low/Medium/High.

4. Training is an isolated pipeline in `train_model.py`. It reads the CSV, preprocesses mixed numeric and categorical inputs, applies encoding/scaling where needed, trains a classifier, and serializes the resulting model to `model.pkl`. The README also mentions an optional `scaler.pkl`, indicating preprocessing artifacts may be persisted separately if the implementation uses them.

5. The architecture boundary is standard ML separation:
   - offline: dataset → preprocessing → model training → saved artifact
   - online: user inputs → same preprocessing assumptions → loaded model → prediction
   This means the app depends on the exact training-time feature order and preprocessing behavior.

6. `mental_health.py` is the inference boundary. It likely encapsulates the logic to transform incoming form values into the model’s expected input format and invoke the loaded model. This keeps prediction details out of the UI layer and makes the app simpler to maintain.

7. `app.py` is the presentation layer built with Streamlit. It renders form controls such as sliders and input fields, collects a small set of user-provided health indicators, and calls the prediction helper. The response is shown immediately in the browser after the user clicks Predict.

8. The model itself is a supervised classification model. The README specifies `RandomForestClassifier` as the default, with `XGBoostClassifier` as an alternative. `StandardScaler` and label encoding are used in preprocessing, so the app is tied to a tabular ML pipeline rather than a deep-learning or API-based architecture.

9. `model.pkl` is the deployed inference artifact and is the main runtime dependency for the app. The app does not retrain the model on startup; it loads the stored artifact, which makes startup fast and keeps the deployed behavior stable relative to training.

10. `requirements.txt` defines the Python environment, with Streamlit and common ML libraries as the key runtime dependencies. There are no database, queue, or external service components in this repo; everything is local-file based: CSV in, pickle out, Streamlit UI on top.

11. For an architecture diagram, the important flows are:
   - user interacts with Streamlit UI in `app.py`
   - `app.py` calls prediction logic in `mental_health.py`
   - `mental_health.py` loads `model.pkl` and applies the trained preprocessing assumptions
   - `train_model.py` produces `model.pkl` from `mental_health_dataset.csv`
   - `mental_health.ipynb` sits outside the main runtime path as analysis/prototyping support

Main entry points: [`train_model.py`](https://github.com/mahamudul-hasan-cse/mental-health-prediction-mlapp/blob/main/train_model.py) (Training pipeline), [`mental_health.py`](https://github.com/mahamudul-hasan-cse/mental-health-prediction-mlapp/blob/main/mental_health.py) (Predictor), [`model.pkl`](https://github.com/mahamudul-hasan-cse/mental-health-prediction-mlapp/blob/main/model.pkl) (Model artifact), [`app.py`](https://github.com/mahamudul-hasan-cse/mental-health-prediction-mlapp/blob/main/app.py) (Streamlit UI)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_offline["Offline training"]
  node_train_model_py["Training pipeline<br/>[train_model.py]"]
  node_preprocessing{{"Preprocessing<br/>feature engineering"}}
  node_classifier(("Classifier<br/>supervised model"))
end

subgraph group_online["Online prediction"]
  node_app_py["Streamlit UI<br/>[app.py]"]
  node_mental_health_py["Predictor<br/>inference<br/>[mental_health.py]"]
end

subgraph group_support["Supporting assets"]
  node_dataset_csv[("Dataset<br/>tabular data")]
  node_model_pkl[("Model artifact<br/>pickle artifact<br/>[model.pkl]")]
  node_notebook_ipynb["Notebook<br/>exploration"]
  node_requirements_txt["Dependencies<br/>env spec<br/>[requirements.txt]"]
end

node_dataset_csv -->|"input"| node_train_model_py
node_train_model_py -->|"fit"| node_preprocessing
node_preprocessing -->|"transform"| node_classifier
node_train_model_py -->|"save"| node_model_pkl
node_classifier -->|"serialize"| node_model_pkl
node_app_py -->|"calls"| node_mental_health_py
node_mental_health_py -->|"loads"| node_model_pkl
node_mental_health_py -->|"applies"| node_preprocessing
node_app_py -.->|"depends on"| node_requirements_txt
node_train_model_py -.->|"depends on"| node_requirements_txt
node_notebook_ipynb -.->|"analyzes"| node_dataset_csv
node_notebook_ipynb -.->|"experiments with"| node_classifier

click node_app_py "https://github.com/mahamudul-hasan-cse/mental-health-prediction-mlapp/blob/main/app.py"
click node_mental_health_py "https://github.com/mahamudul-hasan-cse/mental-health-prediction-mlapp/blob/main/mental_health.py"
click node_train_model_py "https://github.com/mahamudul-hasan-cse/mental-health-prediction-mlapp/blob/main/train_model.py"
click node_dataset_csv "https://github.com/mahamudul-hasan-cse/mental-health-prediction-mlapp/blob/main/mental_health_dataset.csv"
click node_model_pkl "https://github.com/mahamudul-hasan-cse/mental-health-prediction-mlapp/blob/main/model.pkl"
click node_notebook_ipynb "https://github.com/mahamudul-hasan-cse/mental-health-prediction-mlapp/blob/main/mental_health.ipynb"
click node_requirements_txt "https://github.com/mahamudul-hasan-cse/mental-health-prediction-mlapp/blob/main/requirements.txt"

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_train_model_py,node_preprocessing,node_classifier toneBlue
class node_app_py,node_mental_health_py toneAmber
class node_dataset_csv,node_model_pkl,node_notebook_ipynb,node_requirements_txt toneMint
```

## Components

### Offline training

Builds the classifier artifact from the tabular dataset.

- [Training pipeline](https://github.com/mahamudul-hasan-cse/mental-health-prediction-mlapp/blob/main/train_model.py): training · Offline script that reads the dataset, preprocesses features, trains a classifier, and saves artifacts. · `train_model.py`
- **Preprocessing**: feature engineering · Shared assumptions for encoding, scaling, and feature ordering between training and inference.
- **Classifier**: supervised model · The trained mental-health risk classifier, typically RandomForest with optional XGBoost.

### Online prediction

Streamlit app and inference helper for runtime predictions.

- [Streamlit UI](https://github.com/mahamudul-hasan-cse/mental-health-prediction-mlapp/blob/main/app.py): ui · Browser-based form that collects user inputs and displays the predicted risk category. · `app.py`
- [Predictor](https://github.com/mahamudul-hasan-cse/mental-health-prediction-mlapp/blob/main/mental_health.py): inference · Transforms form values into model input and returns the mental-health risk class. · `mental_health.py`

### Supporting assets

Data, artifact, notebook, and environment definitions.

- [Dataset](https://github.com/mahamudul-hasan-cse/mental-health-prediction-mlapp/blob/main/mental_health_dataset.csv): tabular data · Structured training data with mental-health features and the target label. · `mental_health_dataset.csv`
- [Model artifact](https://github.com/mahamudul-hasan-cse/mental-health-prediction-mlapp/blob/main/model.pkl): pickle artifact · Serialized trained classifier loaded by the runtime app for prediction. · `model.pkl`
- [Notebook](https://github.com/mahamudul-hasan-cse/mental-health-prediction-mlapp/blob/main/mental_health.ipynb): exploration · Interactive analysis and experimentation outside the production flow. · `mental_health.ipynb`
- [Dependencies](https://github.com/mahamudul-hasan-cse/mental-health-prediction-mlapp/blob/main/requirements.txt): env spec · Python packages needed for training and the Streamlit app. · `requirements.txt`

## Connections

- Dataset → Training pipeline: input · Training data is read from the CSV.
- Training pipeline → Preprocessing: fit · Training fits preprocessing to the dataset.
- Preprocessing → Classifier: transform · Encoded and scaled features are used to train the model.
- Training pipeline → Model artifact: save · Trained artifact is serialized to disk.
- Classifier → Model artifact: serialize · The fitted classifier is stored in the pickle artifact.
- Streamlit UI → Predictor: calls · The UI delegates prediction to the helper layer.
- Predictor → Model artifact: loads · Inference loads the saved model artifact from disk.
- Predictor → Preprocessing: applies · Runtime inputs must match training-time feature assumptions.
- Streamlit UI → Dependencies: depends on · Streamlit and ML libraries are required to run the app.
- Training pipeline → Dependencies: depends on · Training uses the same Python ML environment.
- Notebook → Dataset: analyzes · Notebook likely explores the dataset and modeling ideas.
- Notebook → Classifier: experiments with · Prototype modeling and analysis happen outside the main runtime flow.
