Also Is there any way of passing in a context obje...
# ask-for-help
r
Also Is there any way of passing in a context object in the constructor or any other hooks to the class. The idea is to use this for as key object store and also to provide common abstractions to the object
Copy code
@bentoml.service
class Summarization:
    def __init__(self, context) -> None:
        self.context = context # <=====================
        self.model = ... model ...
        pass

    @bentoml.api
    def summarize(self, text: str = EXAMPLE_INPUT) -> str:
        logger = self.context.logger() # <===== logger is just an example. It could be a logger which is different depending on local, prod
        logger.info("In Summarize")
        return self.model.predict()
The idea is construct common object like monitoring, logging, feature stores through this context object and to pass it to the
Summarization
Service
f
You can get access to the context with
Summarization.context
, (Note,
self.__class__.context
doesn’t work, since they are different object.
Summarization != self.__class__ or type(self)
)
the context provides an attribute
.state
which is a dictionary to store arbitrary global states
r
Thanks @Frost Ming. That's good to know.