ML Client/Server Using gRPC in Python

Mohammed AL-Ma'amari
Good Audience
Published in
4 min readOct 4, 2018

--

A machine learning model by its own is not enough, we have to deploy it to use it efficiently.

Imagine that we want to make a microservice to predict home pricing, this service will be deployed in a phone app or in a web app, then we have to make a client/server to serve the clients.

In this article we will learn how to make a simple gRPC client/server to respond to the clients’ requests.

Steps:

1- Write the service to be served.

2- Make a proto file to define the messages and services.

3- Use the proto file to generate gRPC classes for Python

4- Create the server.

5- Create the client .

Step 1 : Write the Service :

In our case, the service is predicting house pricing, I have already trained a model to do that, I will use it in this example.

We used joblib to load the trained model.

This model has 5 input arguments (features of the house that we want to predict its sale price)

Step 2 : Make the Proto File :

gRPC uses proto instead of Json.

If you want to learn about proto files read :

Here, we did not give values to the features, those numbers indicate the order of serializing the features.

Step 3 : Generate gRPC classes for Python :

Open the terminal, change the directory to be in the same folder that the proto file is in.

To generate the gRPC classes we have to install the needed libraries first:

#Install gRPC :
python -m pip install grpcio
#To install gRPC tools, run:python -m pip install grpcio-tools googleapis-common-protos

for more details, visit (How to install gRPC for Python)

Now, run the following command:

python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. ML_example.proto

This command used ML_example.proto file to generate the needed stubs to create the client/server.

The files generated will be as follows:

ML_example_pb2.py — contains message classes

  • ML_example_pb2.Features for the input features
  • ML_example_pb2.Prediction for the prediction price

ML_example_pb2_grpc.py — contains server and client classes

  • ML_example_pb2_grpc.PredictServicer will be used by the server
  • ML_example_pb2_grpc.PredictStub the client will use it

Step 4 : Creating the Server :

The server will import the generated files and the function that will handle the predictions.

Then we will define a class that will take a request from the client and uses the prediction function to return a respond.

The request gives us the five features ,the respond is a prediction.

After that, we will use add_PredictServicer_to_serverfunction from (ML_example_pb2_grpc.py)file that was generated before to add the class PredictSevicer to the server .

Step 5: Creating the Client :

In the client file we will do the following :

  • Open a gRPC channel
  • Create a stub
  • Create a request message
  • Use the stub to call the service

We generated a hundred samples of house features to test the prediction function, we passed the generated samples as arguments to the prediction function.

We used two timers to measure the performance of our client/server.

Now its time to try our client/server

First, put all the needed files in one directory, for example :

ML_example/
├── predict_sale_price.py # Prediction function
|
├── ML_example.proto # protobuf definition file
|
├── ML_example_pb2_grpc.py # generated class for server/client
├── ML_example_pb2.py # generated class for message

| # a server to expose the function
├── house_price_prediction_server.py
| # The client
└── house_price_prediction_client.py

Now run the server :

After that, in another terminal window, run the client :

You can see the results yourself, It worked like a charm.

Now, after we learned how to make a client/server, we can deploy any ML model we make, we can make a server and use it in phone apps or web apps.

What Next :

  • Now you can make your own ML model and deploy it.
  • Learn more about gRPC (you can find some helpful references below)
  • Try to use Go language to make the client and see how will it effect the performance

References :

You can follow me on Twitter @ModMaamari

--

--

I am a computer engineer | I love machine learning and data science and spend my time learning new stuff about them.