pythontidymodelsvetiver

Python Vetiver with fastText


Inspired by Python Vetiver model - use alternative prediction method I want to deploy my https://fasttext.cc/ model with the help of vetiver and pins. Unfortunately the solution in the link does not work for me. After I wrote my FastTextHandler class I cannot even execute the following line:

from vetiver.handlers.base import VetiverHandler

Does this error arise due to the version update 0.1.5 -> 0.1.6 ?

So I changed from VetiverHandler to BaseHandler, then I get the following code:

from vetiver.handlers.base import BaseHandler

class FastTextHandler(BaseHandler):
    def __init__(model, ptype_data):
        super().__init__(model, ptype_data)

   def handler_predict(self, input_data, check_ptype):
        """
        Define how to make predictions from your model
        """
        prediction = self.model.predict(input_data)
        
        return prediction


custom_model = FastTextHandler(model,"hello")

But this throws the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_3423/1130715213.py in <module>
     11         return prediction
     12
---> 13 custom_model = FastTextHandler(model, "hello")

TypeError: __init__() takes 2 positional arguments but 3 were give

I don't understand why the model object is interpreted as two positional arguments. Is it due to the fastText structure and how can it be fixed / used with vetiver?

Thank you very much in advance!

Best, M.


Solution

  • Your __init__ function is missing a self argument.

    class FastTextHandler(BaseHandler):
        def __init__(self, model, ptype_data):
            super().__init__(model, ptype_data)
    

    etc.

    Looking at this code, you are passing in the string "hello" into the ptype_data argument. To avoid later problems with this, custom_model = FastTextHandler(model, data) (or leave your data as None), and then add the model name "hello" when creating your deployable model object, such as VetiverModel(custom_model, "hello").