Navigating the World of Streamlit and AWS GenAI services: An Introduction
Tags: streamlit, tutorial, beginners, ai
Published: 2023-12-22 11:29:52

Hey everyone! 

So, I recently got hands-on with Streamlit during a GenAI project, and it brought a whole new vibe to my coding game.

This blog is crafted for the curious minds, the aspiring developers, and the seasoned data scientists who seek a bridge between their code and the interactive web. We'll dissect the elements that make Streamlit an attractive option for building data-driven applications and demystify how it integrates seamlessly with advanced services like AWS GenAI.

Before we delve into the nuts and bolts of Streamlit in subsequent posts, let's set the theoretical foundation. Understanding the 'why' and 'how' will equip you with the knowledge to appreciate the 'what' that Streamlit can do for you.

So, whether you're new to web development or looking to streamline your data visualization processes, join me on this journey through the Streamlit landscape. 

## Introduction to Streamlit

### What is Streamlit and why is it a preferred choice for building web applications in data science and machine learning? 


Streamlit is an open-source Python library that allows you to create a stunning-looking web applications for data science and machine learning with only a few lines of code. 

**Advantages**

- no front-end (html, js, css) experience or knowledge is required,
- you don't need to spend days or months to create a web app, you can create a really beautiful machine learning or data science app in only a few hours or even minutes,
- compatible with the majority of Python libraries (e.g. pandas, matplotlib, seaborn, plotly, Keras, PyTorch, SymPy(latex)),
- less code is needed to create amazing web apps,
- data caching simplifies and speeds up computation pipelines.

### What are some common use cases of Streamlit in combination with AWS GenAI Services? 

Combining Streamlit with AWS GenAI Services opens up a range of powerful and innovative use cases, particularly in the realms of data science, machine learning, and AI-driven applications. Some common use cases include.

**Image recognition and classification:**

- **Medical image analysis**
 Streamlit applications can be built for medical professionals to upload and analyze medical images (e.g., X-rays, MRIs) using AWS services like Amazon Rekognition Custom Labels or Amazon Comprehend Medical.

- **Content moderation**
 Create a content moderation tool that allows users to upload images, and AWS Rekognition can automatically detect and filter out inappropriate content.

**Natural language processing (NLP)**

- **Sentiment Analysis Dashboard**
 Build a sentiment analysis dashboard using Streamlit and AWS Comprehend to analyze and visualize sentiment in social media posts or product reviews.

- **Text Summarization** 
 Create a tool that summarizes lengthy documents or articles using AWS Textract or Amazon Comprehend's summarization capabilities.

**Data Visualization and Enhancement:**


- **Data Augmentation** 
 Streamlit can be used to visualize datasets, and AWS GenAI Services like Amazon Data Augmentation can automatically generate variations of your data to enhance model training.

- **Data Transformation**
 Use Streamlit to display data transformation processes, especially when preparing data for machine learning models using AWS Glue, SageMaker Data Wrangler, or Athena.

**Voice and Speech Processing:**


- **Voice Assistant**
 Build a voice-controlled Streamlit app with AWS Polly for text-to-speech conversion and Lex for natural language understanding. This can be used in customer support applications or for creating voice-guided tutorials.

**Chatbots and Conversational Interfaces:**


- **AI-Powered Chatbots**
 Create a conversational interface using Streamlit with AWS Lex for chatbot interaction, making it easy for users to get information or perform actions via chat.

**Custom Machine Learning Models:**

- **Custom Models with SageMaker**
 Develop Streamlit applications that leverage custom machine learning models trained on AWS SageMaker and allow users to input data for predictions.

## Integration of Streamlit with AWS

### How can you set up a Streamlit application on an AWS server, such as EC2 or an container?

Deploying a Streamlit application on an AWS server is a strategic choice for ensuring your data-driven web app is accessible, scalable, and performant. Amazon Web Services (AWS) offers a range of services, including EC2 instances and container solutions, that provide a robust foundation for hosting applications. 

#### Streamlit on EC2 instance

