While Baseten provides PostgreSQL tables for storing information from model runs, you may want to store your data in your own databases. Plus, for data ill-suited for relational databases, like large files and key-value pairs, you’ll want to use more appropriate technologies. With data connections, you can read and write from external sources directly from your Baseten account.
This tutorial covers writing files to Amazon S3 via a data connection. To get started, all you’ll need is:
Deploy the photo restoration starter application
If you don’t already have a copy of the photo restoration starter application deployed on your Baseten account, let’s take 30 seconds to get you a fresh copy. Head to the applications tab and click “Try it” under the “Photo restoration with GFP-GAN” starter app.
Play around with it a bit to see how it works, as we’ll be extending its functionality after creating the data connection.
Establish a data connection
From the dropdown menu in the upper-left corner, select Data. On the data page, click on “Connections” and then click “Add connection.”
Name your connection and select S3 from the dropdown. This creates a connection to your account’s S3 resources. You’ll pick which bucket to use later, when you write code that accesses the data connection.
Enter your AWS credentials, save your data connection and return to the application builder.
Update application code
With the data connection all ready to go, it’s time to modify the code to access it. From the application builder, find the file main.py.
At the top of main.py, import the two libraries we’ll need. Both are already available, no need to install any packages.
Replace the function `save_restored_image` in main.py with the following code. Make sure to replace `Your S3 connection` with the name you gave your data connection and `your-bucket-name` with your S3 bucket’s name.
This modified function adds these four lines to the middle:
First, it accesses the data connection, then brings in the restored image and converts it into a file-like object. The image was already stored in Baseten during the model invocation, which is why we use a get request to grab the image content. With the AWS Python SDK, it uploads the image to your S3 bucket as a file.
Head back to the application’s view and try running the photo restoration app. Within a few seconds, you’ll not only see the restored photo in the UI, but you’ll also see it in your S3 bucket!
New in June: Full-stack superpowers
We’re excited to share some of this month’s biggest wins toward empowering data scientists to build and maintain full-stack applications that deliver real value.
How to add an interactive ML demo to your landing page
Embedding a live, interactive demo of your product in a landing page can boost conversion rates. Let’s say you’re tasked with adding a ML model demo to the homepage of a machine learning API provider. Let's consider how to overcome the technical and organizational challenges of embedding this demo.