In this project I used Django as backend and Django REST Framework to build the RESTful
API to analyse the sentiment of a text. The API endpoint is /analyze which takes the following
data as POST request payload:
{
"text": "Simple text"
}And return the output after analyse the sentiment of that text as following:
{
"sentiment": "positive/negative/neutral"
}The API can be tested at docs/ endpoint which will load the Swagger Docs.
The pre-trained model used in this project to analyze the sentiment of a sentence/text
cardiffnlp/twitter-roberta-base-sentiment-latest which can be found
here.
This is Twitter-roBERTa-base for Sentiment Analysis model which updated at 2022 and trained on ~124M tweets from January 2018 to December 2021, and fine-tuned for sentiment analysis with the TweetEval benchmark.
The project can be setup and run by 2 ways:
- By using Docker
- Manual Setup
This is the easiest way to set up and run the project. For this, the Docker and docker compose plugin are
need to be installed in your local computer. The installation instructions can be found here.
After that, use the following commands to run the project:
git clone https://github.com/shazolKh/Sentiment-Analysis.gitcd Sentiment-Analysis/cp .env.example .envand put some random text as the value ofSECRET_KEYat.envfile.docker compose up --build
The last command will take some time to download the model and install the required dependencies to run the project.
The project will be available at localhost:8000 or http://127.0.0.1:8000.
After that, the API can be tested with API testing tool or at docs/ endpoint with Swagger.
Assuming you have completed the necessary setup to perform python related commands. And also assuming
that you are using a linux computer or you are using WSL on windows. Use the following commands to run the project.
Noted that, at least, python 3.8 is needed.
apt updateapt-get install git-lfsgit lfs installgit clone https://github.com/shazolKh/Sentiment-Analysis.gitcd Sentiment-Analysis/python -m venv venvorpython3 -m venv venv- Activate the virtual environment.
pip install -r requirements.txt- Change the working directory to
model/folder. git lfs clone https://huggingface.co/cardiffnlp/twitter-roberta-base-sentiment-latest- Change the working directory back to
Sentiment-Analysis/i.e. project root folder. cp .env.example .envand put some random text as the value ofSECRET_KEYat.envfile.python manage.py migratepython manage.py runserver
The project will be available at http://127.0.0.1:8000. And the API can be tested with API testing tool or
at docs/ endpoint with Swagger.