From 713aec7654540f84ffd803c81ad889779e8dc088 Mon Sep 17 00:00:00 2001 From: Robin van der Noord Date: Fri, 22 Sep 2023 15:56:39 +0200 Subject: [PATCH] docs(examples): updated example directory and README --- README.md | 54 ++++++++++++++-------------- examples/basic.py | 71 ++++++++++++++++++++++++++++++++++++ examples/basic.sh | 7 ++++ examples/tmp.py | 92 ----------------------------------------------- 4 files changed, 104 insertions(+), 120 deletions(-) create mode 100644 examples/basic.py create mode 100644 examples/basic.sh delete mode 100644 examples/tmp.py diff --git a/README.md b/README.md index 0912479..3003d22 100644 --- a/README.md +++ b/README.md @@ -29,12 +29,6 @@ Bundling your machine learning model with VerySimpleTransformers is straightforward. Follow these steps: -Import the necessary modules and functions: - -```python -from verysimpletransformers import to_vst -``` - Create or load your machine learning model (e.g., a ClassificationModel) using Simple Transformers. Example: @@ -60,7 +54,8 @@ model.train_model(...) ``` -Use the `to_vst` function to package the model into a .vst file with optional compression. +Import the `verysimpletransformers` package, and use the `to_vst` function to package the model into a .vst file with +optional compression. Provide the model, the output file path, and an optional compression level (0-9). By default, compression will be disabled. @@ -74,12 +69,13 @@ The output file path can be specified as: Example: ```python -from pathlib import Path +import verysimpletransformers # or: from verysimpletransformers import to_vst +from pathlib import Path # can be used instead of a string -to_vst(model, - "my_model.vst", - compression=5, # optional - ) +verysimpletransformers.to_vst(model, + "my_model.vst", + compression=5, # optional + ) ``` Note: As an alias, `bundle_model` can be used instead of `to_vst`. @@ -88,30 +84,24 @@ Note: As an alias, `bundle_model` can be used instead of `to_vst`. Loading a machine learning model from a .vst file is just as simple. Follow these steps: -Import the necessary modules and functions: - -```python -from verysimpletransformers import from_vst -``` - -Specify the path to the .vst file you want to load. Just like with `to_vst`, -this path can be a `str`, `Path` or binary IO. +Import `from_vst` from `simpletransformers`. +Specify the path to the .vst file you want to load. Just like with `to_vst`, this path can be a `str`, `Path` or binary +IO. +Use the `from_vst` function to load the model. Optionally, you can specify the `device` parameter to set the device ( +e.g., 'cpu' or 'cuda'). If not specified, the function will select a device based on availability. Example: ```python +from verysimpletransformers import from_vst from pathlib import Path -fp = Path("model.vst") -``` +fp = Path("model.vst") # can also be simply a string. -Use the `from_vst` function to load the model. Optionally, you can specify the `device` parameter to set the device ( -e.g., 'cpu' or 'cuda'). If not specified, the function will select a device based on availability. +new_model = from_vst(fp, + device='cuda', # optional + ) -Example: - -```python -new_model = from_vst(fp, device='cuda') ``` You can now use the `new_model` for various machine learning tasks. @@ -126,6 +116,11 @@ Note: As an alias, `load_model` can be used instead of `to_vst`. - Ensure that you have the required dependencies, such as SimpleTransformers and PyTorch, installed to use these functions effectively. +### Full Example + +To see a full example of saving and loading a `.vst` file, +see [examples/basic.py](https://github.com/trialandsuccess/verysimpletransformers/tree/master/examples/basic.py) + ## CLI The VerySimpleTransformers CLI provides a convenient and versatile way to interact with and manage your machine learning @@ -173,6 +168,9 @@ vst serve ./classification.vst ./classification.vst serve ``` +For more examples, +see [examples/basic.sh](https://github.com/trialandsuccess/verysimpletransformers/tree/master/examples/basic.sh) + ### Notes - A .vst file (e.g., 'model.vst') is required for most commands. diff --git a/examples/basic.py b/examples/basic.py new file mode 100644 index 0000000..4d05d82 --- /dev/null +++ b/examples/basic.py @@ -0,0 +1,71 @@ +from verysimpletransformers import from_vst, to_vst + +######################################## +### From the SimpleTransformers docs ### +######################################## + +from simpletransformers.classification import ClassificationModel, ClassificationArgs +import pandas as pd +import logging +import torch + +logging.basicConfig(level=logging.INFO) +transformers_logger = logging.getLogger("transformers") +transformers_logger.setLevel(logging.WARNING) + +# Preparing train data +train_data = [ + ["Aragorn was the heir of Isildur", 1], + ["Frodo was the heir of Isildur", 0], +] +train_df = pd.DataFrame(train_data) +train_df.columns = ["text", "labels"] + +# Preparing eval data +eval_data = [ + ["Theoden was the king of Rohan", 1], + ["Merry was the king of Rohan", 0], +] +eval_df = pd.DataFrame(eval_data) +eval_df.columns = ["text", "labels"] + +# Optional model configuration +model_args = ClassificationArgs(num_train_epochs=1) + +# Create a ClassificationModel +model = ClassificationModel( + "roberta", + "roberta-base", + args=model_args, + use_cuda=torch.cuda.is_available() +) + +# Train the model +model.train_model(train_df) + +# Evaluate the model +result, model_outputs, wrong_predictions = model.eval_model(eval_df) + +# Make predictions with the model +predictions, raw_outputs = model.predict(["Sam was a Wizard"]) + +print("Model Prediction:", predictions[0]) + +###################################### +### Now let's package it into VST! ### +###################################### + +to_vst(model, "example_basic.vst", compression=5) + +# that's it! + +###################################### +### Now let's load the model again ### +###################################### + +model = from_vst("example_basic.vst") + +# that's it! +predictions, _ = model.predict(["Sam was a Wizard"]) + +print("New Model Prediction:", predictions[0]) diff --git a/examples/basic.sh b/examples/basic.sh new file mode 100644 index 0000000..1cbc761 --- /dev/null +++ b/examples/basic.sh @@ -0,0 +1,7 @@ +python basic.py +vst example_basic.vst +vst run example_basic.vst +vst serve example_basic.vst +chmod +x example_basic.vst +./example_basic.vst serve +./example_basic.vst upgrade # warn: model is up-to-date diff --git a/examples/tmp.py b/examples/tmp.py deleted file mode 100644 index a6a59d6..0000000 --- a/examples/tmp.py +++ /dev/null @@ -1,92 +0,0 @@ -from pathlib import Path - -import pandas as pd -import torch.cuda -from simpletransformers.classification import ClassificationModel, ClassificationArgs -from verysimpletransformers.core import from_vst, to_vst -from verysimpletransformers.types import DummyModel - - -def example_bundle(): - # Optional model configuration - FAST = 1 - - if FAST == 2: - model = DummyModel() - elif FAST == 1: - model_args = ClassificationArgs( - num_train_epochs=1, - overwrite_output_dir=True, - save_model_every_epoch=False, - ) - - # Create a ClassificationModel - model = ClassificationModel( - "bert", "./outputs", args=model_args, - use_cuda=torch.cuda.is_available() - ) - else: - # Preparing train data - train_data = [ - ["Aragorn was the heir of Isildur", 1], - ["Frodo was the heir of Isildur", 0], - ] - train_df = pd.DataFrame(train_data) - train_df.columns = ["text", "labels"] - - # Preparing eval data - eval_data = [ - ["Theoden was the king of Rohan", 1], - ["Merry was the king of Rohan", 0], - ] - eval_df = pd.DataFrame(eval_data) - eval_df.columns = ["text", "labels"] - - model_args = ClassificationArgs( - num_train_epochs=1, - overwrite_output_dir=True, - save_model_every_epoch=False, - ) - - # Create a ClassificationModel - model = ClassificationModel( - "bert", "prajjwal1/bert-small", args=model_args, - use_cuda=torch.cuda.is_available() - ) - - # Train the model - model.train_model(train_df) - - fp = Path("pytest1.vst") - to_vst(model, fp, compression=5) - - new_model = from_vst(fp) - - # assert hasattr(model, 'predict') - # assert not hasattr(model, 'predictx') - assert hasattr(new_model, 'predict') - assert not hasattr(new_model, 'predictx') - -def load_with_cuda(): - fp = Path("kaggle.vst") - - new_model = from_vst(fp) - - print(new_model) - print( - new_model.predict(["Hi, how are you?"]) - ) - - to_vst(new_model, "kaggle_cpu.vst") - -def load_without_cuda(): - fp = Path("kaggle_cpu.vst") - - new_model = from_vst(fp) - - print(new_model) - - -if __name__ == '__main__': - load_with_cuda() - load_without_cuda()