diff --git a/examples/plot_model_card.py b/examples/plot_model_card.py index 9ff2b4b7..4acdfd1b 100644 --- a/examples/plot_model_card.py +++ b/examples/plot_model_card.py @@ -108,13 +108,9 @@ " max_leaf_nodes and max_depth." ) model_card_authors = "skops_user" -get_started_code = ( - "import pickle\nwith open(pkl_filename, 'rb') as file:\n clf = pickle.load(file)" -) citation_bibtex = "bibtex\n@inproceedings{...,year={2020}}" model_card.add( citation_bibtex=citation_bibtex, - get_started_code=get_started_code, model_card_authors=model_card_authors, limitations=limitations, model_description=model_description, diff --git a/skops/card/_model_card.py b/skops/card/_model_card.py index da9bb1bd..5b97c4ef 100644 --- a/skops/card/_model_card.py +++ b/skops/card/_model_card.py @@ -136,7 +136,7 @@ def metadata_from_config(config_path: Union[str, Path]) -> CardData: task = config.get("sklearn", {}).get("task", None) if task: card_data.tags += [task] - + card_data.model_file = config.get("sklearn", {}).get("model", {}).get("file") example_input = config.get("sklearn", {}).get("example_input", None) # Documentation on what the widget expects: # https://huggingface.co/docs/hub/models-widgets-examples @@ -380,6 +380,16 @@ def _generate_card(self) -> ModelCard: # add evaluation results template_sections = copy.deepcopy(self._template_sections) + if self.metadata: + model_file = self.metadata.to_dict().get("model_file") + if model_file: + template_sections["get_started_code"] = ( + "import joblib\nimport json\nimport pandas as pd\nclf =" + f' joblib.load({model_file})\nwith open("config.json") as f:\n ' + " config =" + " json.load(f)\n" + 'clf.predict(pd.DataFrame.from_dict(config["sklearn"]["example_input"]))' + ) template_sections["eval_results"] = tabulate( list(self._eval_results.items()), headers=["Metric", "Value"], diff --git a/skops/card/default_template.md b/skops/card/default_template.md index bbe9a4f8..edbc8d49 100644 --- a/skops/card/default_template.md +++ b/skops/card/default_template.md @@ -41,17 +41,10 @@ You can find the details about evaluation process and the evaluation results. Use the code below to get started with the model. -
- Click to expand - ```python {{ get_started_code | default("[More Information Needed]", true)}} ``` -
- - - # Model Card Authors diff --git a/skops/card/tests/test_card.py b/skops/card/tests/test_card.py index 79d381ae..dc5de4cd 100644 --- a/skops/card/tests/test_card.py +++ b/skops/card/tests/test_card.py @@ -32,6 +32,26 @@ def model_card(model_diagram=True): yield card +@pytest.fixture +def model_card_metadata_from_config(destination_path): + X, y = load_iris(return_X_y=True, as_frame=True) + est = LogisticRegression(solver="liblinear").fit(X, y) + pkl_file = tempfile.mkstemp(suffix=".pkl", prefix="skops-test")[1] + with open(pkl_file, "wb") as f: + pickle.dump(est, f) + hub_utils.init( + model=pkl_file, + requirements=[f"scikit-learn=={sklearn.__version__}"], + dst=destination_path, + task="tabular-classification", + data=X, + ) + card = Card( + est, model_diagram=True, metadata=metadata_from_config(destination_path) + ) + yield card + + @pytest.fixture def destination_path(): with tempfile.TemporaryDirectory(prefix="skops-test") as dir_path: @@ -109,24 +129,20 @@ def test_add_metrics(destination_path, model_card): assert ("acc" in card) and ("f1" in card) and ("0.1" in card) -def test_metadata_from_config_tabular_data(destination_path): +def test_code_autogeneration(destination_path, model_card_metadata_from_config): + # test if getting started code is automatically generated + model_card_metadata_from_config.save(Path(destination_path) / "README.md") + metadata = metadata_load(local_path=Path(destination_path) / "README.md") + filename = metadata["model_file"] + with open(Path(destination_path) / "README.md") as f: + assert f"joblib.load({filename})" in f.read() + + +def test_metadata_from_config_tabular_data( + model_card_metadata_from_config, destination_path +): # test if widget data is correctly set in the README - X, y = load_iris(return_X_y=True, as_frame=True) - est = LogisticRegression(solver="liblinear").fit(X, y) - pkl_file = tempfile.mkstemp(suffix=".pkl", prefix="skops-test")[1] - with open(pkl_file, "wb") as f: - pickle.dump(est, f) - hub_utils.init( - model=pkl_file, - requirements=[f"scikit-learn=={sklearn.__version__}"], - dst=destination_path, - task="tabular-classification", - data=X, - ) - card = Card( - est, model_diagram=True, metadata=metadata_from_config(destination_path) - ) - card.save(Path(destination_path) / "README.md") + model_card_metadata_from_config.save(Path(destination_path) / "README.md") metadata = metadata_load(local_path=Path(destination_path) / "README.md") assert "widget" in metadata