Serve machine learning model using GraphQL subscription.
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
- FastAPI + Ariadne to build the GraphQL backend.
- React + Apollo Client + react-webcam to build the frontend.
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
Unlike REST APIs, for GraphQL normally there is only 1 endpoint for which you need to define the schema for queries, mutations, and subscriptions.