-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added example page to the pydantic_plugin
Signed-off-by: sumana sree <[email protected]>
- Loading branch information
1 parent
92d0b3d
commit 8ff18ef
Showing
2 changed files
with
46 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
examples/pydantic_plugin/pydantic_plugin/pydantic_integration_example.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")]) |