Skip to content

Commit

Permalink
added example page to the pydantic_plugin
Browse files Browse the repository at this point in the history
Signed-off-by: sumana sree <[email protected]>
  • Loading branch information
sumana-2705 committed Oct 20, 2024
1 parent 92d0b3d commit 8ff18ef
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/pydantic_plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pip install flytekitplugins-pydantic
```
## Example usage

For a usage example, see {doc}`Pydantic example usage <example>`.
For a usage example, see {doc}`Pydantic example usage <pydantic_integration_example>`.

```{toctree}
:maxdepth: -1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# %% [markdown]
# (pydantic_integration_example)=
#
# # Pydantic Integration Example
#
# Pydantic is a data validation and settings management library for Python, enabling the creation of data models with type annotations.
#
# Flyte leverages Pydantic for robust input validation and serialization, ensuring that task inputs are correctly structured.

# %%
from pydantic.v1 import BaseModel
from flytekit import task, workflow
from flytekit.types.file import FlyteFile
from typing import List

# %% [markdown]
# Let's first define a Pydantic model for training configuration.
# %%
class TrainConfig(BaseModel):
lr: float = 1e-3 # Learning rate
batch_size: int = 32 # Batch size for training
files: List[FlyteFile] # List of file inputs for training

# %% [markdown]
# Next, we use the Pydantic model in a Flyte task to train a model.
# %%
@task
def train(cfg: TrainConfig):
print(f"Training with learning rate: {cfg.lr} and batch size: {cfg.batch_size}")
for file in cfg.files:
print(f"Processing file: {file}")

# %% [markdown]
# Now we define a Flyte workflow that utilizes the training task.
# %%
@workflow
def training_workflow(lr: float = 1e-3, batch_size: int = 32, files: List[FlyteFile] = []):
cfg = TrainConfig(lr=lr, batch_size=batch_size, files=files)
train(cfg=cfg)

# %% [markdown]
# Finally, we execute the workflow with sample parameters.
# %%
if __name__ == "__main__":
training_workflow(lr=1e-3, batch_size=32, files=[FlyteFile(path="path/to/your/file")])

0 comments on commit 8ff18ef

Please sign in to comment.