Serving ML Model using GraphQL Subscription

alpha2phi
3 min readMay 9, 2021

Serve machine learning model using GraphQL subscription.

Photo by h heyerlein on Unsplash

Overview

In previous articles, I have used FastAPI + WebSocket + React to serve machine learning models. As machine learning models getting more complicated, the time to come out with predictions also takes longer. Using WebSocket provides an asynchronous method for the server to update the client with the prediction when the evaluation is completed.

GraphQL Subscription is a feature that allows a server to send data to its clients when a specific event happens and is usually implemented with WebSocket.

In this article, let’s explore using GraphQL to serve machine learning models. I am going to use

For the machine learning model, I am going to use Photo2Cartoon model from PaddleGAN.

The frontend application takes a photo of your face and sends the image to the GraphQL endpoint for processing. As soon as the processing is done, the processed image is sent back to the frontend application.

For GraphQL beginners, go through this tutorial to find out more.

Backend — FastAPI + Ariadne

Schema

Unlike REST APIs, for GraphQL normally there is only 1 endpoint for which you need to define the schema for queries, mutations, and subscriptions.

--

--

alpha2phi

Software engineer, Data Science and ML practitioner.