Welcome, fellow alchemist! ๐งโโ๏ธ Ready to brew some machine learning magic without getting locked in a cauldron?
MLPotion is your chest of modular, mix-and-match ML building blocks that work across Keras, TensorFlow, and PyTorch. Think of it as LEGOยฎ for ML pipelines, but with fewer foot injuries and more flexibility!
Why MLPotion? ๐ค
Ever felt trapped by a framework that forces you to do things "their way"? We've been there. That's why we created MLPotion:
๐ฏ Framework Agnostic: Write once, run anywhere (well, on Keras, TensorFlow, or PyTorch)
๐งฑ Modular by Design: Pick the pieces you need, leave the rest in the box
๐ฌ Type-Safe: Python 3.10+ typing that actually helps you (mypy approved!)
๐ Production Ready: Built for the real world, not just notebooks
๐จ Orchestration Flexible: Works standalone OR with ZenML, Prefect, Airflow - your choice!
๐ฆ Install What You Need: Core package works without any ML frameworks (you only install what you need)!
๐ค Community-Driven: Missing something? Contribute it back - we love community additions!
What's in the Potion? ๐งช
โ๏ธ Core Ingredients
Type-safe protocols for all components
Framework-agnostic result types
Consistent error handling
Zero-dependency core package
๐ง Framework Support
Keras 3.0+ - The friendly one
TensorFlow 2.15+ - The production workhorse
PyTorch 2.0+ - The researcher's favorite
๐ Data Processing
CSV loaders for all frameworks
Dataset optimization utilities
Data transformers
Preprocessing pipelines
๐ Training & Evaluation
Unified training interface
Comprehensive evaluation tools
Rich result objects
Training history tracking
๐พ Model Management
Save/load model checkpoints
Export to production formats
Model inspection utilities
Multiple export formats
๐ Orchestration Integration
ZenML integration built-in
Extensible to Prefect, Airflow, etc.
Works standalone (no orchestration needed!)
Community contributions welcome
The MLPotion Philosophy ๐ญ
"A good potion doesn't force you to drink it a certain way. It just... works."
โ Ancient ML Alchemist Proverb (we just made that up)
We believe in:
Flexibility > Convention: Your project, your rules
Simplicity > Complexity: If it's hard to use, we failed
Type Safety > Runtime Surprises: Catch errors before they bite
Modularity > Monoliths: Use what you need, ignore the rest
Consistency > Chaos: Same patterns across all frameworks
Community > Corporate: Built by the community, for the community
Extensibility & Community Contributions ๐
MLPotion is designed to be extensible. While we provide ZenML integration out-of-the-box, you can easily integrate with:
Prefect: Wrap components as Prefect tasks
Airflow: Use as operators in DAGs
Kubeflow: Deploy in Kubeflow pipelines
Your Custom Orchestrator: The building blocks work anywhere!
Missing a feature? We actively encourage community contributions! Whether it's:
A new data loader (Parquet, Avro, databases)
Integration with another orchestration framework
Framework-specific optimizations
New export formats
Your contributions help everyone. Check out our Contributing Guide to get started!
Who's This For? ๐ฏ
You'll love MLPotion if you:
Switch between frameworks and hate rewriting everything
Value heavily tested code that you can reuse
Value type safety and IDE autocomplete (who doesn't?)
Want production-ready code without enterprise bloat
Believe ML pipelines should be composable and testable
You might want something else if you:
Do not like modularity
Do not like reusability
Are too lazy to contribute something that you can't already find here
"""Basic Keras usage WITHOUT ZenML.This example demonstrates the core MLPotion Keras workflow:1. Load data from CSV2. Create a Keras model3. Train the model4. Evaluate the model5. Save and export the model"""importtensorflowastffromloguruimportloggerfrommlpotion.frameworks.kerasimport(CSVDataLoader,ModelEvaluator,ModelPersistence,ModelTrainer,ModelTrainingConfig,)# =================== METHODS ===========================defcreate_model(input_dim:int=10)->tf.keras.Model:"""Create a simple feedforward neural network. Args: input_dim: Number of input features. Returns: Compiled Keras model. """model=tf.keras.Sequential([tf.keras.layers.Dense(64,activation="relu",input_shape=(input_dim,)),tf.keras.layers.Dropout(0.2),tf.keras.layers.Dense(32,activation="relu"),tf.keras.layers.Dropout(0.2),tf.keras.layers.Dense(1),])model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),loss="mse",metrics=["mae","mse"],)returnmodel# =================== MAIN ===========================# 1. Load data (โป๏ธ REUSABLE)logger.info("\n1. Loading data from CSV...")loader=CSVDataLoader(file_pattern="docs/examples/data/sample.csv",label_name="target",batch_size=8,shuffle=True,)dataset=loader.load()logger.info(f"Dataset created: {dataset}")# 2. Create model (CUSTOM)logger.info("\n2. Creating Keras model...")model=create_model(input_dim=10)logger.info(model.summary())# 3. Train model (โป๏ธ REUSABLE)logger.info("\n3. Training model...")trainer=ModelTrainer()config=ModelTrainingConfig(epochs=10,batch_size=8,learning_rate=0.001,validation_split=0.2,verbose=1,)result=trainer.train(model=model,data=dataset,config=config,)logger.info("\nTraining completed!")logger.info(f"Final training results: {result}")# 4. Evaluate model (โป๏ธ REUSABLE)logger.info("\n4. Evaluating model...")evaluator=ModelEvaluator()eval_result=evaluator.evaluate(model=model,data=dataset,config=config,)logger.info("Evaluation completed!")logger.info(f"Evaluation results: {eval_result}")# 5. Save model (โป๏ธ REUSABLE)logger.info("\n5. Saving model...")model_path="/tmp/keras_model.keras"persistence=ModelPersistence(path=model_path,model=model,)persistence.save(save_format="keras",)logger.info(f"Model saved to: {model_path}")# 6. Load model (โป๏ธ REUSABLE)logger.info("\n6. Loading model...")loaded_model,metadata=persistence.load()logger.info(f"Model loaded successfully: {type(loaded_model)}")logger.info("\n"+"="*60)logger.info("Complete!")logger.info("="*60)
"""Basic TensorFlow usage WITHOUT ZenML.This example demonstrates the core MLPotion TensorFlow workflow:1. Load data from CSV2. Optimize dataset for performance3. Create a TensorFlow model4. Train the model5. Evaluate the model6. Save and export the model"""importtensorflowastffrommlpotion.frameworks.tensorflowimport(CSVDataLoader,DatasetOptimizer,ModelEvaluator,ModelPersistence,ModelTrainer,ModelTrainingConfig,)defmain()->None:"""Run basic TensorFlow training pipeline."""print("="*60)print("MLPotion - TensorFlow Basic Usage")print("="*60)# 1. Load dataprint("\n1. Loading data...")loader=CSVDataLoader(file_pattern="examples/data/sample.csv",label_name="target",batch_size=1,# Load unbatched, let DatasetOptimizer handle batching)dataset=loader.load()print(f"Dataset: {dataset}")# Unbatch the dataset first (since CSVDataLoader batches by default)dataset=dataset.unbatch()# Transform OrderedDict to single tensordefprepare_features(features,label):"""Convert OrderedDict of features to single tensor."""feature_list=[features[key]forkeyinsorted(features.keys())]stacked_features=tf.stack(feature_list,axis=-1)returnstacked_features,labeldataset=dataset.map(prepare_features)# 2. Optimize datasetprint("\n2. Optimizing dataset...")optimizer=DatasetOptimizer(batch_size=8,shuffle_buffer_size=100)dataset=optimizer.optimize(dataset)# 3. Create modelprint("\n3. Creating model...")model=tf.keras.Sequential([tf.keras.layers.Dense(64,activation="relu",input_shape=(10,)),tf.keras.layers.Dropout(0.2),tf.keras.layers.Dense(32,activation="relu"),tf.keras.layers.Dropout(0.2),tf.keras.layers.Dense(1),])model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),loss="mse",metrics=["mae","mse"],)print(model.summary())# 4. Train modelprint("\n4. Training model...")trainer=ModelTrainer()config=ModelTrainingConfig(epochs=10,batch_size=8,learning_rate=0.001,verbose=1,)result=trainer.train(model=model,data=dataset,config=config,)print("\nTraining completed!")print(f"{result=}")# 5. Evaluate modelprint("\n5. Evaluating model...")evaluator=ModelEvaluator()frommlpotion.frameworks.tensorflowimportModelEvaluationConfigeval_config=ModelEvaluationConfig(batch_size=8,verbose=1)eval_result=evaluator.evaluate(model=model,data=dataset,config=eval_config,)print(f"{eval_result=}")# 6. Save modelprint("\n6. Saving model...")model_path="/tmp/tensorflow_model.keras"persistence=ModelPersistence(path=model_path,model=model,)persistence.save(save_format=".keras",)print(f"Model saved to: {model_path}")# 7. Load modelprint("\n7. Loading model...")loaded_model,metadata=persistence.load()print(f"Model loaded successfully: {type(loaded_model)}")print("\n"+"="*60)print("Complete!")print("="*60)if__name__=="__main__":main()
"""Keras training pipeline WITH ZenML orchestration.This example demonstrates how to use MLPotion's Keras componentswithin a ZenML pipeline for reproducible and tracked ML workflows.Requirements: pip install zenmlSetup: zenml init # Initialize ZenML repository export ZENML_RUN_SINGLE_STEPS_WITHOUT_STACK=true # For testing without full stack"""importkerasfromzenmlimportpipeline,stepfrommlpotion.integrations.zenml.keras.stepsimport(evaluate_model,export_model,load_data,save_model,train_model,)@stepdefcreate_model()->keras.Model:"""Create and compile a Keras model. Returns: Compiled Keras model ready for training. """model=keras.Sequential([keras.layers.Dense(64,activation="relu",input_shape=(10,)),keras.layers.Dropout(0.2),keras.layers.Dense(32,activation="relu"),keras.layers.Dropout(0.2),keras.layers.Dense(1),])model.compile(optimizer=keras.optimizers.Adam(learning_rate=0.001),loss="mse",metrics=["mae","mse"],)returnmodel@pipeline(enable_cache=False)defkeras_training_pipeline(file_path:str="examples/data/sample.csv",label_name:str="target",model_save_path:str="/tmp/keras_model.keras",export_path:str="/tmp/keras_model_export",):"""Complete Keras training pipeline with ZenML. This pipeline orchestrates the entire ML workflow: 1. Load data from CSV 2. Create and configure model 3. Train model 4. Evaluate model 5. Save model 6. Export model for deployment Args: file_path: Path to CSV data file. label_name: Name of the target column. model_save_path: Path to save the trained model. export_path: Path to export the model for serving. """# Step 1: Load datadataset=load_data(file_path=file_path,label_name=label_name,batch_size=8,shuffle=True,)# Step 2: Create model and configmodel=create_model()_config_train={"epochs":10,"learning_rate":0.001,"verbose":1,}# Step 3: Train modeltrained_model,training_metrics=train_model(model=model,data=dataset,**_config_train,)# Step 4: Evaluate modelevaluation_metrics=evaluate_model(model=trained_model,data=dataset,verbose=1,)# # Step 5: Save modelsave_model(model=trained_model,save_path=model_save_path,)# # Step 6: Export model for servingexport_model(model=trained_model,export_path=export_path,export_format="tf",)returntrained_model,training_metrics,evaluation_metricsif__name__=="__main__":"""Run the Keras ZenML pipeline."""print("="*60)print("MLPotion - Keras ZenML Pipeline")print("="*60)# Initialize ZenML (if not already initialized)try:fromzenml.clientimportClientclient=Client()print(f"โ ZenML initialized. Active stack: {client.active_stack_model.name}")exceptExceptionase:print(f"โ ๏ธ ZenML client error: {e}")print("Run 'zenml init' if you haven't already")# Run the pipelineprint("\nRunning ZenML pipeline...")result=keras_training_pipeline()print("\n"+"="*60)print("Pipeline completed successfully!")
"""TensorFlow training pipeline WITH ZenML orchestration.This example demonstrates how to use MLPotion's TensorFlow componentswithin a ZenML pipeline for reproducible and tracked ML workflows.Requirements: pip install zenmlSetup: zenml init # Initialize ZenML repository export ZENML_RUN_SINGLE_STEPS_WITHOUT_STACK=true # For testing without full stack"""importtensorflowastffromzenmlimportpipeline,stepfrommlpotion.frameworks.tensorflowimportModelTrainingConfigfrommlpotion.integrations.zenml.tensorflow.stepsimport(evaluate_model,export_model,load_data,optimize_data,save_model,train_model,)@step(enable_cache=False)# Disable caching to ensure fresh modeldefcreate_model()->tf.keras.Model:"""Create and compile a TensorFlow model that accepts dict inputs. Returns: Compiled TensorFlow/Keras model ready for training. """# Create inputs for each feature (10 features: feature_0 to feature_9)# After batching, make_csv_dataset produces tensors with shape (batch_size,) for each scalar feature# The materializer now correctly preserves this shape as (None,) where None is the batch dimensioninputs={}feature_list=[]foriinrange(10):# Each input has shape (1,) per sample after batching and materializer roundtrip# The materializer preserves the concrete shape (batch_size, 1)inp=tf.keras.Input(shape=(1,),name=f"feature_{i}",dtype=tf.float32)inputs[f"feature_{i}"]=inp# Already shape (batch_size, 1), no need to reshapefeature_list.append(inp)# Concatenate all features along the last axis# This will create shape (batch_size, 10)concatenated=tf.keras.layers.Concatenate(axis=-1)(feature_list)# Build the model architecturex=tf.keras.layers.Dense(64,activation="relu")(concatenated)x=tf.keras.layers.Dropout(0.2)(x)x=tf.keras.layers.Dense(32,activation="relu")(x)x=tf.keras.layers.Dropout(0.2)(x)outputs=tf.keras.layers.Dense(1)(x)# Create the functional modelmodel=tf.keras.Model(inputs=inputs,outputs=outputs)model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),loss="mse",metrics=["mae","mse"],)returnmodel@stepdefcreate_training_config()->ModelTrainingConfig:"""Create training configuration. Returns: Training configuration with hyperparameters. """returnModelTrainingConfig(epochs=10,batch_size=8,learning_rate=0.001,verbose=1,)@pipeline(enable_cache=False)deftensorflow_training_pipeline(file_path:str="examples/data/sample.csv",label_name:str="target",model_save_path:str="/tmp/tensorflow_model.keras",export_path:str="/tmp/tensorflow_model_export",):"""Complete TensorFlow training pipeline with ZenML. This pipeline orchestrates the entire ML workflow: 1. Load data from CSV 2. Optimize dataset for performance 3. Create and configure model 4. Train model 5. Evaluate model 6. Save model 7. Export model for deployment Args: file_path: Path to CSV data file. label_name: Name of the target column. model_save_path: Path to save the trained model. export_path: Path to export the model for serving. """# Step 1: Load datadataset=load_data(file_path=file_path,batch_size=1,label_name=label_name,)# Step 2: Optimize datasetoptimized_dataset=optimize_data(dataset=dataset,batch_size=8,shuffle_buffer_size=100,)# Step 3: Create model and configmodel=create_model()# Step 4: Train model_config_train={"epochs":10,"learning_rate":0.001,"verbose":1,}trained_model,training_metrics=train_model(model=model,dataset=optimized_dataset,**_config_train,)# Step 5: Evaluate modelevaluation_metrics=evaluate_model(model=trained_model,dataset=optimized_dataset,)# Step 6: Save modelsave_model(model=trained_model,save_path=model_save_path,)# Step 7: Export model for servingexport_model(model=trained_model,export_path=export_path,export_format="keras",)returntrained_model,training_metrics,evaluation_metricsif__name__=="__main__":"""Run the TensorFlow ZenML pipeline."""print("="*60)print("MLPotion - TensorFlow ZenML Pipeline")print("="*60)# Run the pipelineprint("\nRunning ZenML pipeline...")result=tensorflow_training_pipeline()print("\n"+"="*60)print("Pipeline completed successfully!")
"""PyTorch training pipeline WITH ZenML orchestration.This example demonstrates how to use MLPotion's PyTorch componentswithin a ZenML pipeline for reproducible and tracked ML workflows.Requirements: pip install zenmlSetup: zenml init # Initialize ZenML repository export ZENML_RUN_SINGLE_STEPS_WITHOUT_STACK=true # For testing without full stack"""importtorchimporttorch.nnasnnfromzenmlimportpipeline,stepfrommlpotion.integrations.zenml.pytorch.stepsimport(evaluate_model,export_model,load_csv_data,save_model,train_model,)classSimpleModel(nn.Module):"""Simple feedforward neural network. Args: input_dim: Number of input features. hidden_dim: Size of hidden layer. """def__init__(self,input_dim:int=10,hidden_dim:int=64)->None:super().__init__()self.fc1=nn.Linear(input_dim,hidden_dim)self.dropout1=nn.Dropout(0.2)self.fc2=nn.Linear(hidden_dim,32)self.dropout2=nn.Dropout(0.2)self.fc3=nn.Linear(32,1)defforward(self,x:torch.Tensor)->torch.Tensor:"""Forward pass through the network."""x=torch.relu(self.fc1(x))x=self.dropout1(x)x=torch.relu(self.fc2(x))x=self.dropout2(x)returnself.fc3(x)@stepdefcreate_model()->nn.Module:"""Create a PyTorch model. Returns: PyTorch model ready for training. """model=SimpleModel(input_dim=10,hidden_dim=64)returnmodel@pipeline(enable_cache=False)defpytorch_training_pipeline(file_path:str="examples/data/sample.csv",label_name:str="target",model_save_path:str="/tmp/pytorch_model.pth",export_path:str="/tmp/pytorch_model_export.pt",):"""Complete PyTorch training pipeline with ZenML. This pipeline orchestrates the entire ML workflow: 1. Load data from CSV 2. Create and configure model 3. Train model 4. Evaluate model 5. Save model 6. Export model for deployment Args: file_path: Path to CSV data file. label_name: Name of the target column. model_save_path: Path to save the trained model. export_path: Path to export the model for serving. """# Step 1: Load datadataloader=load_csv_data(file_path=file_path,label_name=label_name,batch_size=8,shuffle=True,)# Step 2: Create model and configmodel=create_model()# Step 3: Train model_config_train={"epochs":10,"learning_rate":0.001,"verbose":1,}model,metrics=train_model(model=model,dataloader=dataloader,**_config_train,)# Step 4: Evaluate modelevaluation_metrics=evaluate_model(model=model,dataloader=dataloader,)# Step 5: Save modelsave_model(model=model,save_path=model_save_path,)# # Step 6: Export model for serving (TorchScript)export_model(model=model,export_path=export_path,export_format="torchscript",)returnmodel,metrics,evaluation_metricsif__name__=="__main__":"""Run the PyTorch ZenML pipeline."""print("="*60)print("MLPotion - PyTorch ZenML Pipeline")print("="*60)# Initialize ZenML (if not already initialized)try:fromzenml.clientimportClientclient=Client()print(f"โ ZenML initialized. Active stack: {client.active_stack_model.name}")exceptExceptionase:print(f"โ ๏ธ ZenML client error: {e}")print("Run 'zenml init' if you haven't already")# Run the pipelineprint("\nRunning ZenML pipeline...")result=pytorch_training_pipeline()print("\n"+"="*60)print("Pipeline completed successfully!")