DEV Community

Cover image for Create Your Own Personalised AWS Learning Experience with Streamlit
omar-steam
omar-steam

Posted on

Create Your Own Personalised AWS Learning Experience with Streamlit

In my journey towards learning about AWS and machine learning/AI, I've decided to create a simple yet powerful AWS Learning Path Recommender using Streamlit, natural language processing or NLP via a mock S3 environment. This application will be able to suggest AWS learning paths based on user input.  

So, let's get into it!

Prerequisites

Before we start, make sure Python is installed and create a new project folder. Then, install the libraries below:

 

pip install streamlit boto3 moto scikit-learn

Enter fullscreen mode Exit fullscreen mode

Step 1: Setting Up the Mock S3

First, we define a function to create a mock S3 using Moto; this will be used to simulate AWS S3 without connecting to AWS.

import boto3
from moto import mock_s3
import json

def setup_mock_s3():
    s3 = boto3.resource("s3", region_name="us-east-1")
    bucket_name = "mock-learning-paths"
    s3.create_bucket(Bucket=bucket_name)
    data = {
        "resources": [
            {"title": "Introduction to AWS", "tags": "AWS, Cloud Computing, Basics"},
            {"title": "Deep Learning on AWS", "tags": "AWS, Deep Learning, AI"},
            {"title": "NLP with SageMaker", "tags": "AWS, NLP, Machine Learning"},
            {"title": "Serverless with AWS Lambda", "tags": "AWS, Serverless, Lambda"},
        ]
    }
    s3.Bucket(bucket_name).put_object(Key="mock_resources.json", Body=json.dumps(data))
    return bucket_name

Enter fullscreen mode Exit fullscreen mode

Step 2: Recommendation Function

Next, we will define a function that, given a user's input, will make suggestions for learning paths by utilizing some NLP:

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

def recommend_learning_path(user_input, bucket_name):
    s3 = boto3.client("s3", region_name="us-east-1")
    obj = s3.get_object(Bucket=bucket_name, Key="mock_resources.json")
    data = json.loads(obj['Body'].read().decode('utf-8'))

    resources = data["resources"]
    titles = [resource["title"] for resource in resources]
    tags = [resource["tags"] for resource in resources]

    corpus = tags + [user_input]
    vectorizer = TfidfVectorizer()
    tfidf_matrix = vectorizer.fit_transform(corpus)
    similarity = cosine_similarity(tfidf_matrix[-1], tfidf_matrix[:-1])

    scores = similarity.flatten()
    ranked_indices = scores.argsort()[::-1]

    recommendations = [titles[i] for i in ranked_indices[:3]]
    return recommendations

Enter fullscreen mode Exit fullscreen mode

Step 3: Streamlit Interface

Let's now design the interface of our application using Streamlit:

import streamlit as st

st.title("AWS Learning Path Recommender")

user_input = st.text_input("What do you want to learn about AWS?", "I want to learn about AWS and AI")

if st.button("Get Recommendations"):
    with mock_s3():
        bucket_name = setup_mock_s3()
        recommendations = recommend_learning_path(user_input, bucket_name)

    st.subheader("Recommended Learning Path:")
    for i, rec in enumerate(recommendations, 1):
        st.write(f"{i}. {rec}")

Enter fullscreen mode Exit fullscreen mode

Step 4: Putting It All Together
Combine all the code snippets into a single Python file named 'app.py':

import streamlit as st
import boto3
from moto import mock_s3
import json
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

# [Insert setup_mock_s3 function here]

# [Insert recommend_learning_path function here]

st.title("AWS Learning Path Recommender")

user_input = st.text_input("What do you want to learn about AWS?", "I want to learn about AWS and AI")

if st.button("Get Recommendations"):
    with mock_s3():
        bucket_name = setup_mock_s3()
        recommendations = recommend_learning_path(user_input, bucket_name)

    st.subheader("Recommended Learning Path:")
    for i, rec in enumerate(recommendations, 1):
        st.write(f"{i}. {rec}")

Enter fullscreen mode Exit fullscreen mode

Step 5: Run the App
To start the Streamlit app, open a terminal, change the directory to your project folder and run:

streamlit run app.py

Enter fullscreen mode Exit fullscreen mode

This will boot the Streamlit server and open the app in your default web browser.

How It Works
The app creates a mock S3 bucket populated with some example AWS learning resources.

This application recommends the most relevant resources, using TF-IDF in addition to cosine similarity, by entering the interests of the user in learning and clicking "Get Recommendations." The top 3 recommendations will be shown to the users.

Conclusion.

This simple application uses Streamlit to glue together the NLP techniques with AWS services, admittedly mocked, to create an interactive learning path recommender. You could extend this example by integrating the actual AWS services, adding more resources, or using more sophisticated recommendation algorithms.

This is a very simple example and can be improved a lot for production. Remember, security, scalability, and user experience are major concerns while developing applications for the real world.

Thank you so much for reading this!

Top comments (0)