In my last post (click here to read it), I showed you how to create the application using mock AWS using Motto which simulates AWS via an SDK. Now, I'm going to show you how to do it via AWS.
The application uses AWS S3 to store learning resources and AWS Lambda to process user input. The recommender employs TF-IDF vectorization and cosine similarity for ranking.
Let's do it, shall we?
1. Set Up AWS S3
Create an S3 bucket:
aws s3 mb s3://learning-paths-data
Upload the learning resources JSON:
echo '{
"resources": [
{"title": "Introduction to AWS", "tags": "AWS, Cloud Computing"},
{"title": "Deep Learning on AWS", "tags": "AWS, AI, Deep Learning"}
]
}' > resources.json
aws s3 cp resources.json s3://learning-paths-data/
2. Write the Lambda Function
Create a Lambda function to process input and calculate similarity:
import json
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
def lambda_handler(event, context):
user_input = event['query']
s3 = boto3.client('s3')
obj = s3.get_object(Bucket='learning-paths-data', Key='resources.json')
resources = json.loads(obj['Body'].read().decode('utf-8'))['resources']
titles, tags = zip(*[(r['title'], r['tags']) for r in resources])
tfidf = TfidfVectorizer().fit_transform(tags + [user_input])
scores = cosine_similarity(tfidf[-1], tfidf[:-1]).flatten()
recommendations = [titles[i] for i in scores.argsort()[-3:]]
return {'recommendations': recommendations}
Deploy the function and expose it via API Gateway.
3. Update Streamlit App
Modify the app to query the Lambda function:
import requests
def main():
user_input = st.text_input('What do you want to learn about AWS?')
if st.button('Recommend'):
response = requests.post('<API Gateway URL>', json={'query': user_input})
recommendations = response.json()['recommendations']
for rec in recommendations:
st.write(rec)
if __name__ == '__main__':
main()
These projects demonstrate how to leverage AWS services to build scalable, reliable applications. Try them via my Github repo here Thanks for reading!
Top comments (0)