CalmNest: Cultivating Mental Wellness for Everyone
Tags: rebase01, gdsciiitkalyani
Published: 2024-04-20 05:14:38

This blog is the final submission of Team 33 (Algorithmic Alchemists) for the Rebase <01> hackathon hosted by IIIT Kalyani.

## Table of Contents

1. [What is CalmNest?](#what-is-calmnest)
2. [Meet the CalmNest Team](#meet-the-calmnest-team)
3. [How to Use CalmNest?](#how-to-use-calmnest)
4. [Why Mental Health?](#why-mental-health)
5. [Our Toolkit and Tech Stack](#our-toolkit-and-tech-stack)
6. [Behind the Scenes: The Lifecycle of a Chat Message](#behind-the-scenes-the-lifecycle-of-a-chat-message)
7. [Behind the Scenes: The DASS Survey and our Music Recommendation System](#behind-the-scenes-dass-survey-and-our-music-recommendation-system)
8. [A Challenge Faced and Conquered: CORS Issue](#a-challenge-faced-and-conquered-cors-issue)
9. [Choosing the Right Resources: Intellectual Property Rights](#choosing-the-right-resources-intellectual-property-rights)
10. [We value efficiency](#we-value-efficiency)

## Important Links 
[Demo Video Link](https://www.youtube.com/watch?v=l1AOVsF44Vk&feature=youtu.be) 
[GitHub Repo Link](https://github.com/MehakMantri/CalmNest)

## What is CalmNest?
CalmNest is an all-purpose mental health platform designed to improve mental well-being among people of all ages. It offers various features such as report generation, music recommendation, chatbot, journal entries, and a resource library. Additionally, it provides four different themes to cater to diverse user preferences.

{% embed https://www.youtube.com/watch?v=l1AOVsF44Vk %}
Watch a short 3-minute demo video to get an introduction to our website!

## Meet the CalmNest Team
Our team consists of individuals from different states and colleges. Let's introduce them briefly:

### Team Lead
- **Divye Joshi (Devfolio : endy)** ([LinkedIn](https://www.linkedin.com/in/divye-joshi-986327296/))
  - A machine learning enthusiast with expertise in various ML techniques. I am currently pursuing Computer Science Engineering at Jawaharlal Nehru University (JNU), completing the second year.

### Team Member 1
- **Aditya Mishra (Devfolio: @aditya_jnu)** ([LinkedIn](https://www.linkedin.com/in/aditya-mishra-9b7690253/))
  - I'm currently dedicated to mastering web development. I am a 2nd-year student (ECE) at Jawaharlal Nehru University (JNU), School of Engineering.

### Team Member 2
- **Mehak Mantri (Devfolio : MehakMantri)** ([LinkedIn](https://www.linkedin.com/in/mehakmantri))
  - Specializes in web development and enjoys exploring new technologies to enhance user experiences. Currently a second-year student at Kurukshetra University, Kurukshetra (KUK).

### Team Member 3
- **Satyam Kumar (Devfolio: @sk73233)** ([LinkedIn](https://www.linkedin.com/in/satyam-kumar-0626b3200/))
  - I enjoy competitive programming and have a passion for web development. Currently a 4th-year student at Netaji Subhash Engineering College, Kolkata.

## How to Use CalmNest?
You can start using CalmNest right away! Access the open-source GitHub repository [here](https://github.com/MehakMantri/CalmNest). The repository contains detailed step-by-step technical instructions in the README.md file to power up the Backend API and get the website running.

If you encounter any issues setting up the website, reach out to Divye on LinkedIn or through Discord (@endymiion).

![Github Showcase](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oytcza3ma6417znaj52a.gif)

## Why Mental Health?
Mental health is a critical aspect of overall well-being, aligning with Goal 3 of the 17 Sustainable Development Goals by the UN, which aims to ensure healthy lives and promote well-being for all at all ages ([learn more](https://sdgs.un.org/goals/goal3)). Addressing mental health issues is crucial, as evidenced by the article "[Emotional problems in young people were rising rapidly even before the pandemic](https://theconversation.com/emotional-problems-in-young-people-were-rising-rapidly-even-before-the-pandemic-220799)" published on The Conversation.

However, our aspirations extend beyond this singular objective. By harnessing the power of technology and community-driven initiatives, CalmNest also contributes to Goal 9: "Build resilient infrastructure, promote sustainable industrialization, and foster innovation," Goal 10: "Reduce inequality within and among countries," and Goal 17: "Strengthen the means of implementation and revitalize the global partnership for sustainable development."

![A Survey Result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d3xw0r8b1jklcv4w55mm.png)

## Our Toolkit and Tech Stack

### Datasets
- **[Mental Health Counseling Conversation Dataset](https://huggingface.co/datasets/Amod/mental_health_counseling_conversations)**
  - A collection of questions and answers sourced from online counseling and therapy platforms, providing insights into various mental health topics.
- **[Music & Mental Health Survey Results](https://www.kaggle.com/datasets/catherinerasgaitis/mxmh-survey-results)**
  - A dataset aiming to identify correlations between individuals' music taste and self-reported mental health.

### Models
- **[TinyLlama-1.1B (Chat)](https://huggingface.co/TinyLlama/TinyLlama-1.1B-Chat-v1.0)**
  - A pre-trained Llama model for chat applications, trained on 3 trillion tokens.
- **[GTE-small (Generate Text Embedder)](https://huggingface.co/Supabase/gte-small)**
  - Models trained by Alibaba DAMO Academy, primarily based on the BERT framework.

### Libraries and Frameworks
- **HTML**: Markup language for structuring webpages.
- **CSS**: Styling language for designing webpage layouts and appearances.
- **JavaScript**: Programming language for adding interactivity and functionality to webpages.
- **ReactJS**: JavaScript library for building user interfaces with modular and reusable components.
- **Vite**: Build tool for bundling and optimizing frontend code, enhancing development workflow.
- **PyTorch**: Open-source machine learning library for Python, providing support for deep learning tasks.
- **Hugging Face Transformers**: Library providing state-of-the-art natural language processing models and tools.
- **Flask**: Lightweight web framework for Python, enabling the development of web applications with simplicity and flexibility.

## Behind the Scenes: The Lifecycle of a Chat Message

- Whenever a chat message is sent to the chatbot, it gets converted to JSON format: `{"query": "(message)"}`. For example, "I am depressed" becomes `{"query": "I am depressed"}`.
- The JSON file is sent to the API using a POST request, and the message is retrieved by the Python backend.
- The message is then converted to a word embedding and compared with a vector database containing actual therapist conversations (dataset provided in our toolkit).
- Using cosine similarity, we find the closest query that has been asked to a therapist.
- For example, "I am depressed" might be most similar to a conversation like:
  - Human: "I am having depression."
  - Therapist: "I see, you should…"
- Using the therapist's answer to that query, we generate a final prompt, feed it to the LLM, and get the final response from that LLM using a GET request through the ngrok public tunnel.

![chat-diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pd3z5x2monetcw4qn79m.png)

![Backend-Diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/izzni0rs1hq2w0563smu.png)

## Behind the Scenes: DASS Survey and Our Music Recommendation System

- The DASS 21-Questionnaire is an efficient survey used to assess levels of stress, depression, and anxiety (https://maic.qld.gov.au/wp-content/uploads/2016/07/DASS-21.pdf). We have implemented this survey in CalmNest.
- Once we find these levels, we retrieve the anxiety-depression (A_D) score list (out of 10) and compare the scores to our music recommendation database (e.g., [7,0]).
- Our database contains A_D scores and the genre of music that has improved mental health. We get the closest A_D score and recommend the same genre of music to the user who has completed the survey. This dataset is derived from the dataset mentioned in our toolkit.
- The closest A_D score to the input score is calculated using Euclidean Distance.

![music-table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dv8n6r6s809xrz6zhtba.png)

![music-backend-diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mx9eys0eut83o4l9bfvl.png)

## A Challenge Faced and Conquered: CORS Issue

- We faced a challenge when using the fetch API to send POST requests to the API Server. The server was healthy, and Postman was working to connect to it, but the website was blocked due to the POST requests being sent as OPTIONS requests, resulting in a CORS error.
- However, thanks to a Stack Overflow article, we could fix this issue by implementing the Flask-Cors library to allow preflight requests in our network: [Stack Overflow article](https://stackoverflow.com/questions/28461001/python-flask-cors-issue).

![cors-error](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fev1897mxdb3p4ybe264.png)

![cors-fix](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8t3rma09ppr9ljkth1bf.png)

## Choosing the Right Resources: Intellectual Property Rights

- Theme Music and Background: We have used free images from [Freepik](https://www.freepik.com) and music taken from YouTube (Non-Copyrighted Sounds).
- Models and Datasets: We have checked the licenses of the models and datasets to ensure they align with our vision. If we ever commercialize this project, we will adhere to the terms under them.
- Resource Library: When you use the resource library, you are directed towards the legal Google Books page of that book, instead of pirated illegal websites.

![tinyllama-license](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ho4amqx4ke18wjv30ly.png)

![legal-gif](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mi2w14sy0vqutnc2iwpl.gif)

## We value efficiency

- LLAMA - 2 INFERENCE TIME IN GOOGLE COLAB (FREE) : 10-20 minutes.
- TINY LLAMA INFERENCE TIME IN COLAB : ONLY 5-10 SECONDS! 
![llmtime](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j84yj75hzuucet6x48a5.png)

- GTE Small is very lightweight in size and powerful
![gte](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3mvqi53u148v71v7um2r.png)