Deploying Movie Review Sentiment Analysis model to Windows EC2 instance

Sriw World of Coding
Analytics Vidhya
Published in
5 min readAug 19, 2020

The workflow can be broken down into following basic steps:

  1. Training a machine learning model on colab.
  2. Wrapping the inference logic into a flask application.
  3. Hosting flask application on an AWS ec2 instance and consuming the web-service.

Preparing the model

The algorithm we will use for classification is DistilBERT. DistilBERT is a small, fast, cheap and light Transformer model trained by distilling Bert base. It has 40% less parameters than bert and runs 60% faster while preserving over 95% of Bert’s performances.

The notebook and dataset link can be found in the github . Clone the github repositary and store all the contents of the folder inside a new folder DisttilBERT-App . Copy the DisttilBERT-App to Drive and open the distilbert.ipynb in colab and execute the cells and at last we will save the weights of the model for future use. Download the weights to your local host system inside the folder distilbert. Copy the distilbert inside the DisttilBERT-App. After all the steps your DisttilBERT-App folder will look like this.

DisttilBERT-App folder
distilbert folder

Setup Your AWS Account

1. You can create an account by the Amazon Web Services portal and click “Sign in to the Console”. From there you can sign in using an existing Amazon account or create a new account.

2. You will need to provide your details as well as a valid credit card that Amazon can charge. The process is a lot quicker if you are already an Amazon customer and have your credit card on file.

Launch Your AWS Instance

  1. Login to your AWS console and search EC2.
  2. Click on EC2 for launching a new virtual server.
  3. Search for windows and click on select and also make sure to select US East(Ohio) from the drop-down in the top right hand corner.

4. Select the type of EC2 instance you want to boot up t2.micro is prefered and click on next.

5. Next we will configure our instance details.Click on Next until Step 6 and when you reach Configure Security Group change Type to All traffic

6. Click on Next to reach Step 7 and click on Launch. Then in the popup click on Create a new key pair and enter any Key pair name and click on Download Key Pair. This will download a .pem file .

7. It will take time for setup and after setup click on View Instances.You can see that it provide public IP where we will send requests.

8. Click on Connect and in the popup click on Download Remote Desktop File . Again a popup will be promted asking for password . So the get the password Click on GetPassword ,upload the .pem file that we downloaded earlier and click on Decrypt Password and enter the required password . This will open the Windows EC2 Instance.

9. Open Internet Explorer , download and Install Anaconda on remote PC.

10. Install all relevent libraries like flask,ktrain etc.

12. To check everything is working correctly , create a app.py file and copy the code and run the app.py

python app.py

13. Go to browser and type localhost:5000

14. Create a new floder DisttilBERT-App and copy all the files including weights from host computer to remote computer using copy and paste command.

15. After transfer of all files , DisttilBERT-App folder will look similar to this .

16. Open the terminal and execute the below command .

python app.py

17. Now open Flask.ipynb and execute the cells to run the flask server.

18. Now open Sending_Request.ipynb and execute the cells.

Making EC2 instance available for all

As we can see we have been provided with public IP and all the request will be directed to this IP.

Click on Security Groups on right side on public IP,then click on Security Group ID and click on edit Inbound Rules and make following changes and click on save rules.

Now we can send request to EC2 from anywhere in the world ,from your localhost jupyter notebook , colab etc.

Conclusion

We have sucessfully deployed our Movie Review Sentiment Analysis model to EC2 instance…..

I am excited to announce the launch of my new Udemy course, “Apache Airflow Bootcamp: Hands-On Workflow Automation.” This comprehensive course is designed to help you master the fundamentals and advanced concepts of Apache Airflow through practical, hands-on exercises.

You can enroll in the course using the following link: [Enroll in Apache Airflow Bootcamp](https://www.udemy.com/course/apache-airflow-bootcamp-hands-on-workflow-automation/?referralCode=F4A9110415714B18E7B5).

I would greatly appreciate it if you could take the time to review the course and share your feedback. Additionally, please consider sharing this course with your colleagues who may benefit from it.

--

--