Sentiment Analysis: Using HuggingFace, RoBERTa model and MindsDB
Sentiment Analysis: Using HuggingFace, RoBERTa model and MindsDB
Here's a quick walkthrough on how you can setup up a complete sentiment analysis pipeline using MindsDB and the Twitter RoBERTa model from Hugging Face, running on an AWS EC2 instance. We'll analyze customer feedback from MindsDB's sample PostgreSQL database and come up with a prediction for the customer sentiment.
1. AWS Account with EC2 access
4. Connecting to Postgres database
5. Configuring RoBERTa model & predicting sentiment for sample data using the model
Part 1: Setting Up the EC2 Instance
Log into AWS Console and navigate to EC2
Choose t2.large (minimum recommended for ML workloads)
5. Configure storage to be 30GB or more
7. Create some network settings,
--- Allow HTTPS trafic from Internet, and
----Allow HTTP traffic from Internet
8. Configure security group by going to the Security tab and clicking on the security group link:-
These three settings should already be there:
3. Allow HTTPS (Port 443)
Add one more setting for the MindsDB port number:-
--> Allow Custom TCP (Port 47334) for MindsDB
8. Launch instance, note down the public IP address and download your key pair to your local machine
1.2 Connect to Your Instance
chmod 400 your-key-pair.pem
ssh -i your-key-pair.pem ubuntu@your-instance-public-ip
Part 2: Installing Docker
2.1 Update System and Install Dependencies
sudo apt-get install -y \
software-properties-common
Installing Docker is quick, just follow the steps outlined here.
# 1. download the script # #
curl -fsSL https://get.docker.com -o install-docker.sh
# # 2. verify the script's content
sudo sh install-docker.sh
sudo systemctl start docker sudo systemctl enable docker sudo usermod -aG docker ubuntu
Log out and log back in for group changes to take effect.
Part 3: Setting Up MindsDB
If you want to persist your models and configurations in the host machine, run these commands:
Note that we are calling the docker run command directly, as it will install the MindsDB instance and the particular engine from huggingface right away and run it as well on port 47334
docker run --name Anil_mindsdb_container -p 47334:47334 -p 47335:47335 -v $(pwd)/mdb_data:/root/mdb_storage mindsdb/mindsdb:huggingface
Now you can access the MindsDB editor by going to
http://<ec2-public-ip-address>:47334 in your browser
Try both, (http and https) - sometimes if https is not configured you might be able to connect via http
Part 4: Connect to Sample PostgreSQL Database
MindsDB offers a sample PostgreSQL database in read only mode, so let's connect to it
CREATE DATABASE example_db
WITH ENGINE = "postgres",
"password": "demo_password",
"host": "samples.mindsdb.com",
Part 5: Configuring Twitter RoBERTa Model and Sample Database
5.1 Create Sentiment Model
Create a new model called "sentiment_classifier" that is responsible for generating a field called "sentiment". We are going to use a model called "twitter-roberta-base-sentiment", which will be provided by the "huggingface" engine.
CREATE MODEL sentiment_classifier
model_name= 'cardiffnlp/twitter-roberta-base-sentiment',
task='text-classification',
input_column = 'comment',
labels=['negative','neutral','positive'];
You can confirm it by observing it in the left side tab under models.
Test to see how does the sentiment classifier provide the data
SELECT * FROM sentiment_classifier
WHERE comment='It is really sucks to do NLP with MindsDB';
This is the output you should see.
Try changing the text with different content and see what happens.
#Now let's try with the sample database
SELECT input.comment, model.sentiment
FROM example_db.user_comments AS input
JOIN sentiment_classifier AS model;
You should see an output like this.
You now have a complete sentiment analysis pipeline that can automatically process customer feedback using the Twitter RoBERTa model. This setup provides valuable insights into customer sentiment and can be easily adapted for other text analysis tasks like support requests from tools like Zendesk, Intercom, etc.
Regularly monitor system performance
Keep all components updated
Review and adjust sentiment analysis results
Back up your data regularly
Monitor costs and resource usage
For production environments, consider setting up redundancy and implementing proper CI/CD practices for updates and maintenance.