class_docstr: Loader that loads the captions of an image
class_docstr_tok: ["Loader","that","loads","the","captions","of","an","image"]
docstr: Helper function for getting the captions and metadata of an image
docstr_tok: ["Helper","function","for","getting","the","captions","and","metadata","of","an","image"]
code: def _get_captions_and_metadata(
      self, model: Any, processor: Any, path_image: str, prompt=None
  ) -> Tuple[str, dict]:
      """
      Helper function for getting the captions and metadata of an image
      """
      if prompt is None:
          prompt = self.prompt
      try:
          from PIL import Image
      except ImportError:
          raise ValueError(
              "`PIL` package not found, please install with `pip install pillow`"
          )
  
      try:
          if path_image.startswith("http://") or path_image.startswith(
              "https://"
          ):
              image = Image.open(
                  requests.get(path_image, stream=True).raw
              ).convert("RGB")
          else:
              image = Image.open(path_image).convert("RGB")
      except Exception:
          raise ValueError(f"Could not get image data for {path_image}")
  
      import torch
  
      with torch.no_grad():
          with self.context_class(self.device):
              context_class_cast = (
                  NullContext
                  if self.device == "cpu"
                  else torch.autocast
              )
              with context_class_cast(self.device):
                  if self.load_half:
                      # FIXME: RuntimeError: "slow_conv2d_cpu" not implemented for 'Half'
                      inputs = processor(
                          image, prompt, return_tensors="pt"
                      )  # .half()
                  else:
                      inputs = processor(
                          image, prompt, return_tensors="pt"
                      )
                  min_length = len(prompt) // 4 + self.min_new_tokens
                  self.max_tokens = max(self.max_tokens, min_length)
                  inputs.to(model.device)
                  output = model.generate(
                      **inputs,
                      min_length=min_length,
                      max_length=self.max_tokens,
                  )
  
                  caption: str = processor.decode(
                      output[0], skip_special_tokens=True
                  )
                  prompti = caption.find(prompt)
                  if prompti >= 0:
                      caption = caption[prompti + len(prompt) :]
                  metadata: dict = {"image_path": path_image}
  
      return caption, metadata
  
file_path: h2ogpt/src/image_captions.py
imports: from typing import List, Union, Any, Tuple
  import requests
  from langchain.docstore.document import Document
  from langchain.document_loaders import ImageCaptionLoader
  from utils import get_device, NullContext, clear_torch_cache
  from importlib.metadata import distribution, PackageNotFoundError
label_desc: Use image node_id 7 for calling the H2OImageCaptionLoader obj's underlying member method code with example usage: obj._get_captions_and_metadata(model, processor, path_image, prompt) and returns: caption,  metadata