- install streamlit on the EC2 instance:
```
pip install streamlit
```
- run your streamlit app on the EC2 instance: 
```
streamlit run your_app.py
```
- access your streamlit app: 
Open your web browser and navigate to the public IP address or DNS of your EC2 instance, specifying the appropriate port (e.g., http://your-ec2-ip:8501)

#### Streamlit on Docker 

- build a docker image from a dockerfile:
```
docker build -t streamlit . 
```
> ``📝`` The -t flag is used to tag the image. Here, we have tagged the image streamlit. 

- run the docker container:
```
docker run -p 8501:8501 streamlit
```
> ``📝`` The -p flag publishes the container’s port 8501 to your server’s 8501 port. 



If all went well, you should see an output similar to the following:

```
docker run -p 8501:8501 streamlit

  You can now view your Streamlit app in your browser.

  URL: http://0.0.0.0:8501
```


### What are the steps to integrate AWS services with a Streamlit application?


Integrating AWS services with a Streamlit application involves several steps, and the specific steps may vary depending on the AWS services you want to use and the requirements of your application. Here's a general guide to get you started:

- **set up AWS account**, 
- **install required libraries** (make sure you have the necessary Python libraries installed like boto3 for AWS SDK and other libraries based on your application's requirements)
```
pip install streamlit boto3
```
- **AWS SDK configuration** (set up AWS SDK (boto3) with your credentials. You can either configure it with environment variables or using the AWS CLI.)
```
import boto3

# Configure AWS SDK
aws_access_key_id = 'your_access_key_id'
aws_secret_access_key = 'your_secret_access_key'
aws_region = 'your_region'

boto3.setup_default_session(
    aws_access_key_id=aws_access_key_id,
    aws_secret_access_key=aws_secret_access_key,
    region_name=aws_region
)
```
- **integrate AWS services** (depending on your application's requirements, you might integrate services like Amazon S3 for storage or other AWS services)
```
import boto3
import streamlit as st

# Function to upload file to S3
uploaded_file = st.sidebar.file_uploader("Choose a file", type=["txt","jpg","png","pdf"])
def upload_to_s3(file_path, bucket_name, object_name):
        s3 = boto3.client('s3')
        try:
            s3.upload_file(file_path, bucket_name, object_name)
            return True
        except Exception as e:
            st.error(f"An error occurred: {e}")
            return False

st.info(f"Updating file '{filename}' to S3 bucket '{bucket_name}'...")
        if upload_to_s3(temp_file.name, bucket_name, object_name):
            st.success(f"File '{filename}' uploaded successfully to S3 bucket: s3://{bucket_name}/{object_name}")        
        else:
            st.error(f"Failed to upload '{filename}' to S3 bucket '{bucket_name}'")
```

> ``📝`` **securely manage credentials** (avoid hardcoding credentials in your code, instead, employ _IAM roles_ and permissions for better security, leverage _AWS Secrets Manager _or _Parameter Store_ to securely store sensitive information)  


> ``📝`` **streamlit application** (develop your Streamlit application as you normally would, utilizing _Streamlit widgets_ to interact with your application and display relevant information)  

> ``📝`` **testing and deployment** (test your application locally to ensure everything is working as expected)  


> ``📝`` **monitoring and logging** (use _CloudWatch_ or other relevant AWS services)  







## Utilizing AWS GenAI Services in a Streamlit App

Generative AI, a subset of artificial intelligence, empowers applications to create new content, such as images, text, or even entire narratives, by learning patterns from existing data. Integrating Generative AI services into a Streamlit app adds a creative and dynamic dimension to user interactions. Whether generating text, images, or other forms of content, these services open up innovative possibilities for enhancing user engagement.

### How can AWS GenAI Services be incorporated into a Streamlit app?

Integrating GenAI services into your Streamlit applications can elevate user experiences and provide advanced functionality. Whether it's image recognition, natural language processing, data visualization, or voice interactions, GenAI services can empower your applications with intelligence and automation. As shown in the examples below, these services can help streamline processes, increase user engagement, and make your Streamlit apps more competitive in today's tech landscape.

**Image Recognition with GenAI Vision**

GenAI Vision is a powerful service that can recognize objects, scenes, and text in images. Let's say you're building an e-commerce application that allows users to upload product images. You can enhance this functionality by integrating GenAI Vision to automatically identify and tag objects within the images.

In the example above, GenAI Vision has identified and tagged objects in an uploaded image, making it easier for users to categorize and search for products.

**Natural Language Processing with GenAI Language**

GenAI Language can be a valuable addition to Streamlit applications that involve text analysis or generation. Imagine you're creating a sentiment analysis tool for social media posts. By incorporating GenAI Language, you can provide more accurate sentiment analysis and even generate suggestions for improving text.

In this example, GenAI Language has analyzed a user's tweet and provided sentiment analysis along with suggestions for a more positive tone.

**Enhancing Data Visualization with GenAI Data**

If your Streamlit application deals with data visualization, GenAI Data can help improve the quality and appeal of your charts and graphs. Whether it's generating realistic data for testing or enhancing visualizations with intelligent data transformation, GenAI Data can save time and make your app more engaging. GenAI Data has transformed raw data into a visually appealing chart, making it easier for users to grasp the information.

**Voice Commands and Responses with GenAI Voice**

Adding voice commands and responses to your Streamlit application can greatly enhance accessibility and user interaction. GenAI Voice can help create conversational interfaces, making your app more user-friendly.


In this example, a Streamlit application uses GenAI Voice to respond to voice commands, providing a hands-free and convenient experience for users.

## Benefits and Limitations

Using Streamlit in combination with AWS GenAI Services provides several benefits, particularly when compared to more comprehensive solutions like AWS Amplify. Here are some of the major advantages:

**Ease of Use and Rapid Prototyping**

**_Streamlit:_** It is incredibly user-friendly for data scientists and developers who are more comfortable with Python. Streamlit allows for rapid prototyping; you can create a working prototype with interactive features quickly.
**_AWS Amplify:_** While Amplify provides a full-stack development environment, it has a steeper learning curve, especially for those who are not as familiar with JavaScript or full-stack development practices.

**Flexibility and Customization**

**_Streamlit:_** It offers a high degree of flexibility, allowing developers to integrate various Python libraries and AWS services as needed. You can tailor your app specifically to your data visualization and processing needs.
**_AWS Amplify:_** Amplify is opinionated in its approach and is designed to work well within the AWS ecosystem. This can sometimes limit customization options, particularly for complex or unique backend requirements.

**Cost Efficiency**

**_Streamlit:_** Running Streamlit on an AWS instance incurs the cost of the instance itself. However, there are no additional charges for the Streamlit framework, which can be a cost-efficient solution for lightweight applications.
**_AWS Amplify:_** Amplify has a pricing model that includes costs for backend services, hosting, and other features. While it offers a free tier, costs can grow as your application scales or as you use more of the Amplify suite.

**Python-Centric Development**

**_Streamlit:_** Ideal for Python developers, Streamlit is a natural choice for teams that are already invested in Python for data science and analytics.
**_AWS Amplify:_** Amplify is more focused on JavaScript/TypeScript and the broader AWS service ecosystem, which can be a barrier for Python-centric teams.

**Integration with AWS Services**

**_Streamlit:_** While not as tightly integrated as Amplify, Streamlit can still easily connect to AWS services, including GenAI. This is especially powerful when leveraging AWS's machine learning services for data-intensive applications.
**_AWS Amplify:_** Amplify is designed to work seamlessly with AWS services, offering built-in components and CLI tools to connect to AWS backends.

**Community and Support**

**_Streamlit:_** It has a growing community, especially among data scientists. The support for data-centric operations is strong, with many resources available.
**_AWS Amplify:_** Amplify is backed by AWS and has extensive documentation and community support, including first-party support from AWS.

**Deployment and Hosting**

**_Streamlit:_** Can be deployed on various AWS services such as EC2, Elastic Beanstalk, or even a container service like ECS or EKS, giving developers flexibility in their deployment strategy.
**_AWS Amplify:_** The deployment is tightly integrated with the AWS ecosystem, which might be more straightforward but also ties your deployment closely to AWS.

##Conclusion

While this blog has delved into the theory behind Streamlit, showcasing its benefits, and how it seamlessly integrates with services such as AWS GenAI, the true value of Streamlit is best appreciated through practical application. In my next blog post, I will guide you through a hands-on example that I’ve been crafting. Stay tuned for our next adventure, where we'll put theory into practice and witness the alchemy of Streamlit in action. Until then, may your data be insightful, and your coding journey be smooth.