Spaces:
Running
Running
title: NLP Sentiment Analysis App | |
emoji: π | |
colorFrom: blue | |
colorTo: purple | |
sdk: gradio | |
sdk_version: "3.50.0" | |
app_file: app.py | |
pinned: false | |
# NLP Sentiment Analysis π | |
This is a **sentiment analysis web app** built using **Hugging Face Transformers** and **Gradio**, deployed on **Hugging Face Spaces**. | |
## π Features | |
- β Uses **DistilBERT** for sentiment analysis. | |
- β Provides **positive/negative** sentiment classification. | |
- β **Web-based UI** using Gradio. | |
- β **Easy deployment** on Hugging Face Spaces. | |
--- | |
# Personalized Mental Health Assistant | |
## Overview | |
This project is an AI-powered **Mental Health Assistant** that analyzes user queries, detects emotions, and provides relevant responses and resources. It leverages **NLP (Natural Language Processing)** techniques and **Machine Learning (ML)** models for sentiment analysis, emotion classification, and response generation. | |
## Features | |
- **Sentiment & Emotion Detection**: Classifies user queries based on emotions (Happy, Sad, Anxious, Overwhelmed, etc.). | |
- **Preprocessing Pipeline**: Cleans and preprocesses text data using **NLTK**. | |
- **TF-IDF Vectorization**: Converts user queries into numerical representations. | |
- **ML Classification**: Uses a **Random Forest Classifier** to predict emotions. | |
- **Response Generation**: Suggests appropriate responses based on detected emotions. | |
- **AWS Deployment**: Deploys as an API using **Flask & AWS Lambda**. | |
## Dataset Columns | |
The dataset includes the following columns: | |
| Column | Description | | |
|--------|-------------| | |
| ID | Unique identifier for each query | | |
| User Query | Raw text input from the user | | |
| Preprocessed Text | Cleaned version of the user query | | |
| Sentiment | Sentiment label (Positive, Neutral, Negative) | | |
| Emotion | Emotion label (e.g., Happy, Sad, Anxious) | | |
| Severity | Severity level of distress | | |
| Intent | Intent classification for responses | | |
| Suggested Response | AI-generated response for user support | | |
| Recommended Action | Suggested action based on the query | | |
| Related Resources | External resources for mental health support | | |
| Time of Interaction | Timestamp of the query | | |
--- | |
## Installation | |
### **1. Clone Repository** | |
```bash | |
git clone https://github.com/your-repo/mental-health-assistant.git | |
cd mental-health-assistant | |
``` | |
### **2. Install Dependencies** | |
```bash | |
pip install -r requirements.txt | |
``` | |
### **3. Download NLTK Resources** | |
```python | |
import nltk | |
nltk.download('stopwords') | |
nltk.download('wordnet') | |
``` | |
--- | |
## Training the Model | |
### **Step 1: Preprocess and Train** | |
```python | |
python train.py | |
``` | |
This script: | |
- Loads the dataset. | |
- Preprocesses text using **NLTK**. | |
- Extracts features using **TF-IDF**. | |
- Trains a **Random Forest Classifier**. | |
- Saves the trained model and vectorizer. | |
### **Step 2: Test Model with Example Queries** | |
```python | |
python test.py | |
``` | |
--- | |
## Deployment on AWS | |
This project can be deployed on **AWS Lambda & API Gateway** using Flask. | |
### **1. Create an AWS Lambda Function** | |
1. Package your code into a **ZIP file**: | |
```bash | |
zip -r deployment.zip * | |
``` | |
2. Upload the ZIP file to AWS Lambda. | |
3. Set the runtime to **Python 3.8+**. | |
4. Create a handler function (`lambda_function.py`): | |
```python | |
from flask import Flask, request, jsonify | |
import joblib | |
app = Flask(__name__) | |
model = joblib.load("mental_health_model.pkl") | |
vectorizer = joblib.load("tfidf_vectorizer.pkl") | |
label_encoder = joblib.load("label_encoder.pkl") | |
def predict_emotion(query): | |
vectorized_input = vectorizer.transform([query]).toarray() | |
prediction = model.predict(vectorized_input) | |
return label_encoder.inverse_transform(prediction)[0] | |
@app.route("/predict", methods=["POST"]) | |
def predict(): | |
data = request.get_json() | |
query = data.get("query", "") | |
response = predict_emotion(query) | |
return jsonify({"emotion": response}) | |
if __name__ == "__main__": | |
app.run() | |
``` | |
5. Deploy with **AWS API Gateway** to create an endpoint. | |
### **2. Access API** | |
Once deployed, you can access the API via: | |
```bash | |
curl -X POST "https://your-api-gateway-url.amazonaws.com/prod/predict" -H "Content-Type: application/json" -d '{"query": "I feel stressed and anxious."}' | |
``` | |
--- | |
## Future Improvements | |
- **Use Deep Learning (LSTMs, Transformers)** for better accuracy. | |
- **Integrate GPT-based models** for advanced response generation. | |
- **Expand dataset** to include more diverse mental health situations. | |
--- | |
## License | |
MIT License. Free to use and modify. | |
--- | |
## Contributors | |
- **Your Name** β [GitHub Profile](https://github.com/your-github) | |
- **Open for Contributions** π | |