Skip to content

Added new file format param to skops snippet script#417

Merged
merveenoyan merged 12 commits intomainfrom
fix_snippet
Jan 5, 2023
Merged

Added new file format param to skops snippet script#417
merveenoyan merged 12 commits intomainfrom
fix_snippet

Conversation

@merveenoyan
Copy link
Copy Markdown
Contributor

As discussed with @rajshah4 in Slack, it's better to state explicitly to load a model using pickle if it's a pickle file. Also cc @adrinjalali

Comment thread js/src/lib/interfaces/Libraries.ts Outdated
@osanseviero
Copy link
Copy Markdown
Contributor

The current code snippet does not work I think. E.g. go to https://huggingface.co/Ramos-Ramos/emb-gam-dino-resnet

The snippet is

from skops.hub_utils import download
from skops.io import load

download("Ramos-Ramos/emb-gam-dino-resnet", "path_to_folder")
# make sure model file is in skops format
# if model is a pickle file, make sure it's from a source you trust
model = load("path_to_folder/model.pkl")

If you try the above, it will give an error

 TypeError: download() takes 2 positional arguments but 1 was given

The expectation is actually to use named arguments

download(repo_id="Ramos-Ramos/emb-gam-dino-resnet", dst="emb-gam-dino-resnet")

But then

model = load("path_to_folder/model.pkl")

Gives me an error

BadZipFile: File is not a zip file

What did work for me was

import pickle

with open("emb-gam-dino-resnet/model.pkl", "rb") as file: 
  logistic_regression = pickle.load(file)

Could we update the auto-snippet for it to work end to end?

@merveenoyan
Copy link
Copy Markdown
Contributor Author

merveenoyan commented Dec 8, 2022

@osanseviero I'm working on this PR: skops-dev/skops#242 (my work is done, waiting for review)
Once it's merged, we can make this work end to end.
Since we made a decision to add a key to config instead, I'll edit the snippet such that this info is taken from config.

Comment thread js/src/lib/interfaces/Libraries.ts
Comment thread js/src/lib/interfaces/Libraries.ts Outdated
const skopssaveFormat = model.config?.sklearn?.save_format;
if (skopssaveFormat == "pickle")
{
return `download("${model.id}", "path_to_folder")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the imports at the top, and also with from skops.hub_utils import download which is not in this one

@merveenoyan
Copy link
Copy Markdown
Contributor Author

@osanseviero please don't review until I can test it 😅 (had a problem in codespaces)

@merveenoyan
Copy link
Copy Markdown
Contributor Author

  1. model uploaded using skops with pickle

Ekran Resmi 2022-12-09 21 08 20

2. model uploaded using skops with skops as file format

Ekran Resmi 2022-12-09 21 09 24

3. vanilla sklearn model

Ekran Resmi 2022-12-09 21 10 07

@merveenoyan
Copy link
Copy Markdown
Contributor Author

The work on this is done: skops-dev/skops#242 (just made a mistake on changelog, fixed it) we can merge this one after that one.

Copy link
Copy Markdown
Contributor

@osanseviero osanseviero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The results look nice! I think we might want to:

  • Keep showing security message for joblib as we were doing before
  • Looking at the file (
    const skopssaveFormat = model.config?.sklearn?.model_format;
    ), there are some minor indentation/formatting issues in the sklearn function. Please try to be consistent (e.g. opening curly braces in same line - 237/238; double indent in lines 247 and 254)

Comment thread js/src/lib/interfaces/Libraries.ts
Comment thread js/src/lib/interfaces/Libraries.ts Outdated
model = load("path_to_folder/${skopsmodelFile}")`;
} else {
}
} else {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we bring the security warning back?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

brought it back

Comment thread js/src/lib/interfaces/Libraries.ts Outdated
model = load("path_to_folder/${skopsmodelFile}")`;
} else {
}
} else {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please unindent the closing ) in the vanilla sklearn code snippet

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread js/src/lib/interfaces/Libraries.ts
@adrinjalali
Copy link
Copy Markdown
Contributor

This looks good to me, but I'm more comfortable if @osanseviero has a final look before merging.

@merveenoyan merveenoyan changed the title Added extra note to snippet Added new file format param to skops snippet script Dec 12, 2022
@merveenoyan
Copy link
Copy Markdown
Contributor Author

merveenoyan commented Dec 12, 2022

@adrinjalali we need to merge the one on skops and moon-landing first that's why I didn't merge it yet.


@osanseviero
I feel like I'm going crazy so I'll stop working today 😂 (for pickle model)

Ekran Resmi 2022-12-12 20 37 32

Ekran Resmi 2022-12-12 20 37 07

BUT
Ekran Resmi 2022-12-12 20 28 33

@osanseviero
Copy link
Copy Markdown
Contributor

In your code snippet you do

const skopssaveFormat = model.config?.sklearn?.model_format;

But there is no model_format, there is fileformat. Hence, you don't enter the condition of line 237 (format == pickle), and you end up being in the else, which is what I see in the screenshot. Maybe this is a good signal of not using if/else here and rather if/else if/else, and in the else show some other snippet with a comment saying it's not a supported format.

@merveenoyan
Copy link
Copy Markdown
Contributor Author

merveenoyan commented Dec 12, 2022

@osanseviero screenshot has the one I'm using for build and it doesn't say 'model_format', it does 'fileformat', hence it gets into the if with 'pickle' (see what's logged) (the code in the HEAD is not updated, don't check that)

@merveenoyan
Copy link
Copy Markdown
Contributor Author

I guess I was using a wrong version on moon-landing and everything was working fine. @osanseviero 😂

Ekran Resmi 2022-12-13 15 50 35

Ekran Resmi 2022-12-13 15 48 08

Ekran Resmi 2022-12-13 15 51 30

We can merge after this one is merged: https://github.com/skops-dev/skops/pull/242

Comment thread js/src/lib/interfaces/Libraries.ts Outdated
merveenoyan and others added 2 commits December 14, 2022 11:54
Co-authored-by: Omar Sanseviero <osanseviero@gmail.com>
@merveenoyan
Copy link
Copy Markdown
Contributor Author

@osanseviero updated variable name in accordance to moon-landing

@merveenoyan
Copy link
Copy Markdown
Contributor Author

@osanseviero can we merge this since one in moon-landing is merged?

Copy link
Copy Markdown
Contributor

@osanseviero osanseviero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to merge :)

@merveenoyan merveenoyan merged commit 2e4d048 into main Jan 5, 2023
@merveenoyan merveenoyan deleted the fix_snippet branch January 5, 2023 06:07
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

Successfully merging this pull request may close these issues.

3 participants