> For the complete documentation index, see [llms.txt](https://docs.cortexlabs.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cortexlabs.com/0.34/workloads/realtime-apis/multi-model/configuration.md).

# Configuration

## Python Handler

### Specifying models in API configuration

#### `cortex.yaml`

The directory `s3://cortex-examples/sklearn/mpg-estimator/linreg/` contains 4 different versions of the model.

```yaml
- name: mpg-estimator
  kind: RealtimeAPI
  handler:
    type: python
    path: handler.py
    models:
      path: s3://cortex-examples/sklearn/mpg-estimator/linreg/
```

#### `handler.py`

```python
import mlflow.sklearn


class Handler:
    def __init__(self, config, python_client):
        self.client = python_client

    def load_model(self, model_path):
        return mlflow.sklearn.load_model(model_path)

    def handle_post(self, payload, query_params):
        model_version = query_params.get("version")

        # model_input = ...

        model = self.client.get_model(model_version=model_version)
        result = model.predict(model_input)

        return {"prediction": result, "model": {"version": model_version}}
```

### Without specifying models in API configuration

#### `cortex.yaml`

```yaml
- name: text-analyzer
  kind: RealtimeAPI
  handler:
    type: python
    path: handler.py
    ...
```

#### `handler.py`

```python
class Handler:
    def __init__(self, config):
        self.analyzer = initialize_model("sentiment-analysis")
        self.summarizer = initialize_model("summarization")

    def handle_post(self, query_params, payload):
        model_name = query_params.get("model")
        model_input = payload["text"]

        # ...

        if model_name == "analyzer":
            results = self.analyzer(model_input)
            predicted_label = postprocess(results)
            return {"label": predicted_label}
        elif model_name == "summarizer":
            results = self.summarizer(model_input)
            predicted_label = postprocess(results)
            return {"label": predicted_label}
        else:
            return JSONResponse({"error": f"unknown model: {model_name}"}, status_code=400)
```

## TensorFlow Handler

### `cortex.yaml`

```yaml
- name: multi-model-classifier
  kind: RealtimeAPI
  handler:
    type: tensorflow
    path: handler.py
    models:
      paths:
        - name: inception
          path: s3://cortex-examples/tensorflow/image-classifier/inception/
        - name: iris
          path: s3://cortex-examples/tensorflow/iris-classifier/nn/
        - name: resnet50
          path: s3://cortex-examples/tensorflow/resnet50/
      ...
```

### `handler.py`

```python
class Handler:
    def __init__(self, tensorflow_client, config):
        self.client = tensorflow_client

    def handle_post(self, payload, query_params):
        model_name = query_params["model"]
        model_input = preprocess(payload["url"])
        results = self.client.predict(model_input, model_name)
        predicted_label = postprocess(results)
        return {"label": predicted_label}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.cortexlabs.com/0.34/workloads/realtime-apis/multi-model/configuration.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
