Skip to main content

MLflow utilities for model uploading, registry management, evaluation, and naming conventions.

Project description

dgtw-mlflow-utils

A collection of MLflow utilities for model uploading, registry management, evaluation, and standardized naming conventions — designed for the TAT Personalization project.


Installation

From PyPI (recommended)

# Install the core package
pip install dgtw-mlflow-utils

# Install with TensorFlow support
pip install "dgtw-mlflow-utils[tensorflow]"

# Install with all ML framework extras
pip install "dgtw-mlflow-utils[all]"

In a Jupyter notebook

%pip install dgtw-mlflow-utils

Quick Start

1. Naming Conventions

See full example: examples/01_naming_conventions.py

from dgtw_mlflow_utils import ModelNameBuilder, Architecture, ModelVariant, Stage, RunAction

builder = ModelNameBuilder()

# Experiment name → "TAT_Recommendation_Research"
builder.experiment_name(component="Recommendation", stage=Stage.RESEARCH)

# Registered model name → "TAT_TwoTower_Baseline"
builder.registered_model_name(
    architecture=Architecture.TWO_TOWER,
    variant=ModelVariant.BASELINE,
)

# Run name → "Train_MiniLM_20261025-1400"
builder.run_name(action=RunAction.TRAIN, use_case="MiniLM")

# Validate a name
ModelNameBuilder.validate_registered_name("TAT_TwoTower_Baseline")  # True

2. Upload a Scikit-learn Model

See full example: examples/02_upload_sklearn.py

from dgtw_mlflow_utils import ModelUploader, UploadConfig, Architecture, ModelVariant, Stage, RunAction

config = UploadConfig(
    architecture=Architecture.TWO_TOWER,
    variant=ModelVariant.BASELINE,
    component="Recommendation",
    stage=Stage.RESEARCH,
    use_case="MiniLM",
    action=RunAction.UPLOAD,
    tracking_uri="http://your-mlflow-server:5000",
    params={"embedding_dim": 256},
    tags={"developer": "ai-team"},
)

uploader = ModelUploader(config)
print(uploader.summary())

# Upload
result = uploader.upload_sklearn(sk_model=pipeline, input_example=X_test[:5])
print(result["model_uri"])

3. Upload a TensorFlow / Keras Model

See full example: examples/04_upload_tensorflow.py

result = uploader.upload_tensorflow(tf_model=keras_model)

4. Upload a PyTorch Model

result = uploader.upload_pytorch(pytorch_model=torch_model)

5. Upload Raw Artifact Files

See full example: examples/05_upload_artifacts.py

result = uploader.upload_artifacts(
    files=["vectors.npy", "config.yaml"],
    artifact_subdir="evaluation",
    extra_metrics={"recall_5": 0.82, "mrr": 0.65},
)

6. Model Registry Management

See full example: examples/03_registry_management.py

from dgtw_mlflow_utils import ModelRegistry

registry = ModelRegistry(tracking_uri="http://your-mlflow-server:5000")

# List all versions
versions = registry.list_versions("TAT_TwoTower_Baseline")

# Promote latest version to champion
registry.promote("TAT_TwoTower_Baseline", alias="champion")

# Load the champion model for inference
model = registry.load_champion("TAT_TwoTower_Baseline")
predictions = model.predict(input_data)

# Cleanup old versions (dry run first)
deleted = registry.cleanup_old_versions("TAT_TwoTower_Baseline", keep=5, dry_run=True)

# Print formatted summary
registry.print_summary("TAT_TwoTower_Baseline")

7. Training Evaluation Callback (TensorFlow / Keras)

See full example: examples/06_evaluator.py

from dgtw_mlflow_utils import TfMLflowEpochLogger

callback = TfMLflowEpochLogger(
    enable_mlflow=True,
    experiment_name="TAT_Recommendation_Research",
    run_name="Training_Run",
    # Extra kwargs are logged as MLflow params:
    log_params={
        "epochs": 50,
        "learning_rate": 0.001,
    }
)

# The callback handles the full lifecycle:
#   on_train_begin → starts MLflow run + logs params
#   on_epoch_end   → logs training metrics per epoch
#   on_test_end    → logs validation metrics
#   on_train_end   → ends MLflow run
model.fit(train_dataset, epochs=50, callbacks=[callback])

NumPy Equivalent (NumpyMLflowLogger)

For custom training loops or other frameworks (PyTorch, Scikit-learn):

from dgtw_mlflow_utils import NumpyMLflowLogger

logger = NumpyMLflowLogger(
    enable_mlflow=True,
    experiment_name="TAT_Recommendation_Research",
    run_name="Training_Run_Numpy",
)

logger.on_train_begin()
for epoch in range(50):
    # ... training ...
    logger.on_epoch_end(epoch, metrics={"loss": 0.5})
logger.on_train_end()

8. Standalone Evaluation

See full example: examples/06_evaluator.py

from dgtw_mlflow_utils import evaluate_model_tf

# Runs a batch-by-batch evaluation loop and computes Recall@K, MRR, NDCG, Loss
metrics = evaluate_model_tf(
    model=trained_model,
    dataset=test_dataset,
    k=10,
    enable_mlflow=True,
    experiment_name="TAT_Recommendation_Research",
    run_name="Final_Evaluation",
    log_params={"dataset_size": 1000},
)
# metrics → {"recall_10": 0.85, "mrr_10": 0.72, "ndcg_10": 0.78, "loss": 0.34}

