# Example: Upload a scikit-learn model The below documentation provides an example of how to integrate a model into Foundry from existing model files. For a step-by-step guide, refer to our documentation on how to [publish a model from pre-trained files](../model-asset-files/index.md). ## Create the model from model files The following example uses a model that was trained locally using the [Iris classification dataset ↗](https://archive.ics.uci.edu/dataset/53/iris) published by UC Irvine. The dataset has four features; `sepal_length`, `sepal_width`, `petal_length` and `petal_width` and can be used to build a model that predicts the species of the iris flower. In this example, we assume the model was trained locally as a [K-nearest neighbors classifier ↗](https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html) with the [scikit-learn library ↗](https://scikit-learn.org/stable/index.html). This example also assumes the model was trained with `Python 3.8.0` and `scikit-learn 1.3.2`. After training, the model is saved as a [pickle file ↗](https://docs.python.org/3/library/pickle.html), as defined below. ```python from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.neighbors import KNeighborsClassifier import pickle iris = load_iris() X = iris.data y = iris.target X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.4, random_state=4) knn = KNeighborsClassifier(n_neighbors = 5) knn.fit(X_train, y_train) with open("iris_model.pkl", "wb") as f: pickle.dump(knn, f) ``` ### 1. Upload the model files to an unstructured dataset The scikit-learn model files are uploaded to Palantir as an unstructured dataset, as shown in the image below: ![The iris classification model dataset, saved as a pickle file in the Palantir platform. ](../../../assets/ff46051743edcb053436145ff08634733078abd958b0dfe4e89779e56db43e81.webp) ## 2. Create a repository for model adapter logic In the Code Repositories application, create a new **Model Integration** repository with the **Model Training** language template, then add a dependency on `scikit-learn 1.3.2`. Define your logic to read the model files and publish a model. ### 3. Publish your model files as a model Once the model adapter logic is executed, the model will be published in the platform. ```python from transforms.api import transform, Input import palantir_models as pm from palantir_models.transforms import ModelOutput from palantir_models.serializers import DillSerializer import pickle @transform( model_files=Input(""), model_output=ModelOutput("") ) def compute(model_files, model_output): fs = model_files.filesystem() with fs.open("iris_model.pkl", "rb") as f: model = pickle.load(f) model_adapter = IrisModelAdapter(model, "target") model_output.publish( model_adapter=model_adapter ) class IrisModelAdapter(pm.ModelAdapter): @pm.auto_serialize( model=DillSerializer(), prediction_column_name=DillSerializer() ) def __init__(self, model, prediction_column_name): self.model = model self.prediction_column_name = prediction_column_name @classmethod def api(cls): column_names = ["sepal_length", "sepal_width", "petal_length", "petal_width"] columns = [(name, float) for name in column_names] inputs = {"df_in": pm.Pandas(columns=columns)} outputs = {"df_out": pm.Pandas(columns=columns + [("target", int)])} return inputs, outputs def predict(self, df_in): inference_data = df_in predictions = self.model.predict(inference_data.values) inference_data[self.prediction_column_name] = predictions return inference_data ``` ### 4. Consume the published model Once the model is published, it is ready to be consumed for inference in the platform. For this example, we will [create a new modeling objective](../../manage-models/create-a-modeling-objective/index.md) and submit the model. 1. Create a new modeling objective by navigating to the Project folder where you wish the objective to live, then selecting **New > Modeling Objective**. This will open the Modeling Objectives application. ![An empty state of the Modeling Objectives application.](../../../assets/516393c6345271d8a11e5121a8c675654f63109de21d8d8b33e82bf643f5da54.webp) 2. Next, submit a model to the objective. In the **Model Submissions > Submit a Model** section, select **Add Model** to open a dialog, as shown below. ![An Add Foundry model pop-up, with options for ways to add models to the platform. ](../../../assets/86648158bf30b0d5377377e61f034d3572a1e4f5920199d89ed8adcde4b3a044.webp) 3. Select **Submit model from Foundry**, then select **Next** to open a dialog where you can load the published model from its location in the platform. ![A pop-up dialog to select a model to submit to the platform. ](../../../assets/08c45c6eb9f69ba5e1fc336f8a749f2c8fc20bdc2f3bfa505b2fa7d96f3b9314.webp) 4. Once the model is submitted, you will be directed back to the modeling objective overview page, where a **Model submissions** section will show information about the submission. ![The recent model submission is now viewable from the overview page of the modeling objective. ](../../../assets/24e8e37a661a2203b8b5e7de36bcaf98e41b427c93ef02e6c0da38c9100dac26.webp) 5. Select the newly submitted model from the **Model submissions** section to open the model page. ![The model details page of the iris classification model in a modeling objective. ](../../../assets/dbf483ad2bce59faae0460cf92e639104a9f26d6351b737b6372d8f119517219.webp) 6. Select **Create new release** in the upper right corner to open a new window to create a release. ![The pop-up dialog to create a new release for the model. ](../../../assets/23a3a099887517923bfdea2a055a30c49acb0e07e2f65b68498c6bbcf204811e.webp) 7. After creating a release, return to the modeling objective overview page by selecting the name in the top left of the screen. Scroll down to **Deployments**, then select **Create deployment** to open another dialog window. ![The Create a new deployment page, with options for setting up a new model deployment.](../../../assets/2e286730e06d040ff0de085112a15e20b3659a08ce346e27eb85b9e82a25aae4.webp) 8. Complete the setup form, then select **Create deployment** in the bottom right corner. Return to the modeling objective overview, scroll down to **Deployments**, then select the newly deployed model to test. ![The test details page for the model deployment. ](../../../assets/434d72d8ece1fd209be7129595abbede25a1eac6c1d6310f98cd9bc63a63dc2a.webp) 9. From the **Query** tab on the top left of the page, test the model by adding values and observing the outputs. ![The Query page for the deployment test, with sections for entering inputs and viewing results.](../../../assets/4dfeb07f175a1e0dac4d03cc175a4d93646a12f4d7032d8383f3bda95c50b1bd.webp) More details on creating and querying a live deployment can be found in the [live deployment documentation](../../manage-models/set-up-live/index.md).