Skip to main content

A CLI tool for deploying ML models as production-ready REST APIs

Project description

DeployWizard

Python Version License: MIT Tests Code style: black

DeployWizard is a powerful CLI tool that automates the deployment of machine learning models as production-ready REST APIs with Docker support. It generates all the necessary code and configuration files to containerize your ML models with FastAPI, making deployment a breeze.

Features

  • Multi-Framework Support: Works with scikit-learn, PyTorch, and TensorFlow models
  • Production-Ready: Generates Dockerfiles and optimized FastAPI applications
  • Environment-Aware: Handles both development and production environments
  • Secure by Default: Uses non-root users in containers and secure defaults
  • Easy to Use: Simple CLI interface for generating deployment code
  • Customizable: Flexible template system for advanced customization
  • Tested: Comprehensive test suite with 100% code coverage

Installation

  1. Create and activate a virtual environment (recommended):

    python -m venv venv
    source venv/bin/activate  # On Windows use: venv\Scripts\activate
    
  2. Install DeployWizard using pip:

    pip install deploywizard
    
  3. Verify installation:

    deploywizard --version
    

Quick Start

1. Initialize a New Project (Recommended for New Models)

The init command creates a new deployment project and registers your model in one step:

# Initialize a project with a scikit-learn model
deploywizard init --model path/to/model.pkl --framework sklearn --output-dir my_ml_api

# Initialize with a custom model name
deploywizard init --model path/to/model.pkl --framework sklearn --output-dir my_ml_api --name my_model

# For PyTorch models (full model)
deploywizard init --model path/to/model.pt --framework pytorch --output-dir pytorch_api

# For PyTorch state_dict models (requires model class)
deploywizard init --model path/to/model.pt --framework pytorch --output-dir pytorch_api \
    --model-class path/to/model.py

This will:

  1. Register your model in the DeployWizard registry
  2. Create a new directory with a complete API project
  3. Generate all necessary configuration files
  4. Print the next steps to run your API

2. Deploying PyTorch Models

When working with PyTorch models, you have two options depending on how the model was saved:

Option 1: Full Model (Recommended)

If you saved the entire model using torch.save(model, 'model.pt'):

deploywizard init --model model.pt --framework pytorch --output-dir pytorch_api

Option 2: State Dictionary

If you saved just the state dict using torch.save(model.state_dict(), 'model.pt'), you need to provide the model class definition:

  1. Create a Python file (e.g., model.py) with your model class:

    import torch
    import torch.nn as nn
    
    class MyModel(nn.Module):
        def __init__(self, input_size=10, hidden_size=20, output_size=1):
            super().__init__()
            self.fc1 = nn.Linear(input_size, hidden_size)
            self.fc2 = nn.Linear(hidden_size, output_size)
            self.sigmoid = nn.Sigmoid()
        
        def forward(self, x):
            x = torch.relu(self.fc1(x))
            x = self.fc2(x)
            return self.sigmoid(x)
    
  2. Initialize the project with the model class:

    deploywizard init --model model.pt --framework pytorch --output-dir pytorch_api \
        --model-class model.py
    

3. Run the Deployed API

After initializing your project, navigate to the output directory and start the API:

cd my_ml_api
docker-compose up --build

Once the containers are running, you can access:

  • API: http://localhost:8000
  • Interactive API documentation: http://localhost:8000/docs
  • Alternative documentation: http://localhost:8000/redoc

4. Test the API

Test the API using curl or any HTTP client:

# Health check
curl http://localhost:8000/health
# Expected output: {"status":"healthy"}

# Make predictions (example format)
curl -X POST http://localhost:8000/predict \
  -H "Content-Type: application/json" \
  -d '{"features": [5.1, 3.5, 1.4, 0.2]}'

Advanced Usage

Manual Model Registration (Alternative to init)

If you prefer to manage the model registration separately:

# Register a model
deploywizard register model.pkl --name my_model --version 1.0.0 --framework sklearn

# Deploy a registered model
deploywizard deploy --name my_model --output my_api

# Update model metadata
deploywizard update --name my_model --new-version 2.0.0 --description "Improved model"

# Delete a model
deploywizard delete --name old_model

Testing

DeployWizard includes a comprehensive test suite. To run the tests:

# Install test dependencies
pip install -e ".[test]"

# Run all tests
pytest -v

# Run tests with coverage report
pytest --cov=deploywizard --cov-report=term-missing

# Run a specific test file
pytest tests/test_cli.py -v

Troubleshooting

Common Issues

  1. Model not found

    • Ensure the model file exists at the specified path
    • Verify the framework is correctly specified
  2. PyTorch Model Loading Issues

    • For state_dict models, ensure you've provided the --model-class option
    • Verify the model class in the provided file matches the architecture used during training
    • Check that all required imports are included in your model class file
  3. Port already in use

    • Stop any containers using port 8000
    • Or specify a different port: docker run -p 8080:8000
  4. Docker build fails

    • Check your internet connection
    • Verify Docker is running
    • Check the Docker logs for specific error messages
  5. UnicodeDecodeError on Windows

    • Ensure your terminal supports UTF-8 encoding
    • Set the following environment variable: set PYTHONUTF8=1

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a new branch for your feature
  3. Commit your changes with descriptive messages
  4. Push to the branch
  5. Create a new Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Built with ❤️ using FastAPI and Docker
  • Inspired by the need for simple ML model deployment solutions
  • Thanks to all contributors who have helped improve this project!

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

deploywizard-0.3.1.1.tar.gz (36.7 kB view details)

Uploaded Source

Built Distribution

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

deploywizard-0.3.1.1-py3-none-any.whl (23.1 kB view details)

Uploaded Python 3

File details

Details for the file deploywizard-0.3.1.1.tar.gz.

File metadata

  • Download URL: deploywizard-0.3.1.1.tar.gz
  • Upload date:
  • Size: 36.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for deploywizard-0.3.1.1.tar.gz
Algorithm Hash digest
SHA256 ea3b8697f62d6dbb695074405acfe6fc0ae8b81106bd221e6bcba757a0766416
MD5 944a26615bb26b5dc4aa80f3c4a631d3
BLAKE2b-256 b889f0a309f39305f5677ca35c18ed767232fa21942c42ce7d7204e25682eede

See more details on using hashes here.

File details

Details for the file deploywizard-0.3.1.1-py3-none-any.whl.

File metadata

  • Download URL: deploywizard-0.3.1.1-py3-none-any.whl
  • Upload date:
  • Size: 23.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for deploywizard-0.3.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ce3ce231ba31bc51cc3f47f43c0ea795b4bb8e23e344eb0d24bfad9ff24b5dcc
MD5 584a5abd6c5d61aa05dc2ac20d056a9d
BLAKE2b-256 d81e8c23cef920c7d6191cb7988040f0067ff6b7c673948a2e69dcadedab4249

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