Getting Started with Supabase: A Guide to Using Postgres Database with Python for AI/ML
Supabase is an increasingly popular open-source backend-as-a-service (BaaS) that makes it easy for developers to set up and manage a PostgreSQL database, user authentication, blob storage, and other key infrastructure. Launched in 2020, Supabase has been gaining traction as an open-source alternative to Google‘s Firebase that avoids vendor lock-in.
For data scientists and ML engineers, Supabase offers a compelling platform for building AI-powered applications. The combination of a managed Postgres database, integrated APIs, and supporting tools for tasks like ETL and dashboarding allows teams to focus on core ML logic vs infrastructure.
In this in-depth guide, we‘ll walk through how to get started using Supabase to power a Python application, with a focus on AI/ML use cases. Topics we‘ll cover include:
- How Supabase fits into the modern AI/ML ecosystem
- Key statistics on Supabase adoption and growth
- A step-by-step tutorial for training and deploying an ML model with Supabase
- Evaluating the strengths and tradeoffs of Postgres for AI/ML workloads
- Emerging best practices and design patterns for AI/ML apps on Supabase
Let‘s dive in!
Supabase and the AI/ML Ecosystem
The sheer number of tools and platforms for building AI/ML applications has exploded in recent years. From managed notebooks to feature stores to ML ops pipelines, data scientists and developers have a dizzying array of options to sort through.
Supabase aims to simplify this landscape by providing a unified and opinionated stack for the “boring” parts of app infrastructure. At its core is a managed PostgreSQL database, which offers a number of advantages as a data layer for ML:
- Postgres‘ support for unstructured JSON enables storing raw event data and model inputs/outputs
- Built-in vector similarity search with the pgvector extension
- Strong consistency and ACID transactions avoid training-serving skew
- Robust security and role-based access control
- Rich ecosystem of BI, data science, and ML tools
Building on this foundation, Supabase layers APIs and supporting services that address adjacent ML infrastructure needs:
- The async Postgres Views API allows transforming raw data into features
- Edge functions can host model inference endpoints
- Object storage buckets can store large datasets, model artifacts, and results
- Lightweight dashboards can monitor model performance and data drift
While managed ML platforms like SageMaker or Vertex AI aim to be a complete solution for the model development lifecycle, Supabase offers a more modular and lightweight approach. Teams can leverage the Postgres foundation and supporting tools where it adds value, while plugging in best-of-breed solutions for areas like experiment tracking, hyperparameter tuning, and ML pipelines.
Supabase Adoption and Growth
Since launching in 2020, Supabase has seen significant adoption and growth. Over 80,000 developers have used the platform, with over 100,000 databases created.
High-profile companies using Supabase include Vercel, Sega, Momentive (SurveyMonkey), and Picsart. Supabase also powers popular open-source projects like Logto (auth), Dashibase (admin panel), and Plz (serverless ML platform).
In December 2022, Supabase raised a $80M Series B, bringing their total funding to over $180M. The company plans to use the funds to expand their enterprise offering, including SOC2 compliance, SSO, and dedicated support.
Tutorial: Training and Deploying an ML Model on Supabase
To illustrate a concrete AI/ML workflow on Supabase, let‘s walk through the process of training and deploying a sentiment analysis model for user reviews.
We‘ll assume you already have a Supabase project set up. If not, follow their guide to create a new project.
Step 1: Load training data into Postgres
For this example, we‘ll use the popular IMDB movie review dataset. Download the CSV file from Kaggle and create a new Supabase bucket to store it:
from supabase import create_client
supabase = create_client(YOUR_SUPABASE_URL, YOUR_SUPABASE_KEY)
bucket_name = "movie_review_data"
file_path = "imdb_movie_reviews.csv"
res = supabase.storage.create_bucket(bucket_name, public=True)
with open(file_path) as ff:
res = supabase.storage.from_(bucket_name).upload(file_path, ff)
Next, let‘s create a movie_reviews table in Supabase to load the CSV data into:
query = """
CREATE TABLE IF NOT EXISTS movie_reviews (
review_id INT PRIMARY KEY,
review_text TEXT,
sentiment INT
);
"""
supabase.execute(query)
query = f"""
COPY movie_reviews (review_id, review_text, sentiment)
FROM ‘https://{supabase_project}.supabase.co/storage/v1/object/public/{bucket_name}/{file_path}‘
WITH (FORMAT CSV, HEADER);
"""
supabase.execute(query)
Step 2: Prepare features and train model
With our training data in Postgres, we can pull it into a Pandas DataFrame to prepare features and train an initial model:
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.linear_model import LogisticRegression
query = """
SELECT review_text, sentiment
FROM movie_reviews
"""
res = supabase.execute(query)
df = pd.DataFrame(res.data)
vectorizer = CountVectorizer(max_features=1000)
vectors = vectorizer.fit_transform(df.review_text)
model = LogisticRegression()
model.fit(vectors, df.sentiment)
Step 3: Save and deploy the model
Once we have a trained model, we can pickle it and save to Supabase storage:
import pickle
model_path = "sentiment_model.pkl"
with open(model_path, "wb") as f:
pickle.dump(model, f)
with open(model_path, "rb") as ff:
res = supabase.storage.from_(bucket_name).upload(model_path, ff)
To expose the model as a live inference endpoint, we can create an Edge Function in Supabase:
import pickle
from supabase import create_client
supabase = create_client(YOUR_SUPABASE_ENDPOINT, YOUR_SERVICE_KEY)
model_url = f"https://{supabase_project}.supabase.in/storage/v1/object/public/{bucket_name}/{model_path}"
res = supabase.storage.from_(bucket_name).download(model_path)
model_data = res.content
model = pickle.loads(model_data)
def predict_sentiment(request):
text = request.json.get("text")
if not text:
return "Missing text input", 400
vector = vectorizer.transform([text])
sentiment = model.predict(vector)[0]
return {"sentiment": int(sentiment)}
Deploy the function:
supabase functions deploy predict_sentiment
That‘s it! We now have an auto-scaling inference endpoint we can call from our application to analyze sentiment of new reviews:
curl -X POST ‘https://your-project.functions.supabase.co/predict_sentiment‘ \
--header ‘Content-Type: application/json‘ \
--data ‘{"text":"This movie was great!"}‘
Step 4: Productionize and monitor
For a real production use case, we‘d want to build a more robust training pipeline and ML ops setup. Some key considerations:
- Use feature views and a feature store layer for consistent feature definitions
- Set up experiment tracking to manage model versions and hyperparameters
- Create labeled evaluation datasets to monitor model performance over time
- Implement CI/CD to automatically retrain and deploy model versions
- Dashboard feature and performance metrics for the model
Many of these workstreams can be managed directly with Supabase. Features can be implemented as materialized Postgres views. Evaluation datasets can be stored as additional Postgres tables. Metrics can be calculated with SQL and visualized using the built-in Supabase Dashboard.
For experiment tracking and pipeline orchestration, you‘ll likely want to leverage more specialized tools like MLflow, Kubeflow, or Metaflow. But the core data flows can still be powered by the underlying Supabase Postgres database.
Evaluating Postgres for AI/ML Applications
Postgres has a number of characteristics that make it well-suited as a database for AI/ML applications:
- Ability to store and query unstructured data like JSON, text, images, etc.
- Declarative abstractions for feature engineering via SQL
- Support for full-text search and fuzzy matching
- Geospatial datatypes and indexes
- Transactional consistency to avoid training-serving skew
- Strong security and permissioning for sensitive datasets
- Flexibility to use as both an analytical and transactional database
Some potential challenges and tradeoffs to consider:
- Horizontal scalability is more difficult vs NoSQL databases
- Less optimized for extremely high-volume or high-velocity streaming data
- Certain data formats like video may be better suited for a separate lakehouse architecture
Supabase does add some compelling enhancements to Postgres for AI/ML as well:
- The pgvector extension enables vector similarity search and embeddings
- Edge functions can be used for online inference
- Policy templates secure and authorize access to different datasets
- Realtime subscriptions stream database updates for model retraining
Emerging AI/ML Best Practices on Supabase
As an emerging space, best practices for building AI/ML applications on Supabase are still being established. But here are some patterns we‘re seeing from the community:
Embrace declarative feature engineering with SQL. Postgres views provide a powerful abstraction for defining reusable features without needing a separate compute layer.
Use foreign tables to create a feature store. Live features can be declaratively synced to a separate feature table, versioned, and joined into data science notebooks and training pipelines.
Implement ML-aware access controls. Leverage Postgres RLS and Supabase auth policies to enforce granular permissions and protect sensitive model features, embedding tables, etc.
Avoid DIY model serving. Use Supabase edge functions with autoscaling, HTTPS, and API authorization for managed and secure inference endpoints.
Minimize data movement. With Postgres as a central store, you can keep feature values, model results, and predictions in a single database to avoid complex data copying and syncing.
Conclusion
Supabase is a powerful and flexible platform for building AI and ML applications. By combining a managed Postgres database with integrated APIs, dashboards, and supporting services, it allows data scientists and developers to focus on high-value modeling and application logic vs undifferentiated infrastructure.
In this guide, we covered:
- The role of Supabase in the modern AI/ML ecosystem
- Adoption and growth statistics for the Supabase platform
- A soup-to-nuts tutorial for training and deploying a sentiment analysis model
- Key benefits and tradeoffs of Postgres for AI/ML workloads
- Emerging best practices for AI/ML applications on Supabase
While not a complete end-to-end ML platform, Supabase provides a flexible and scalable foundation that can be combined with more specialized tools for the model development lifecycle. We‘re excited to see the novel AI/ML use cases the community builds on Supabase in the years ahead!
To dive deeper, check out the official Supabase documentation and join the conversation on GitHub and Discord.