This project provides a tool for analyzing mental health based on user-provided statements. It uses machine learning techniques for prediction and provides insights into potential mental health conditions and sentiment.
- Overview
- Dataset Preprocessing Steps
- Model Selection Rationale
- How to Run the Inference Script
- UI/CLI Usage Instructions
- Dependencies
This tool takes a statement as input and predicts the user's mental health condition (e.g., Anxiety, Depression, Stress, Neutral) and the sentiment of the statement (Positive, Negative, Neutral). It then offers basic coping advice and connects to a large language model to provide detailed and personalized assistance.
Dataset Used: Sarkar, Suchintika (2024), "Sentiment Analysis for Mental Health", Kaggle.
The dataset used for training the model went through the following preprocessing steps:
- Data Loading: The dataset (CSV file) was loaded into a Pandas DataFrame.
- Handling Missing Values: Rows with missing values were removed using
dropna()to ensure data completeness. - Sentiment Analysis: The VADER sentiment analyzer was used to determine the sentiment (Positive, Negative, Neutral) of each statement and create a sentiment score. These sentiment scores became new features.
- Feature Engineering: The "statement" column, mental health status, and sentiment analysis were converted into feature vectors suitable for machine learning.
- The dataset has 53403 rows with columns containing a statement and the mental health status associated with the statement. It contained 362 rows where the statement was null. After dropping rows with null values, the total number of entries is 52681.
- The test size was 25% for y_status and 30% for y_sentiment.
- TF-IDF Vectorization: Used to convert text data into numerical feature vectors, which can be used for machine learning models.
- Linear Support Vector Classification (LinearSVC): First model of choice due to high accuracy and low computing power.
- Random Forest Classifier: Another model tested to improve prediction accuracy, although performed worse than LinearSVC.
- Groq LLM: Finally implemented to add a level of complexity, and personalized coping strategies.
To run the inference script and get a prediction for a new statement, follow these steps:
-
Ensure the required libraries are installed:
pip install scikit-learn pandas nltk -
Save the
predict_mental_health.pyscript: Save the script to a file namedpredict_mental_health.py. -
Run the script from the command line:
python predict_mental_health.py "Your statement here"Replace
"Your statement here"with the actual statement you want to analyze.Example:
python predict_mental_health.py "I've been feeling down and anxious lately." -
Check the Output: The script will print the predicted mental health condition and a suggested coping mechanism.
This project has two ways to interact with it: a command-line interface (CLI) using the inference script, and a Gradio-based user interface (UI).
The CLI is used through the predict_mental_health.py script as shown in the previous section ("How to Run the Inference Script").
Eg:
The UI provides a more interactive way to use the tool through a web browser.
Steps to Run the UI:
-
Ensure the required libraries are installed:
pip install vaderSentiment pip install langchain pip install groq pip install python-dotenv pip install gradio pip install langchain-groq pip install scikit-learn pandas nltk lime -
Set your Groq API key:
- Obtain an API key from Groq.
- Set the
GROQ_API_KEYenvironment variable in your environment or directly in the script if necessary (not recommended for security reasons).
os.environ['GROQ_API_KEY'] = 'YOUR_GROQ_API_KEY' -
Run the main Python script:
python your_main_script.pyThis will launch a Gradio interface in your web browser.
-
Interact with the UI:
- Enter your statement in the text box.
- Click the "Submit" button.
- The chatbot response, including the predicted mental health status and suggestions, will be displayed.
Eg:
The project relies on the following Python libraries:
pandas: For data manipulation and reading CSV files.nltk: For sentiment analysis.scikit-learn: For machine learning models (LinearSVC, RandomForestClassifier), TF-IDF vectorization, and train-test splitting.python-dotenv: For loading environment variables.langchain: For integrating with LLMs like Groq.gradio: For creating the user interface.lime: for model explainabilitypickle: To load the saved model
You can install these dependencies using pip:
Notes:
- Replace
"path/to/your/model.pkl"with the actual path to your saved model file. - The Gradio interface requires a Groq API key, which should be set as an environment variable.

