Upload fish_model_to_hub.py
Browse files- fish_model_to_hub.py +83 -0
fish_model_to_hub.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datasets import load_dataset
|
2 |
+
import pandas as pd
|
3 |
+
|
4 |
+
from sklearn.ensemble import GradientBoostingRegressor
|
5 |
+
from sklearn.pipeline import make_pipeline
|
6 |
+
from sklearn.compose import make_column_transformer
|
7 |
+
from sklearn.compose import make_column_selector
|
8 |
+
from sklearn.preprocessing import OneHotEncoder
|
9 |
+
|
10 |
+
from skops import hub_utils
|
11 |
+
import pickle
|
12 |
+
from skops import card
|
13 |
+
from pathlib import Path
|
14 |
+
|
15 |
+
my_token = "your token here"
|
16 |
+
|
17 |
+
# Load our data
|
18 |
+
dataset = load_dataset("brendenc/Fish")
|
19 |
+
|
20 |
+
df = pd.DataFrame(dataset['train'][:])
|
21 |
+
target = df.Weight
|
22 |
+
df = df.drop('Weight', axis=1)
|
23 |
+
|
24 |
+
# One hot encode our input
|
25 |
+
one_hot_encoder = make_column_transformer(
|
26 |
+
(
|
27 |
+
OneHotEncoder(sparse=False, handle_unknown="ignore"),
|
28 |
+
make_column_selector(dtype_include="object"),
|
29 |
+
),
|
30 |
+
remainder="passthrough",
|
31 |
+
)
|
32 |
+
|
33 |
+
# Train model
|
34 |
+
pipe = make_pipeline(
|
35 |
+
one_hot_encoder, GradientBoostingRegressor(random_state=42)
|
36 |
+
)
|
37 |
+
|
38 |
+
pipe.fit(df, target)
|
39 |
+
|
40 |
+
# Save the model
|
41 |
+
model_path = "example.pkl"
|
42 |
+
local_repo = "fish-model"
|
43 |
+
with open(model_path, mode="bw") as f:
|
44 |
+
pickle.dump(pipe, file=f)
|
45 |
+
|
46 |
+
# we will now initialize a local repository
|
47 |
+
hub_utils.init(
|
48 |
+
model=model_path,
|
49 |
+
requirements=[f"scikit-learn={sklearn.__version__}"],
|
50 |
+
dst=local_repo,
|
51 |
+
task="tabular-regression",
|
52 |
+
data=df,
|
53 |
+
)
|
54 |
+
|
55 |
+
# create the card
|
56 |
+
model_card = card.Card(pipe, metadata=card.metadata_from_config(Path('fish-model')))
|
57 |
+
|
58 |
+
limitations = "This model is intended for educational purposes."
|
59 |
+
model_description = "This is a GradientBoostingRegressor on a fish dataset."
|
60 |
+
model_card_authors = "Brenden Connors"
|
61 |
+
|
62 |
+
|
63 |
+
# we can add the information using add
|
64 |
+
model_card.add(
|
65 |
+
model_card_authors=model_card_authors,
|
66 |
+
limitations=limitations,
|
67 |
+
model_description=model_description,
|
68 |
+
)
|
69 |
+
|
70 |
+
# we can set the metadata part directly
|
71 |
+
model_card.metadata.license = "mit"
|
72 |
+
|
73 |
+
model_card.save(Path(local_repo) / "README.md")
|
74 |
+
|
75 |
+
# Push to the hub
|
76 |
+
repo_id = "scikit-learn/Fish-Weight/Fish-Weight"
|
77 |
+
hub_utils.push(
|
78 |
+
repo_id=repo_id,
|
79 |
+
source=local_repo,
|
80 |
+
token=my_token,
|
81 |
+
commit_message="Adding model files",
|
82 |
+
create_remote=True,
|
83 |
+
)
|