How mahamudul-hasan-cse/mental-health-prediction-mlapp works
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 Training pipeline · mental_health.py Predictor · model.pkl Model artifact · app.py Streamlit UI
Components 9
Offline training
Builds the classifier artifact from the tabular dataset.
- Training pipeline · Offline script that reads the dataset, preprocesses features, trains a classifier, and saves artifacts.
train_model.py - Preprocessing · Shared assumptions for encoding, scaling, and feature ordering between training and inference.
- Classifier · The trained mental-health risk classifier, typically RandomForest with optional XGBoost.
Online prediction
Streamlit app and inference helper for runtime predictions.
- Streamlit UI · Browser-based form that collects user inputs and displays the predicted risk category.
app.py - Predictor · 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 · Structured training data with mental-health features and the target label.
mental_health_dataset.csv - Model artifact · Serialized trained classifier loaded by the runtime app for prediction.
model.pkl - Notebook · Interactive analysis and experimentation outside the production flow.
mental_health.ipynb - Dependencies · Python packages needed for training and the Streamlit app.
requirements.txt
Connections 12
- 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.