NumPy Equivalent (evaluate_model_numpy)

For evaluating any model that can return predictions as a NumPy array:

from dgtw_mlflow_utils import evaluate_model_numpy

metrics = evaluate_model_numpy(
    predict_fn=lambda batch: your_model.predict(batch),
    dataset=test_dataset,
    k=10,
    enable_mlflow=True,
    experiment_name="TAT_Recommendation_Research",
)

9. Low-level Retrieval Metrics

See full example: examples/06_evaluator.py

import tensorflow as tf
from dgtw_mlflow_utils import compute_retrieval_metrics

# logits: (batch_size, batch_size) similarity matrix from in-batch negatives
logits = tf.random.normal((32, 32))

metrics = compute_retrieval_metrics(logits, k=5)
# metrics → {"recall_5": tensor, "mrr_5": tensor, "ndcg_5": tensor}

NumPy Equivalent (compute_retrieval_metrics_numpy)

import numpy as np
from dgtw_mlflow_utils import compute_retrieval_metrics_numpy

logits = np.random.normal(size=(32, 32))
metrics = compute_retrieval_metrics_numpy(logits, k=5)

Examples

The examples/ directory contains runnable scripts demonstrating each feature:

File Description
01_naming_conventions.py Generate experiment, model, and run names with validation
02_upload_sklearn.py Train & upload a scikit-learn RandomForest on Iris
03_registry_management.py List versions, promote champion, load model, cleanup
04_upload_tensorflow.py Train a Keras model with epoch logging & upload
05_upload_artifacts.py Upload raw files (configs, CSVs) without model flavour
06_evaluator.py compute_retrieval_metrics, TfMLflowEpochLogger callback, evaluate_model_tf loop
# Run an example
python examples/01_naming_conventions.py

Modules

Module Description
dgtw_mlflow_utils.naming Enums & ModelNameBuilder for standardized MLflow naming
dgtw_mlflow_utils.uploader ModelUploader & UploadConfig for uploading models
dgtw_mlflow_utils.registry ModelRegistry for lifecycle management (aliases, cleanup, loading)
dgtw_mlflow_utils.evaluator TfMLflowEpochLogger, NumpyMLflowLogger, & standalone evaluation loops
dgtw_mlflow_utils.metrics compute_retrieval_metrics and compute_retrieval_metrics_numpy (Recall@K, MRR@K, NDCG@K)

Note: evaluator and metrics modules require TensorFlow. They are lazy-loaded so you can use the rest of the package without TensorFlow installed.

Optional Dependencies

Extra Packages Install command
tensorflow tensorflow>=2.10 pip install dgtw-mlflow-utils[tensorflow]
sklearn scikit-learn>=1.0 pip install dgtw-mlflow-utils[sklearn]
pytorch torch>=1.12 pip install dgtw-mlflow-utils[pytorch]
all All of the above pip install dgtw-mlflow-utils[all]
dev pytest, pytest-cov pip install dgtw-mlflow-utils[dev]

Project Structure

dgtw-mlflow-utils/
├── pyproject.toml              # pip packaging config
├── README.md
├── src/                        # → installed as dgtw_mlflow_utils
│   ├── __init__.py             # package exports (lazy TF imports)
│   ├── naming.py               # ModelNameBuilder & enums
│   ├── uploader.py             # ModelUploader & UploadConfig
│   ├── registry.py             # ModelRegistry
│   ├── evaluator.py            # MLflowEpochLogger & evaluate_model_tf (and NumPy variants)
│   └── metrics.py              # compute_retrieval_metrics
└── examples/
    ├── 01_naming_conventions.py
    ├── 02_upload_sklearn.py
    ├── 03_registry_management.py
    ├── 04_upload_tensorflow.py
    ├── 05_upload_artifacts.py
    └── 06_evaluator.py

Repository

https://git.digithunworldwide.com/ai/mlflow-utils.git

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dgtw_mlflow_utils-1.0.4.tar.gz (33.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dgtw_mlflow_utils-1.0.4-py3-none-any.whl (19.2 kB view details)

Uploaded Python 3

File details

Details for the file dgtw_mlflow_utils-1.0.4.tar.gz.

File metadata

  • Download URL: dgtw_mlflow_utils-1.0.4.tar.gz
  • Upload date:
  • Size: 33.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for dgtw_mlflow_utils-1.0.4.tar.gz
Algorithm Hash digest
SHA256 d2c9d2fca5ad97b47d40b42be23220ffe87a3a7c3d367695c526b65acb5370bd
MD5 3ccae0257ce54a6e1a6e3f80a5ed1254
BLAKE2b-256 b828bbe73daab34896a70b81a489152d3f72def5b4f1ba46a2270f0de13f8906

See more details on using hashes here.

File details

Details for the file dgtw_mlflow_utils-1.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for dgtw_mlflow_utils-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 59bdbd43aedc32f099f7303480e5840afb28bb1ffaacb71fdce2372a61f2dfa4
MD5 753d864929239c77222eadbdc0e5d2bc
BLAKE2b-256 47583c86f258c3a82a37f35016b9a22c3edfda166f9b776e6c03ca0037b7c198

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page