Learn to Build a Photo Captioning Streamlit Application with Amazon Bedrock LLMs

In this article, we will walk through how to create an AI-powered photo captioning app. First, let's create our app.
Creating LLM integrated Steamlit app
You can download the code for the app from the following GitHub repository:
https://github.com/Vivek-Rathee/PhotoCaptioner
But before deploying, let's understand what's happening inside the AIPCWA.py file.
First, you import the necessary packages mentioned in the file. You will need to pip install these packages if you don't already have them on your system.
Then set the page configurations for Streamlit, and load the CLIP model and processor. Next, add an AWS profile that has the necessary permissions to access AWS Rekognition and Bedrock, and create clients for the respective services.
Add a modelID of the LLM you would like to use, which you can find on the AWS Bedrock models page.
Create a function that converts the uploaded images to bytes and calls Rekognition to detect labels.
Next, refine the caption based on the user's choice of platform. Create the body as a dictionary, then serialize it to JSON and encode it as bytes. Read and decode the response, extract the refined caption from the response, and remove any prefatory text.
At last, create your Streamlit app, and with it, you have a functioning code for your AI-enhanced photo captioning app.
Test locally using VS Code with "streamlit run yourAppName.py". You'll need to configure the profile you mentioned in the code. You can use the AWS extension for configuring your profile in VS Code.
Hosting your Streamlit app on your custom domain.
Create an EC2 instance with the following configuration:
OS: Amazon AMI Linux 2023
Network/Security Group: Allow internet access on ports 80, 443, and 8501 (port on which streamlit runs)
Storage: 100GB (as dependencies need space)
System Update and Python Environment Setup
Switch to the root user:
sudo suUpdate all installed packages to their latest version:
yum update -yUpgrade the operating system to the latest release:
yum upgrade -yCreate a Python virtual environment:
python3 -m venv venvActivate the Python virtual environment:
source venv/bin/activate
Install Python Packages
Install the Python package installer (pip):
yum install -y python3-pipInstall specific versions of PyTorch and related libraries:
python3 -m pip install torch==1.13.1 torchvision==0.14.1 torchaudio==0.13.1 --index-url https://download.pytorch.org/whl/cpuInstall Streamlit, ignoring any previously installed versions:
python3 -m pip install --ignore-installed streamlitInstall Boto3, the Amazon Web Services (AWS) SDK for Python:
python3 -m pip install boto3Install Pillow, a Python Imaging Library (PIL) fork:
python3 -m pip install pillowInstall the Transformers library:
python3 -m pip install transformers
Git and Project Setup
Install Git:
yum install gitClone the PhotoCaptioner repository:
git clone https://github.com/Vivek-Rathee/PhotoCaptioner.gitNavigate to the project directory:
cd PhotoCaptioner
AWS Configuration
Configure AWS CLI with your user profile:
aws configure --profile YourUserProfileFollow the prompts to enter your AWS credentials:
Access Key ID [None]: Your Access Key ID AWS Secret Access Key [None]: Your Secret Access Key Default region name [None]: us-east-1 Default output format [None]: json
Setting up HTTP nginx web server
Install nginx:
yum install nginx -yInstall Certbot:
yum install certbot -yStop nginx:
sudo systemctl stop nginxObtain SSL certificates for your domain:
sudo certbot certonly --standalone -d yourdomain.comClear and edit nginx configuration file:
sudo sh -c 'echo -n > /etc/nginx/nginx.conf' nano /etc/nginx/nginx.confEdit the nginx configuration file:
user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; include /etc/nginx/conf.d/*.conf; map $http_upgrade $connection_upgrade { default upgrade; '' close; } upstream backend { server 127.0.0.1:8501; keepalive 64; } server { listen 80; server_name aipc.vivekrathee.com; location / { return 301 https://$host$request_uri; } } server { listen 443 ssl; server_name aipc.vivekrathee.com; ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem; location / { proxy_pass http://backend; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; client_max_body_size 20M; # Adjust this value as necessary } } types_hash_max_size 2048; types_hash_bucket_size 128; }
Restart and Enable nginx
Restart nginx:
sudo systemctl restart nginxEnable nginx to start on boot:
sudo systemctl enable nginx
Create the Shell Script
Create a new file named
run_streamlit.shin the same directory:nano run_streamlit.shAdd the following content to the file:
while true do nohup streamlit run AIPCWA.py & wait $! sleep 5 doneMake the shell script executable:
chmod +x run_streamlit.sh
Using tmux to Run the Script
Install tmux:
sudo yum install tmuxCreate a new tmux session named
streamlit_session:tmux new -s streamlit_sessionRun the shell script in the background:
nohup ./run_streamlit.sh > streamlit.log 2>&1 &Detach from the tmux session by pressing
Ctrl+bfollowed byd.
Conclusion: After following the above prcess you should have an AI powered Photo Captioning app running on your custom domain (like the one you can use here https://aipc.vivekrathee.com/).


