/flinksql-mlflow

A flinksql-mlflow-pytorch implementation

Primary LanguagePython

FlinkSQL with MLFlow and PyTorch

This repository illustrates how to use an event streaming platform (Apache Kafka) in combination with a realtime event stream processor (Apache Flink) using a SQL syntax to predict on trained PyTorch models which are distributed via a S3-compatible object store (MinIO) and tracked/served with a platform for model lifecycle management (MLflow).

Prerequisites:

  • Docker installed

Running:

  1. Run the setup profile to download the fruit360 dataset and build a Flink image with Python installed

docker compose --profile setup build

  1. Run the dataset container to intialize the shared volume:

docker compose run dataset

  1. Run the mlflow profile to start MLFlow and its dependencies (Minio, PostgreSQL)

docker compose --profile mlflow up -d

  1. Run the training profile to train the PyTorch when mlflow becomes available (check http://localhost:5001 in the browser)

docker compose --profile training up

  1. Run the fullstack profile to run the remainder of the stack (zookeeper, kafka, jobmanager, taskmanager, sqlclient)

docker compose --profile fullstack up -d

tail the logs of the model_deployment container to see when it is ready to receive connections:

docker compose logs -f model_deployment

if the model_deployment container fails to install its dependencies because of a connection reset (unstable WiFi) just rerun the command docker compose up -d model_deployment

  1. Now that the full stack is running, visit in the browser:

mlflow: http://localhost:5001 model_deployment: http://localhost:5002 (or run something from the invocation.http file) minio: http://localhost:9000 jobmanager: http://localhost:8091 kafka-ui: http://localhost:9003

  1. Go to the kafka-ui in the browser and select the image-stream topic. Produce a message which is a base64 encoded image, e.g.:

/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgr/wAARCABkAGQDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0f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