Real-time classified ads enrichment

A practical example of creating a pipeline to enrich classified ads in real-time using AI, GlassFlow, Langchain, and Redis.

Learn how to build a data processing pipeline to enrich classified ads in real-time. We'll use GlassFlow to process ads, enrich them with additional information, categorize them using Langchain and OpenAI, and store the enriched ads in Redis for quick and advanced search.

Pipeline components

Data source

The initial data source is a platform where users post classified ads. This could be an existing marketplace like Craigslist or eBay, or a custom-built website. GlassFlow can also continuously ingest new and updated ads from a custom website using a Webhook connector. For the sake of the demo, we generate and ingest sample ads from predefined JSON files using Python SDK. Sample input looks like this:

[
  {
      "id": "1",
      "user_id": "1",
      "title": "Math tutor for high school",
      "description": "I amd a Math graduate and graduated with honors in the university of Barcelona I can give you theoretical classes and practical labs where we go through problems and their solutions"
  },
  {
    "id": "2",
    "user_id": "2",
    "title": "Sage Barista Pro portafilter machine",
    "description": "My Sage Barista Pro is for sale because I'm moving.\n- regularly descaled\n- rinsed almost daily with coffee degreaser\n- very good condition, fine scratches, but nothing major and no dents\n- 4sieves and accessories included\n- heats up very quicly thanks to its thermoblock\n\nif you have any questions, just write a short message :-)",
    "images": [
      {
        "format": "image/jpg",
        "path": "data/ad2_image1.jpg"
      }
    ]
  },
  ...
]

Transformation

The transformation function in GlassFlow processes the ingested ads. It uses AI to analyze images for tags, descriptions, and generated content descriptions.

Data sink

The enriched ad data is stored in Redis, a high-performance in-memory database. The enriched data in Redis is made available to the frontend of the classified ads platform, ensuring that users see the most relevant and informative ads.

Setting Up the Pipeline with GlassFlow

You will use the GlassFlow WebApp to create a data processing pipeline.

Prerequisites

To start with this setup, you need a free GlassFlow account.

Sign up for a free account

Step 1. Log in to GlassFlow WebApp

Navigate to the GlassFlow WebApp and log in with your credentials.

Step 2. Create a New Pipeline

Click on "Create New Pipeline" and provide a name. You can name it "classified-ads-enrichment".

Step 3. Configure a Data Source

Select the "SDK" data source to configure the pipeline to use Python SDK. For demo purposes, we ingest data from a JSON file with examples of classified ads using a Python script called producer.py.

Step 4. Define the Transformer

generateThe transformation function uses Langchain and OpenAI to analyze, categorize, and add more information to the ad, generate tags based on ad images, and write a summary for each ad.

Copy and paste the following transformation function code into the transformer's built-in editor.

Note that the handler function is mandatory to implement in your code. Without it, the transformation function will not be successful.

By default, the transformer function uses a free OpenAI API key provided by GlassFlow.

You can replace it with your API key too. To do so:

  1. Have an OpenAI API account.

  2. Create an API key.

  3. Set the API key in the transformation code.

Step 5. Choose a transformer dependency

The transformation function uses openai external library in the code, so we need to choose it from the Dependencies dropdown menu. GlassFlow includes the external library in the function deployment and runtime. Read more about Python dependencies for transformation.

Step 6. Configure a Data Sink

Select "SDK" as a data sink to configure the pipeline to use Python SDK. Code provided in Python sink_connector.py for sending enriched classified ads to Redis. You use Docker to run a Redis instance and a sink connector Python script for Redis.

Step 7. Confirm the Pipeline

Confirm the pipeline settings in the final step and click "Create Pipeline".

Step 8. Copy the Pipeline Credentials

Once the pipeline is created, copy its credentials such as Pipeline ID and Access Token.

Send and consume data from the pipeline

Prerequisites

To complete this part you'll need the following:

Installation

  1. Clone the glassflow-examples repository to your local machine:

    git clone https://github.com/glassflow/glassflow-examples.git
  2. Navigate to the project directory:

    cd use-cases/classified-ads-data-enrichment
  3. Create a new virtual environment:

    python -m venv .venv && source .venv/bin/activate
  4. Install the required dependencies:

    pip install -r requirements.txt

Create an environment configuration file

Add a .env file in the project directory and add the following configuration variables:

PIPELINE_ID=your_pipeline_id
PIPELINE_ACCESS_TOKEN=your_pipeline_access_token

Replace your_pipeline_id and your_pipeline_access_token with appropriate values obtained from your GlassFlow account.

Run the Docker

Run the below Docker command in a terminal:

docker compose up

Docker compose will spin up the sink connector service that listens to the GlassFlow pipeline and saves the documents to the Redis database continuously. Also, it runs Redis Stack with Redis server and Redis insight.

Run the producer

Run producer.py Python script in a terminal to publish sample ads data to the GlassFlow pipeline:

python producer.py

Once your Docker containers and producer Python script are running, you can access the Redis Insight UI to view the enriched ads.

See the enriched data on Redis Insight

Open your web browser and navigate to the following URL(https://localhost:8001) in your browser and have a look at the enriched data.

You should see the output with enriched ads.

Summary

By following these steps, you created the pipeline with GlassFlow, processed and enriched classified ads in real time. You can also experiment with updating input ads data and searching for ads based on different attributes to see how the enriched information enhances the search experience. Explore other use cases to see how GlassFlow can revolutionize your data processing needs.

Last updated

Logo

© 2023 GlassFlow