Skip to main content

Turn a Model into an API in One Line

Project description

Quick-API

Turn a Model into an API in One Line

Quick-API is a Python library that wraps saved machine learning models (like .pkl, .h5, .joblib files) in a simple REST API using FastAPI with just one line of code.

Features

  • 🚀 One-line API creation - Turn any saved model into a REST API instantly
  • 🔄 Auto data conversion - Automatically handles JSON to NumPy array conversion
  • 📊 Multiple model formats - Supports scikit-learn, TensorFlow/Keras, PyTorch models
  • FastAPI powered - Built on FastAPI for high performance and automatic documentation
  • 🔍 Automatic endpoint discovery - Creates /predict endpoint automatically
  • 📝 Interactive docs - Get Swagger UI documentation out of the box
  • 🛡️ Input validation - Built-in request validation and error handling

Installation

pip install ml-quick-api

Quick Start

1. Basic Usage

from quick_api import create_api

# Turn your model into an API with one line
api = create_api("path/to/your/model.pkl")

# Run the API
api.run()

2. Advanced Usage

from quick_api import create_api

# Create API with custom configuration
api = create_api(
    model_path="models/my_classifier.pkl",
    host="0.0.0.0",
    port=8080,
    title="My ML API",
    description="A custom machine learning API",
    version="1.0.0"
)

# Run with custom settings
api.run(reload=True, workers=4)

3. Using the API

Once your API is running, you can make predictions:

curl -X POST "http://localhost:8000/predict" \
     -H "Content-Type: application/json" \
     -d '{"data": [[1.0, 2.0, 3.0, 4.0]]}'

Or visit http://localhost:8000/docs for interactive Swagger documentation.

Supported Model Types

  • Scikit-learn models (.pkl, .joblib)
  • TensorFlow/Keras models (.h5, .keras, saved_model format)
  • PyTorch models (.pt, .pth)
  • Custom models with predict method

API Endpoints

Endpoint Method Description
/predict POST Make predictions with your model
/health GET Check API health status
/info GET Get model information
/docs GET Interactive API documentation

Examples

Scikit-learn Example

import joblib
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
from quick_api import create_api

# Train and save a model
X, y = make_classification(n_samples=1000, n_features=4)
model = RandomForestClassifier()
model.fit(X, y)
joblib.dump(model, "classifier.pkl")

# Create API
api = create_api("classifier.pkl")
api.run()

TensorFlow Example

import tensorflow as tf
from quick_api import create_api

# Assuming you have a saved TensorFlow model
api = create_api("path/to/model.h5")
api.run()

Configuration Options

api = create_api(
    model_path="model.pkl",           # Path to your model file
    host="localhost",                 # Host to run the API on
    port=8000,                       # Port to run the API on
    title="Quick-API",               # API title
    description="ML Model API",      # API description
    version="1.0.0",                # API version
    input_shape=None,                # Expected input shape (auto-detected)
    preprocess_func=None,            # Custom preprocessing function
    postprocess_func=None,           # Custom postprocessing function
)

CLI Usage

Quick-API also provides a command-line interface:

# Basic usage
quick-api serve model.pkl

# With custom options
quick-api serve model.pkl --host 0.0.0.0 --port 8080 --title "My API"

Development

# Clone the repository
git clone https://github.com/yourusername/quick-api.git
cd quick-api

# Install in development mode
pip install -e ".[dev]"

# Run tests
pytest

# Format code
black quick_api/

# Type checking
mypy quick_api/

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

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

Changelog

v0.1.0

  • Initial release
  • Support for scikit-learn, TensorFlow, and PyTorch models
  • FastAPI-based REST API
  • Automatic documentation
  • CLI interface

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

ml-quick-api-0.1.1.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

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

ml_quick_api-0.1.1-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file ml-quick-api-0.1.1.tar.gz.

File metadata

  • Download URL: ml-quick-api-0.1.1.tar.gz
  • Upload date:
  • Size: 17.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.11.2

File hashes

Hashes for ml-quick-api-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3b449385bbfe2023234c5555ead868c229be411776e79abed8dfeb13ab333a6a
MD5 29536e108decf0218811e58a363751f2
BLAKE2b-256 d176a02afe493a1a9b70b3ed0484c380f1d7f626ee1b18d42ed5413cc3859f7d

See more details on using hashes here.

File details

Details for the file ml_quick_api-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: ml_quick_api-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.11.2

File hashes

Hashes for ml_quick_api-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e304a7b38eb07cd31b8c662572414c60e149252c4bedbf77c82b1056da4f7ca4
MD5 6e6bf5d155289b7acdbf8c8490e12058
BLAKE2b-256 ae639a87c9c9805ee6d9da04ae653a19cf01a2d3023019b1bfe391efafd543e8

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