Skip to content

Sedimark/mlflow_api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

22 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Welcome to mlflow_api

Version License: MIT

SEDIMARK Rest API for interacting with the MLFlow in the toolbox

Usage

  • uv

    Requirements

    • uv >= 0.5.0
    • python >= 3.11.0

    Running

    • First run

      uv run src/mlflow_api/main.py
    • All the other runs

      uv run mlflow_api
  • Docker

    Environment Variables

    • MLFLOW_TRACKING_USERNAME - The username for the local MLFlow instance
    • MLFLOW_TRACKING_PASSWORD - The password for the local MLFlow instance
    • AWS_ACCESS_KEY_ID - The access key for the local MINIO/remote S3 instance
    • AWS_SECRET_ACCESS_KEY - The secret key for the local MINIO/remote S3 instance
    • MLFLOW_S3_ENDPOINT_URL - The url for the local MINIO/remote S3 instance
    • MLFLOW_TRACKING_INSECURE_TLS - The type of connection for the local MLFlow instance (true/false)
    • MLFLOW_TRACKING_URI - The url for the local MLFlow instance

    Building the image

    docker build -t mlflow_api .

    Running

      docker run -itd -p 8000:8000 \
    -e MLFLOW_TRACKING_USERNAME=admin \
    -e MLFLOW_TRACKING_PASSWORD=password \
    -e AWS_ACCESS_KEY_ID=<key> \
    -e AWS_SECRET_ACCESS_KEY=<secret> \ 
    -e MLFLOW_S3_ENDPOINT_URL=http://localhost:9001 \
    -e MLFLOW_TRACKING_INSECURE_TLS=true \
    -e MLFLOW_TRACKING_URI=http://localhost:5000 \
    mlflow_api

πŸš€ MLflow API

A powerful REST API for seamless MLflow integration

Version License: MIT Python FastAPI MLflow

πŸ“‹ Overview

The MLflow API is a comprehensive REST API service that provides seamless integration with MLflow for machine learning model management. Built with FastAPI, it offers endpoints for model registration, versioning, deployment, and various ML operations including support for multiple frameworks like PyTorch, TensorFlow, Keras, and scikit-learn.

✨ Features

  • πŸ” Model Discovery: Browse and search registered models
  • πŸ“Š Metrics & Parameters: Access model parameters, metrics, and metadata
  • πŸ—‚οΈ Version Management: Handle model versions and lifecycle stages
  • πŸ“ˆ Dataset Integration: Access training datasets and artifacts
  • πŸ–ΌοΈ Image Artifacts: Retrieve and display model-related images
  • πŸ”„ Model Import/Export: Seamlessly transfer models between environments
  • 🎯 Predictions: Make predictions using registered models
  • 🧠 Multi-Framework Support: Works with PyTorch, TensorFlow, Keras, scikit-learn
  • πŸ”§ Framework Tools: Get optimizers and loss functions for different ML frameworks
  • πŸ“¦ Model Packaging: Package models for deployment

πŸ› οΈ Supported ML Frameworks

Framework Import Export Predictions Packaging
PyTorch βœ… βœ… βœ… βœ…
TensorFlow βœ… βœ… βœ… βœ…
Keras βœ… βœ… βœ… βœ…
scikit-learn βœ… βœ… βœ… βœ…
PyFunc βœ… βœ… βœ… βœ…

πŸš€ Quick Start

Prerequisites

  • Python >= 3.12.0
  • uv >= 0.5.0 (recommended) or pip
  • Access to an MLflow tracking server
  • S3-compatible storage (MinIO or AWS S3)

🐍 Using uv (Recommended)

# Clone the repository
git clone <your-repo-url>
cd mlflow_api

# First run - install dependencies and run
uv run src/mlflow_api/main.py

# Subsequent runs
uv run mlflow_api

🐳 Using Docker

Environment Variables

Create a .env file with the following variables:

MLFLOW_TRACKING_USERNAME=admin
MLFLOW_TRACKING_PASSWORD=password
AWS_ACCESS_KEY_ID=your_access_key
AWS_SECRET_ACCESS_KEY=your_secret_key
MLFLOW_S3_ENDPOINT_URL=http://localhost:9001
MLFLOW_TRACKING_INSECURE_TLS=true
MLFLOW_TRACKING_URI=http://localhost:5000

Build and Run

# Build the Docker image
docker build -t mlflow-api .

# Run the container
docker run -itd -p 8000:8000 \
  -e MLFLOW_TRACKING_USERNAME=admin \
  -e MLFLOW_TRACKING_PASSWORD=password \
  -e AWS_ACCESS_KEY_ID=your_access_key \
  -e AWS_SECRET_ACCESS_KEY=your_secret_key \
  -e MLFLOW_S3_ENDPOINT_URL=http://localhost:9001 \
  -e MLFLOW_TRACKING_INSECURE_TLS=true \
  -e MLFLOW_TRACKING_URI=http://localhost:5000 \
  mlflow-api

πŸ“š API Documentation

Once the service is running, access the interactive API documentation:

πŸ”§ API Endpoints

🏠 Health Check

  • GET / - Server health check

πŸ€– Models

  • GET /models - List all registered models
  • GET /model/parameters - Get model parameters
  • GET /model/metrics - Get model metrics
  • GET /model/dataset - Download training dataset
  • GET /model/images - Get model artifacts (images)
  • GET /model/versions - Get model versions
  • GET /model/package - Package model for deployment
  • GET /model/export - Export model as ZIP
  • POST /model/import - Import model from ZIP
  • POST /model/predict - Make predictions
  • POST /model/register - Register a new model

🧠 Framework Tools

  • GET /optimizers/{framework} - Get available optimizers
  • GET /losses/{framework} - Get available loss functions

Supported frameworks: torch, keras

πŸ—οΈ Project Structure

mlflow_api/
β”œβ”€β”€ src/mlflow_api/
β”‚   β”œβ”€β”€ __init__.py          # Package initialization
β”‚   β”œβ”€β”€ main.py              # FastAPI application and endpoints
β”‚   β”œβ”€β”€ mlflow_client.py     # MLflow client wrapper
β”‚   β”œβ”€β”€ models.py            # Model handlers for different frameworks
β”‚   └── schemas.py           # Pydantic schemas for API responses
β”œβ”€β”€ .github/workflows/       # CI/CD workflows
β”œβ”€β”€ Dockerfile              # Docker configuration
β”œβ”€β”€ pyproject.toml          # Project configuration and dependencies
└── README.md               # This file

πŸ§ͺ Example Usage

Register a Model

curl -X POST "http://localhost:8000/model/register" \
     -H "Content-Type: application/json" \
     -d '{"run_id": "experiment_id/run_id", "model_name": "my_model"}'

Get Model Parameters

curl "http://localhost:8000/model/parameters?name=my_model&version=1"

Make Predictions

curl -X POST "http://localhost:8000/model/predict?name=my_model" \
     -F "[email protected]"

Export a Model

curl "http://localhost:8000/model/export?name=my_model&version=1" \
     --output my_model_v1.zip

πŸ”’ Security & Configuration

The API uses environment variables for configuration. Ensure secure storage of credentials:

  • Use strong passwords for MLflow authentication
  • Secure S3/MinIO access keys
  • Consider using Docker secrets or Kubernetes secrets in production
  • Enable TLS in production environments

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License.

πŸ™ Acknowledgments

  • Built with FastAPI
  • Powered by MLflow
  • Supports multiple ML frameworks
  • Created by SIEMENS SRL

Made with ❀️ for the ML community

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published