This new language model can be your future programmer
ChatGPT a new language model is an improved version of GPT-3, and possibly, gives us a glimpse of what GPT-4 will be capable of when it’s released early next year (as outlined above). rumor). With ChatGPT it is possible to have an actual conversation with the model, referencing earlier points in the conversation.
I want to try it out if I can use this model as a fellow programmer that I can give some guidance and it generates the code for me. Of course, I’ll still test those pieces of code again, but at least I won’t have to write them from scratch.
So, in this post, I describe how I used ChatGPT to create a simple sentiment analysis app from scratch. The application will run on an EC2 instance and use the modern NLP model from the Hugging Face Model Hub. The results are amazing
I had to try and tweak the prompt a few times to get the results I wanted. That being said, it usually only takes small tweaks to get the desired results. My prompts and the code generated by the model can be found in this GitHub repo !
Let’s start!
First check if Chatgpt is available:
Let’s dive into the details, at the request of this companion!
Create a CloudFormation Template for an 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. Thus, our first task for ChatGPT is to create a CloudFormation template that sets up an EC2 instance:
The instructions are pretty specific (I had to look up the AMI ID myself, for example), but I was still quite surprised when it came up with a near-perfect CF sample. Note that ChatGPT also puts some disclaimer at the end of the code! Full code generated by Chatgpt:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | AWSTemplateFormatVersion: '2010-09-09' Description: A simple CloudFormation template for creating a security group and launching an EC2 instance Resources: SecurityGroup: Type: AWS::EC2::SecurityGroup Properties: GroupName: chatgpt-demo-sg GroupDescription: Security group for chatgpt-demo EC2 instance SecurityGroupIngress: - IpProtocol: tcp FromPort: 22 ToPort: 22 CidrIp: 0.0.0.0/0 - IpProtocol: tcp FromPort: 8501 ToPort: 8501 CidrIp: 0.0.0.0/0 EC2Instance: Type: AWS::EC2::Instance Properties: InstanceType: g4dn.xlarge ImageId: ami-09f85944838b438c5 SecurityGroups: - Ref: SecurityGroup UserData: 'Fn::Base64': | #!/bin/bash # Activate the pre-built pytorch environment source activate pytorch_p36 # Install the libraries "transformers" and "streamlit" via pip pip install transformers streamlit |
Note that the EC2 instance is actually not getting the name we specified in the prompt. Another “error” in this sample is that the preinstalled pytorch environment on the AMI is called “pytorch” not “pytorch_36”. Let’s fix it by replacing the environment name.
Ok, running this line started the Stack creation using the CF template. After a few minutes, we see the EC2 instance in action. Note that the template created a security group and the EC2 instance uses that security group as specified:
SSH into the EC2 instance to see if the packages we need are installed
Streamlit app
Now we need an application that runs on Streamlit and analyzes the sentiment of the text. To my surprise, this was even easier than I expected:
Again, nice disclaimer at the end. Entire code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import streamlit as st from transformers import pipeline # Set the title of the application st.title("A sentiment analyser written by ChatGPT") # Create the input text field text = st.text_input("Enter some text to analyse:") # Use the Hugging Face Pipeline API to create a sentiment classifier sentiment_classifier = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english") # Evaluate the text entered by the user and display the result if text: result = sentiment_classifier(text)[0] st.write(f"Predicted sentiment: {result['label']}") |
This looks really good to me, let’s try to run this without modification. Copy and paste this code into a file on EC2 called ‘ app.py ‘. But how to run Streamlit application again? Ask my ‘colleague’:
I have Streamlit installed, so go ahead and run ‘streamlit run app.py ‘:
Conclusion
This has been very exciting and the possibilities are endless. I will try to experiment more with this model in the future!
References
https://towardsdatascience.com/i-used-chatgpt-to-create-an-entire-ai-application-on-aws-5b90e34c3d50