In this tutorial, you'll learn how to leverage Bagel's APIs to efficiently manage your RAW assets, enabling streamlined data access and manipulation capabilities.
Prerequisites
To ensure that all the code in this tutorial runs properly, follow these steps:
Make sure you have Python installed.
Install the Bagel Python client by running:
pipinstallbagelML
Step 1: Creating an Asset
Before we can begin managing our asset, we need to create a new asset within Bagel. This can be achieved using the Bagel client:
import osimport bagelfrom getpass import getpass# Create the asset using the API key from environment variablesclient = bagel.Client()# Copy & Paste the API Key from https://bakery.bagel.net/api-keyDEMO_KEY_IN_USE =getpass("Enter your API key: ")# Set environment variableapi_key = os.environ['BAGEL_API_KEY']= DEMO_KEY_IN_USEpayload ={"dataset_type":"RAW",# e.g "RAW", "VECTOR", "MODEL""title":"",# e.g "Apple""category":"inpute category",# e.g "Cat1", "Cat2""details":"input details",#e.g "testing""tags": ["Optional, input tags"],# e.g ["AI", "DEMO", "TEST"]"user_id":"input user id"# e.g "345281182308180743453"}# Calling the create asset methodresponse = client.create_asset(payload, api_key)print(response)
dataset_type: Defines the type of dataset being created. This could be a RAW, VECTOR, or MODEL dataset. They will be treated differently depending on the type.
title: The title or name for the dataset, e.g., "Apple".
category: Classifies the dataset into a specific category such as Cat1 or Cat2 to help with organization and retrieval.
details: Additional descriptive details or notes about the dataset.
tags: A list of optional tags to further classify the asset for easier searching, e.g., ["AI", "DEMO", "TEST"].
user_id: The unique identifier for the user creating the asset, e.g., "345281182308180743453".
Note: This code prompts the user to enter their API key securely (without displaying it on the screen) and then sets it as an environment variable named BAGEL_API_KEY
Here we can find our created dataset on the Bakery!
Step 2: Uploading Data to the Asset
With our asset created, we can now begin uploading data to it. Bagel's client provides a method to upload files:
import osimport bagelfrom getpass import getpass# Create the asset using the API key from environment variablesclient = bagel.Client()# Copy & Paste the API Key from https://bakery.bagel.net/api-keyDEMO_KEY_IN_USE =getpass("Enter your API key: ")# Set environment variableapi_key = os.environ['BAGEL_API_KEY']= DEMO_KEY_IN_USEasset_id ="input dataset id"# e.g "eR57yd73b-7gt4-4a71-b0fd-afKJUIY6578Ge"file_path ="input file path"# e.g "product_catalog.csv" or "path/dir/file.csv"# Calling the file upload methodresponse = client.file_upload(file_path, asset_id, api_key)print(response)
Step 3: Managing the Asset
Bagel's APIs provide functionality for managing the lifecycle of assets, including retrieving asset information, updating asset properties, and deleting assets when they are no longer needed.
Retrieving Asset Information
To retrieve information about an asset by asset id, use the following method:
import osimport bagelfrom getpass import getpass# Create the asset using the API key from environment variablesclient = bagel.Client()# Copy & Paste the API Key from https://bakery.bagel.net/api-keyDEMO_KEY_IN_USE =getpass("Enter your API key: ")# Set environment variableapi_key = os.environ['BAGEL_API_KEY']= DEMO_KEY_IN_USEasset_id ="input dataset id"# e.g "eR57yd73b-7gt4-4a71-b0fd-afKJUIY6578Ge"response = client.get_asset_by_id(asset_id, api_key)print(response)
The response will print all details of the specified asset.
Updating Asset Properties
To update an asset's properties, such as the title or category, use the following method:
import osimport bagelfrom getpass import getpass# Create the asset using the API key from environment variablesclient = bagel.Client()# Copy & Paste the API Key from https://bakery.bagel.net/api-keyDEMO_KEY_IN_USE =getpass("Enter your API key: ")# Set environment variableapi_key = os.environ['BAGEL_API_KEY']= DEMO_KEY_IN_USEasset_id ="input dataset id"# e.g "eR57yd73b-7gt4-4a71-b0fd-afKJUIY6578Ge"# Payload for updating the assetpayload ={"title":"input title",# e.g "Clothe""category":"input category",# e.g "Cat3","details":"input details",# e.g "Updated dataset description.","tags": ["input tags"] # e.g ["Updated", "Tags"] or [ ]}# Calling update asset methodresponse = client.update_asset(asset_id, payload, api_key)print(response)
Deleting an Asset
When an asset is no longer needed, delete it using the following method:
import osimport bagelfrom getpass import getpass# Create the asset using the API key from environment variablesclient = bagel.Client()# Copy & Paste the API Key from https://bakery.bagel.net/api-keyDEMO_KEY_IN_USE =getpass("Enter your API key: ")# Set environment variableapi_key = os.environ['BAGEL_API_KEY']= DEMO_KEY_IN_USEasset_id ="input dataset id"# e.g "eR57yd73b-7gt4-4a71-b0fd-afKJUIY6578Ge"# Calling delete asset methodresponse = client.delete_asset(asset_id, api_key)print(response)
The dataset will no longer be accessible on the Bakery after deleting.