Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot find custom model handler #137

Open
deadsoul44 opened this issue Dec 26, 2024 · 4 comments
Open

Cannot find custom model handler #137

deadsoul44 opened this issue Dec 26, 2024 · 4 comments
Assignees

Comments

@deadsoul44
Copy link

We exactly followed this doc to save a custom model:

https://docs.snowflake.com/en/developer-guide/snowflake-ml/model-registry/custom-models

But we get a handler error as described in this issue: #135

TypeError("Your input type to custom model is not currently supported")

Could you please help us to find a way to define a custom handler?

@sfc-gh-xjiang
Copy link

Hey @deadsoul44 , thanks for reporting this issue and we removed this assertion in the next release.

Apart from the the assertion, is it possible for you to provide a minimal reproducible example so that we can help investigate the issue?

Thanks,
Xinyi

@sfc-gh-xjiang sfc-gh-xjiang self-assigned this Jan 9, 2025
@deadsoul44
Copy link
Author

deadsoul44 commented Jan 9, 2025

We were trying to save a model that was trained using PerpetualBooster:

https://github.com/perpetual-ml/perpetual

Perpetual is still not available on the Snowflake conda channel, although we made a request almost two months ago.

It should be stated in docs that the package should be available on the Snowflake Anaconda channel.

Or there should be a way to define a custom handler.

@sfc-gh-xjiang
Copy link

sfc-gh-xjiang commented Jan 9, 2025

Got it, I wrote an example that may work in your use case, can you try?

import pickle
import os

from snowflake.ml.model import custom_model
from sklearn.svm import SVC
import pandas as pd


# Step 1: Train the model
model = SVC() <<please_replace_with_your_model>> # Example model, please replace with your own model
data = pd.DataFrame({'feature1': [1, 2, 3], 'feature2': [4, 5, 6]})
target = [1, 2, 3]
model.fit(data, target)

# Step 2: Save the model to a file
os.makedirs('path_to_save', exist_ok=True)

with open('path_to_save/model.pkl', 'wb') as f:
    pickle.dump(model, f)

# Step 3: Define a ModelContext with the path to the saved model
mc = custom_model.ModelContext(
    model_file='path_to_save/model.pkl',
)

# Step 4: Define a custom model class that loads the model and makes prediction
class ExampleBringYourOwnModel(custom_model.CustomModel):
    def __init__(self, context: custom_model.ModelContext) -> None:
        super().__init__(context)
        with open(context['model_file'], 'rb') as f:
            self.model = pickle.load(f)

    @custom_model.inference_api
    def predict(self, input: pd.DataFrame) -> pd.DataFrame:
        # Return predictions and probabilities as a DataFrame
        return pd.DataFrame({'output': self.model.predict(input)})

# Example usage:
# Initialize the custom model and run predictions
bring_your_own_model = ExampleBringYourOwnModel(context=mc)
result = bring_your_own_model.predict(data)
print(result)


# Snowpark for Python
from snowflake.ml.registry import Registry
session = <<create_your_session>>

# Step 5: Log the custom model to the registry
reg = Registry(session=session, database_name=session.get_current_database(), schema_name=session.get_current_schema())

mv = reg.log_model(bring_your_own_model,
            model_name="my_model",
            version_name="version1",
            conda_dependencies=["scikit-learn"],  #<<please_include_dependency_here>>
            comment="My Custom ML Model Pipeline",
            sample_input_data=data)

# Step 6: Run the model in the registry
output_df = mv.run(data)
print(output_df)

Please let me know if you've met any issues
Xinyi

@sfc-gh-xjiang
Copy link

Hey @deadsoul44 ,

Just following up on this issue. Has there been any progress or updates regarding the fix above? If there's anything I can help with, let me know!

Thanks in advance for your time.

Xinyi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants