How to Build and Deploy a ChatGPT-powered Sentiment Analysis FastAPI Microservice to AWS
In this post, we’ll build a sentiment analysis application using ChatGPT and FastAPI, and deploy it to AWS using AWS App Runner. We'll use ChatGPT to generate and document the application code, write tests, and setting up the deployment. Let's get started!
Building the FastAPI Application
To create our sentiment analysis app, we'll use ChatGPT, an AI language model that can help speed up development by generating code based on prompts. We asked ChatGPT to provide us with instructions to build a sentiment analysis application, and it generated the necessary code and deployment steps.
You can find the generated code in this GitHub repository:
This repository contains the FastAPI app, a unit test, and a README documentation file.
Testing the App Locally
Before deploying the app, it's a good idea to test it locally. Run the pytest
command in your terminal to execute the tests.
If the tests pass, you can start the application locally using:
uvicorn main:app --host 0.0.0.0 --port 8000
Now, navigate to http://127.0.0.1:8000/docs
in your browser to access the Swagger UI, where you can test the sentiment analysis endpoint.
Deploying the App to AWS
With a working and tested FastAPI app, it's time to deploy it to AWS. We'll use AWS App Runner, a service that allows you to deploy your app by simply pointing it to your GitHub repository or Docker container image.
To deploy the app, follow these steps:
- Sign in to the AWS Management Console and open the AWS App Runner console.
- Click "Create service" and connect your GitHub account.
- Select the repository and branch containing your FastAPI app.
- Configure the build settings to use the Python 3 runtime.
- Set the required commands for installing dependencies and starting the app (these were also generated by ChatGPT).
- Configure resources, environment variables, auto-scaling, health checks, permissions, and observability (using AWS X-Ray) as needed.
- Review your settings and click "Create and deploy" to deploy your app.
After deployment, AWS App Runner will provide you with a secure API endpoint for your FastAPI app. You can access the Swagger UI at the /docs
path of the deployed endpoint to test the sentiment analysis functionality.