```python:inference/models/moondream2/moondream2_inference_models.py
from typing import Union

import cv2
import numpy as np
from inference.core.env import (
    ALLOW_INFERENCE_MODELS_DIRECTLY_ACCESS_LOCAL_PACKAGES,
    ALLOW_INFERENCE_MODELS_UNTRUSTED_PACKAGES, API_KEY)
from inference.core.models.base import Model
from inference.core.models.inference_models_adapters import \
    get_extra_weights_provider_headers
from inference_models import AutoModel
from inference_models.models.moondream2.moondream2_hf import MoonDream2HF
from PIL import Image


class InferenceModelsMoondream2Adapter(Model):
    def __init__(self, model_id: str, api_key: str = None, **kwargs):
        super().__init__()

        self.metrics = {"num_inferences": 0, "avg_inference_time": 0.0}

        self.api_key = api_key if api_key else API_KEY

        self.task_type = "llm"

        extra_weights_provider_headers = get_extra_weights_provider_headers()

        self._model: MoonDream2HF = AutoModel.from_pretrained(
            model_id_or_path=model_id,
            api_key=self.api_key,
            allow_untrusted_packages=ALLOW_INFERENCE_MODELS_UNTRUSTED_PACKAGES,
            allow_direct_local_storage_loading=ALLOW_INFERENCE_MODELS_DIRECTLY_ACCESS_LOCAL_PACKAGES,
            extra_weights_provider_headers=extra_weights_provider_headers,
            **kwargs,
        )

    def caption(self, image_in: Union[Image.Image, np.array], **kwargs):
        if not isinstance(image_in, np.ndarray):
            np_img = np.asarray(image_in)  # RGB
            bgr_img = cv2.cvtColor(np_img, cv2.COLOR_RGB2BGR)
        else:
            bgr_img = image_in
        return self._model.caption(bgr_img)[0]
```