Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
Project Overview

This project aims to develop a web application capable of predicting human stress levels based on text provided by users. The prediction model is built using a dataset sourced from Kaggle. The web application is developed using the Django framework, and the model achieves an accuracy of 86%.

Key Components
Data Analysis and Preprocessing
The project includes comprehensive Jupyter notebook files that detail the entire data analysis and preprocessing steps. Key processes involve:

Data visualization to understand patterns and correlations.
Data cleaning and preprocessing to prepare the dataset for machine learning algorithms.
Natural Language Processing (NLP)
The prediction is based on the analysis of user-provided text, capturing their emotions and feelings. Through NLP techniques, the patterns in the text are read and analyzed to make predictions about stress levels.

Machine Learning Algorithms
Two machine learning algorithms were implemented for stress prediction:

RandomForestClassifier
Multinomial Naive Bayes
Upon evaluation, the Multinomial Naive Bayes algorithm demonstrated superior performance and was thus selected for deployment in the web application.

Model Deployment
The trained model is serialized using the pickle package and integrated into a simple web application developed with the Django framework. The web application includes a sign-in form and is designed to be user-friendly.

Running the Project
To run this project, ensure you have the necessary settings for the Django framework configured. All required packages are listed in the requirements.txt file.

Thank you for your interest in this project.
  • Loading branch information
Priyanka32-gif authored Jun 11, 2024
1 parent bde9ccd commit a26dd9e
Show file tree
Hide file tree
Showing 26 changed files with 6,856 additions and 0 deletions.
Binary file not shown.
2,839 changes: 2,839 additions & 0 deletions Stress Prediction/Human Stress Prediction/Stress.csv

Large diffs are not rendered by default.

2,405 changes: 2,405 additions & 0 deletions Stress Prediction/Human Stress Prediction/Stress_Prediction.ipynb

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for Stress_Prediction_Project project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/5.0/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Stress_Prediction_Project.settings')

application = get_asgi_application()
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# forms.py
from django import forms
from django.contrib.auth.forms import AuthenticationForm

class LoginForm(AuthenticationForm):
username = forms.CharField(label="Username")
password = forms.CharField(label="Password", widget=forms.PasswordInput)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'gfg.demo.django.login'
EMAIL_HOST_PASSWORD = 'gfgdemo123'
EMAIL_PORT = 587
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import pandas as pd
import string
from datetime import datetime
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.naive_bayes import MultinomialNB
import pickle

data = pd.read_csv("Stress.csv") #reading csv file
data.drop(['post_id','sentence_range'],axis=1,inplace=True) #droping these columns because these are less required columns

data['text_length'] = data['text'].apply(len)
'''dividing timestamp into day hr min sec month and year'''
data['date'] = data['social_timestamp'].apply(lambda time: datetime.fromtimestamp(time))
data['month'] = data['date'].apply(lambda date: date.month)
data['day'] = data['date'].apply(lambda date: date.day)
data['week_day'] = data['date'].apply(lambda date: date.day_name)
data['hour'] = data['date'].apply(lambda date: date.hour)
data['minumte'] = data['date'].apply(lambda date: date.minute)
data['sec'] = data['date'].apply(lambda date: date.second)

data.drop(['date','week_day'],axis=1,inplace=True) #droping date and week_day columns
data.drop(['social_timestamp'],axis=1,inplace=True)


def text_process(mess):
nopunc = [char for char in mess if char not in string.punctuation]
nopunc = ''.join(nopunc)

return [word for word in nopunc.split() if word.lower() not in stopwords.words('english')]

mess_data = data[['label', 'text_length', 'text']]
mess_data['label_name'] = data['label'].map({0: 'Not Stress', 1: 'Stress'})

# Train the model
model = MultinomialNB()
mess_transformer = CountVectorizer().fit(mess_data['text'])
messages_bow = mess_transformer.transform(mess_data['text'])
tfidf_transformer = TfidfTransformer().fit(messages_bow)
messages_tfidf = tfidf_transformer.transform(messages_bow)
model.fit(messages_tfidf, mess_data['label_name'])

# Pickle the model and transformers
with open('Stress_Prediction_model.pkl', 'wb') as model_file:
pickle.dump(model, model_file)

with open('CountVectorizer.pkl', 'wb') as cv_file:
pickle.dump(mess_transformer, cv_file)

with open('TfidfTransformer.pkl', 'wb') as tfidf_file:
pickle.dump(tfidf_transformer, tfidf_file)
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
"""
Django settings for Stress_Prediction_Project project.
Generated by 'django-admin startproject' using Django 5.0.3.
For more information on this file, see
https://docs.djangoproject.com/en/5.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.0/ref/settings/
"""

from pathlib import Path
from .info import *
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

EMAIL_USE_TLS = EMAIL_USE_TLS
EMAIL_HOST = EMAIL_HOST
EMAIL_HOST_USER = EMAIL_HOST_USER
EMAIL_HOST_PASSWORD = EMAIL_HOST_PASSWORD
EMAIL_PORT = EMAIL_PORT

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-4n(9o9%ua$_fc5o(v6k1r^b=$&nee&3-c23#+%d96$jt0u8(3('

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []

APPEND_SLASH = False

# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'Stress_Prediction_Project.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'Stress_Prediction_Project','templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'Stress_Prediction_Project.wsgi.application'


# Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}


# Password validation
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/5.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.0/howto/static-files/

STATIC_URL = '/static/'

# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% autoescape off %}

Activation failed, Please Try Again !!

{% autoescape %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% autoescape off %}

Welcome to Colors Login!!
Hello {{ name }}!!
Please confirm your email by clicking on the following link.

Confirmation Link: http://{{ domain }}{% url 'activate' uidb64=uid token=token %}

{% endautoescape %}
Loading

0 comments on commit a26dd9e

Please sign in to comment.