The world’s leading publication for data science, AI, and ML professionals.

I Used ChatGPT to Create an Entire AI Application on AWS

This new language model could be the pair programmer of your choice going forward

Image by author - created with Stable Diffusion
Image by author – created with Stable Diffusion

What is this about?

Two days ago OpenAI released ChatGPT, a new language model which is an improved version of GPT-3 and, possibly, gives us a peek into what GPT-4 will be capable of when it is released early next year (as is rumoured). With ChatGPT it is possible to have actual conversation with the model, referring back to previous points in the conversation.

I wanted to try out if I could use this model as a pair programmer that I can give some instructions and it produces the code for me. I would still double-check those code snippets, of course, but at least I won’t have to write them from scratch anymore.

So in this blog post I describe how I used ChatGPT to create a simple sentiment analysis application from scratch. The app should run on an EC2 instance and utilise a state-of-the-art NLP model from the Hugging Face Model Hub. The results were astonishing 😮

Full disclaimer – I had to try and refine the prompts a few times to get the results I wanted. That being said, it usually took only minor refinements to get the desired results. My prompts and the code generated by the model can be found in this GitHub repo.

Why is this important?

You’re joking, right?

Let’s get started

Ok, let’s get started! Let’s first check if ChatGPT is willing to help:

Image by author
Image by author

Ok, this is a promising start 😊 Let’s dive into the details, as requested by our companion!

Creating a CloudFormation template for the EC2 instance

We want to run this application on an EC2 instance, but we don’t want to click through the AWS console to create this EC2 instance. Therefore, our first task for ChatGPT is to create a CloudFormation template that sets up the EC2 instance:

Image by author
Image by author

The instruction are rather specific (I had to look up the AMI ID myself, for example), but still I was quite surprised that it came out with an almost perfect CF template. Note that ChatGPT also puts some disclaimers at the end of the code:

Image by author
Image by author

The entire code produced by ChatGPT:

Note that the EC2 instance actually doesn’t receive the name we specified in the prompt. But ok, let’s live with that for now. Another "bug" in this template is that the pre-installed Pytorch environment on the AMI is called "pytorch" not "pytorch_36". Let’s fix that by replacing the environment name.

Now, how do we actually run this? Let’s ask ChatGPT:

Image by author
Image by author

Ok, running this line kicked off the Stack creation using the CF template. After a few minutes we see the EC2 instance is up and running. Note that the template created a security group and the EC2 instance used that security group like specified:

Image by author
Image by author

Let’s also SSH into the EC2 instance to see if the packages we need are installed:

Image by author
Image by author

Looks like everything is set up for the Streamlit app 😊

Streamlit application

Now we need an application that runs on Streamlit and that analyses the sentiment of text. To my surprise this was even easier than I expected:

Image by author
Image by author
Image by author
Image by author

Again, nice disclaimer at the end.

The entire code:

This actually looks good to me, let’s try to run this without modifications. Copy and pasting this code into a file on EC2 called "app.py". But how do we run Streamlit apps again? Let’s ask our "colleague":

Image by author
Image by author

We already have Streamlit installed so let’s go ahead and run "streamlit run app.py":

Image by author
Image by author

Seems all good!

Testing the application

Now comes the moment of truth. We plug in the URL streamlit exposed and see if the app runs.

Image by author
Image by author

Wow, ChatGPT just built an entire text sentiment app just with our instructions 🤯

Conclusion

I’m very much speechless. This has been so much fun and the possibilities are endless. I will try to experiment more with this model going forward and would also love to hear what you have built with it. Please comment below!


Related Articles