Overview of available implementations

  • To build both related item and personalized recommendation feeds for a Python-based social video platform using Firebase, you need a hybrid architecture. Firestore manages your data, while Python handles the heavier embedding generation and personalized matrix calculations.

Architecture Overview

[Firebase Client / App]
       │             ▲
1. Play Video     6. Deliver Feeds
       ▼             │
[Firestore DB] ◄──► [Python Backend / Cloud Run]
  • video_metadata     • Vector Search via findNearest()
  • user_interactions  • Personalized Feed Generator (Colab / TFLite)

This system relies on semantic similarity. You convert video descriptions and tags into vector embeddings using Python, store them in Cloud Firestore, and use native vector search to find related items. [1, 2, 3]

1. Generate and Store Embeddings (Python Backend) [4]

Run a Python worker (on Cloud Run or a server) using the google-cloud-firestore SDK and a model like text-embedding-3-small from the google-genai library to create and save vectors when a video is uploaded.

from google import genaifrom google.cloud import firestore

Initialize clientsdb = firestore.Client()ai_client = genai.Client()

def process_video_embedding(video_id, title, description, tags):
    # Combine text context
    combined_text = f"Title: {title}. Description: {description}. Tags: {', '.join(tags)}"
    
    # 1. Generate vector embedding
    response = ai_client.models.embed_content(
        model="text-embedding-3-small",
        contents=combined_text
    )
    embedding_vector = response.embeddings[0].values
    
    # 2. Store vector natively in Firestore
    video_ref = db.collection("videos").document(video_id)
    video_ref.update({
        "embedding": firestore.Vector(embedding_vector),
        "title": title,
        "tags": tags
    })

When a user opens a video, your Python backend uses the native find_nearest vector search parameter to fetch the top 5 semantically related videos.

from google.cloud.firestore_v1.base_vector_query import DistanceMeasure
def get_related_videos(current_video_embedding):
    videos_ref = db.collection("videos")
    
    # Native Firestore vector search
    query = videos_ref.find_nearest(
        vector_field="embedding",
        query_vector=firestore.Vector(current_video_embedding),
        distance_measure=DistanceMeasure.COSINE,
        limit=5
    )
    
    docs = query.stream()
    return [doc.to_dict() for doc in docs]

Part 2: “Picked For You” (Personalised Homepage Feed)

Personalisation for short-form social videos requires tracking user interaction metrics (watch time, likes, skips). You train a lightweight collaborative filtering model in Python, export it to Firebase Cloud Storage, and sync it for fast delivery.

1. Database Structure for Interactions

Create an interaction collection in Firestore to capture user habits:

interactions (Collection)
 └── interaction_id (Document)
      ├── user_id: "user_abc123"
      ├── video_id: "video_xyz987"
      ├── watch_percentage: 0.85  # Crucial for video apps (e.g. watched 85% of video)
      ├── liked: true
      └── timestamp: SERVER_TIMESTAMP

2. Model Training & Export (Python Pipeline)

Periodically export interaction documents into a Python training loop. You can use matrix factorization frameworks like scikit-learn or TensorFlow.

import pandas as pdfrom sklearn.decomposition import TruncatedSVDfrom google.cloud import storageimport pickle
def train_and_upload_recommendation_model():
    # 1. Fetch data from Firestore interaction collection
    docs = db.collection("interactions").stream()
    data = [doc.to_dict() for doc in docs]
    df = pd.DataFrame(data)
    
    # Calculate an interaction score
    # Score = watch_percentage + (2.0 if liked else 0)
    df['score'] = df['watch_percentage'] + df['liked'].apply(lambda x: 2.0 if x else 0.0)
    
    # 2. Create User-Item Pivot Matrix
    pivot_table = df.pivot(index='user_id', columns='video_id', values='score').fillna(0)
    
    # 3. Train Collaborative Filtering Matrix Factorization (SVD)
    svd = TruncatedSVD(n_components=20, random_state=42)
    matrix_features = svd.fit_transform(pivot_table)
    
    # 4. Serialize model and upload to Firebase Storage
    with open("model.pkl", "wb") as f:
        pickle.dump((svd, pivot_table, matrix_features), f)
        
    bucket = storage.Client().bucket("your-firebase-storage-bucket")
    blob = bucket.blob("models/personalized_feed_model.pkl")
    blob.upload_from_filename("model.pkl")

3. Serving the Personalized Feed

When generating a personalized homepage feed, load the serialized components into memory on your Python api. Map the specific user_id to their index, multiply the latent vectors, and output the highly scored video IDs.

Optimization Strategy for Social Videos

Because social video recommendations require fast adjustments based on immediate interactions, use this hybrid operational pattern:

RequirementApproachTech Stack
High Velocity Cold StartServe videos matching the user’s explicit category selections mixed with trending metrics.Firestore Compound Queries (orderBy(“views”, “desc”))
Instant Video-to-Video ContinuityFind text-based matching vectors via the backend when an organic playlist runs dry.Firestore find_nearest() + Cosine Distance
Algorithmic Chronological FeedPeriodically recalculate matrix user affinities offline and store recommendations in user sub-collections.Python Scheduled Tasks + Firestore Batched Writes