I'm upgrading from v1.1 to v1.3 and I can't figure...
# ask-for-help
e
I'm upgrading from v1.1 to v1.3 and I can't figure out how to achieve the following. I have customized the
bentoml.Service
class - so my code at the moment looks like
Copy code
svc = MyCustomService(...)

@svc.api(input=..., output..., doc=...)
async def classify(...)

    return ...
I wonder, now with the class based services, I guess that I can still wrap the methods with the save decorator - but I'm wondering how would I connect it to the class itself? For exmaple, from your XGBoost docs:
Copy code
@bentoml.service(
    resources={"cpu": "2"},
    traffic={"timeout": 10},
)
class CancerClassifier:
    # Retrieve the latest version of the model from the BentoML Model Store
    bento_model = bentoml.models.get("cancer:latest")

    def __init__(self):
        self.model = bentoml.xgboost.load_model(self.bento_model)

        # Check resource availability
        if os.getenv("CUDA_VISIBLE_DEVICES") not in (None, "", "-1"):
            self.model.set_param({"predictor": "gpu_predictor", "gpu_id": 0})  # type: ignore (incomplete XGBoost types)
        else:
            nthreads = os.getenv("OMP_NUM_THREADS")
            if nthreads:
                nthreads = max(int(nthreads), 1)
            else:
                nthreads = 1
            self.model.set_param(
                {"predictor": "cpu_predictor", "nthread": nthreads}
            )

    @bentoml.api
    def predict(self, data: np.ndarray) -> np.ndarray:
        return self.model.predict(xgb.DMatrix(data))
How would I replicate this with a custom service?
If it is of any interest, I customize the class because I need to edit the contents of the OpenAPI spec - so I override the
def openapi_spec()
to adhere to API standards of my org
j
Hello @Elior Cohen, i am not sure if I understood your question, are you looking to create a base class?
e
mmm
Previously, before 1.3
we explicitly created service classes
but no as the new bentoml interface is class based the
bentoml.Service
class is being created internally by the
@benotml.service
dcorator
Since I have a custom implementation of
bentoml.Service
I wonder how should I incorporate my custom class (e.g.
class MyCustomService(benotml.Service)
with the new bentoml interface
I guess if I could get an example from you, assuming we have a custom service (
class MyCustomService(bentoml.Service)
), of how to implement the
service.py
shown in my first comment while using the custom service - that will be enough
j
oh, you can do
Copy code
@bentoml.service
class CustomService:
    ...


class ChildService(CustomService):
    ...
e
@Jian Shen Yap I did exactly that - now using my new childservice - how do I apply it this example
Copy code
@bentoml.service(
    resources={"cpu": "2"},
    traffic={"timeout": 10},
)
class CancerClassifier:
    # Retrieve the latest version of the model from the BentoML Model Store
    bento_model = bentoml.models.get("cancer:latest")

    def __init__(self):
        self.model = bentoml.xgboost.load_model(self.bento_model)

        # Check resource availability
        if os.getenv("CUDA_VISIBLE_DEVICES") not in (None, "", "-1"):
            self.model.set_param({"predictor": "gpu_predictor", "gpu_id": 0})  # type: ignore (incomplete XGBoost types)
        else:
            nthreads = os.getenv("OMP_NUM_THREADS")
            if nthreads:
                nthreads = max(int(nthreads), 1)
            else:
                nthreads = 1
            self.model.set_param(
                {"predictor": "cpu_predictor", "nthread": nthreads}
            )

    @bentoml.api
    def predict(self, data: np.ndarray) -> np.ndarray:
        return self.model.predict(xgb.DMatrix(data))
There are two usages here of the service the
@bentoml.service
and the
@bentoml.api
- how do I apply the child service I created to the class and method decorators?
@Jian Shen Yap can you help me figure this out?
j
Hello, sorry i let this slip past my backlog. Maybe i did not understood your use case completely, would doing
Copy code
class ChildClassifier(CancerClassifier):
    ...
works for you?
e
Okay - and now how 1. How do pass
resources
and
traffic
as in the XGBoost example (the class level decorator) 2. How do I decorate the
predict
function which now has
@bentoml.api
- what should I put instead of it? Because internally
@bentoml.api
creates a different
Service
and we want to use the new one (
ChildClassifier
)
All I need is simply an example of how to implement the code in the XGBoost docs, with a custom Service class - very simple The most clear response you can give me, is to show a working example of the code I linked to in the XGBoost BentoML docs that uses a custom Service class
@Jian Shen Yap