End-to-end Natural Language Processing WebApp build using Flask and deployed as an image on DockerHub. A docker image as a web application which predicts the sensitivity of text (negative or positive) using multinomial naive bayes algorithm and countvectorizer. The random state
parameter has been set to a constant, so that the model doesn't undergo underspecification during deployement stage. Green command to be executed to run the image directly in any local machine and red is the output after running the image (detached mode). The image is in it's own docker isolated network
, for our application to talk to the backend we must specify on which port will the requests hit the application (port:5000)
and which port the application listens to (exposed as 4000, can't be changed during runtime).
For this project, I have used the Twitter sentiment dataset. It consists of 2000 rows and 2 columns. It's a relatively small dataset which makes it portable across different computers. The target column has 2 unique values, pos and neg. The other column has textual data. On the side note, I used the Rainbow CSV extension for VS-code to make .csv files look more attractive 😅. The dataset can be viewed here.
The web application was build using inline html styling and rendered using flask utility functions. By clicking on predict, the application will predict whether the comment is a negative or positive comment. Along the footer, I have added all my social icons using fontawesome js kit. Webapp code for home and result page can be found here.
To create docker images and run them in containers, you need to have a dockerfile which includes all the commands to be executed sequential for the application to run in the base OS kernel (in this case, Ubuntu). Firstly, we'll use a basic linux OS kernel with python 3.7.9-slim edition (keeps the image size small and very portable across different computers).
Secondly, we'll make an app directory where all our code will be saved, move to the app directory, then copy all the project files in the app directory, and pip install all the requirements. Finally, we expose the port as 4000 for the docker container to communicate with the app using localhost and execute python3 command along with the file we need to execute for that application to run.
docker build -t ete-nlp-docker-webapp:latest .
Docker build command will build the docker image for us, docker build -t <image-name>:<tag> .
, here -t stands for tag. Tag is used to version the docker images in deployement phase. The default tag is latest. Make sure you are in the working directory of the project.
docker run -d -p 5000:4000 --name my-nlp ete-nlp-docker-webapp:latest
Docker run command will run the docker image in a docker container for us, docker build --name <container-name>:<tag> .
, here --name stands to container name, -d stands for detached mode and -p stands for port. Here, 5000 is the localhost port and 4000 is the docker network port. We are connecting the 5000-4000 port for the app to communicate with the backend.
docker commit <container-id> <dockerhub-username>/<repo-name>
Docker commit command will commit the the unstaged changed and prepare the changes to be pushed in docker hub repositories.
docker login
Docker login command will setup the credentials to log in to a Docker registry.
docker push <newly-created-image-name>
Docker push command will push the staged changes to docker cloud.
Click the icon below to checkout the docker registry, just execute docker run -d -p 5000:4000 nakshatrasinghh/ete-nlp-docker-webapp
on cmd, the application will run on localhost:5000
. After running the image, open a chrome tab, and type localhost:5000
. Your application is ready to use, without downloading any dependencies, you can run an end-to-end web application. Make sure you have docker installed, that's the only thing you'll need to run the application in your local workspace.
If you have any doubts feel free to click on the social icon you would like to connect with 🤗
If you liked my work and gained insightful knowledge, you can buy me a coffee buying click the button below 😊