LogoLogo
WebsiteSlack
0.30
0.30
  • Get started
  • Clients
    • Install
    • CLI commands
    • Python API
    • Environments
    • Telemetry
    • Uninstall
  • Workloads
    • Realtime APIs
      • Example
      • Predictor
      • Configuration
      • Models
      • Parallelism
      • Server-side batching
      • Autoscaling
      • Statuses
      • Metrics
      • Multi-model
        • Example
        • Configuration
        • Caching
      • Traffic Splitter
        • Example
        • Configuration
      • Troubleshooting
    • Batch APIs
      • Example
      • Predictor
      • Configuration
      • Jobs
      • Statuses
    • Task APIs
      • Example
      • Definition
      • Configuration
      • Jobs
      • Statuses
    • Dependencies
      • Example
      • Python packages
      • System packages
      • Custom images
    • Observability
      • Logging
      • Metrics
  • Clusters
    • AWS
      • Install
      • Update
      • Auth
      • Security
      • Spot instances
      • Networking
        • Custom domain
        • HTTPS (via API Gateway)
        • VPC peering
      • Setting up kubectl
      • Uninstall
    • GCP
      • Install
      • Credentials
      • Setting up kubectl
      • Uninstall
    • Private Docker registry
Powered by GitBook
On this page
  • Define a multi-model API
  • Deploy
  1. Workloads
  2. Realtime APIs
  3. Multi-model

Example

Deploy several models in a single API to improve resource utilization efficiency.

Define a multi-model API

# multi_model.py

import cortex

class PythonPredictor:
    def __init__(self, config):
        from transformers import pipeline
        self.analyzer = pipeline(task="sentiment-analysis")

        import wget
        import fasttext
        wget.download(
            "https://dl.fbaipublicfiles.com/fasttext/supervised-models/lid.176.bin", "/tmp/model"
        )
        self.language_identifier = fasttext.load_model("/tmp/model")

    def predict(self, query_params, payload):
        model = query_params.get("model")
        if model == "sentiment":
            return self.analyzer(payload["text"])[0]
        elif model == "language":
            return self.language_identifier.predict(payload["text"])[0][0][-2:]

requirements = ["tensorflow", "transformers", "wget", "fasttext"]

api_spec = {"name": "multi-model", "kind": "RealtimeAPI"}

cx = cortex.client("aws")
cx.create_api(api_spec, predictor=PythonPredictor, requirements=requirements)

Deploy

python multi_model.py
PreviousMulti-modelNextConfiguration

Last updated 4 years ago