Giter Site home page Giter Site logo

basegrpc's Introduction

Base gRPC

PyPI version CircleCI License: GPL v3

It is a universal gRPC service framework suitable for model as a service. The data is input-output like JSON. Different services only need to add different model class files. At the same time, extension also becomes easy, as it can be inherited or replaced.

  • see the Quickstart section below to get started with the basic functionality
  • see the /examples folder for more in-depth py walkthroughs

Dependencies

  • grpcio >= 1.57.0
  • grpcio_health_checking >= 1.56.0
  • protobuf >= 4.24.3
  • scikit_learn >= 1.2.1

Installation

Package Installation

You can install the latest published version from PyPI using pip:

pip install basegrpc

Quickstart

Prerequisites

  • Input / output: please refer to the struct of gRPC
  • Model file: /examples/logic.mdl
  • Model class files: the file name and class name must be the same(Logic.py and class Logic)
import pickle
import numpy as np


class Logic(object):
    def __init__(self):
        self.model_path = "logic.mdl"
        # load model
        self.model = self.load_model()

    def load_model(self):
        with open(self.model_path, "rb") as pickle_file:
            model = pickle.load(pickle_file)
        return model

    def predict(self, data) -> list:
        result = np.array([])
        try:
            data_X = data['data_X']
            result = self.model.predict(data_X)
        except Exception as e:
            print(e)
        return result.tolist()

Start Service

from basegrpc.grpc_service import BaseService
from basegrpc.grpc_module import ml_module


def run():
    ml_module.module_name = "LogicV1"
    grpc_service = BaseService()
    grpc_service.run()


if __name__ == '__main__':
    run()

# output:
# [PID 11732] gRPC load module: LogicV1
# [PID 11732] gRPC server start...

Test

from basegrpc.grpc_client import GrpcClient


if __name__ == "__main__":
    grpc_client = GrpcClient(bind_address='127.0.0.1:50051')
    data = {"data_X": [[5.1, 3.5]]}
    response = grpc_client.request(request_data=data)
    print("response: ", response)

    health_status = grpc_client.health_check()
    print("health status: ", health_status)
    
# output:
# [0.0]
# status: SERVING

basegrpc's People

Contributors

erratico avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.