> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pinecone.io/llms.txt
> Use this file to discover all available pages before exploring further.

# CLIP

> CLIP (Contrastive Language–Image Pre-training) builds on a large body of work on zero-shot transfer, natural language supervision, and multimodal learning.

export const ModelPageTabs = ({inferenceEnabled}) => {
  const showGuides = () => {
    document.querySelector('.model-page-guides').style.display = 'flex';
    document.querySelector('.model-page-playground').style.display = 'none';
    document.querySelector('.guides').classList.add('active');
    document.querySelector('.playground').classList.remove('active');
  };
  const showPlayground = () => {
    document.querySelector('.model-page-guides').style.display = 'none';
    document.querySelector('.model-page-playground').style.display = 'block';
    document.querySelector('.guides').classList.remove('active');
    document.querySelector('.playground').classList.add('active');
  };
  return <div className="model-page-body">
        <div className="model-page-tabs">
          <button className={`guides active ${inferenceEnabled ? "" : "cursor-default"}`} onClick={inferenceEnabled ? showGuides : null}>
            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none">
              <path d="M6.5 16C7.28333 16 8.04583 16.0875 8.7875 16.2625C9.52917 16.4375 10.2667 16.7 11 17.05V7.2C10.3167 6.8 9.59167 6.5 8.825 6.3C8.05833 6.1 7.28333 6 6.5 6C5.9 6 5.30417 6.05833 4.7125 6.175C4.12083 6.29167 3.55 6.46667 3 6.7V16.6C3.58333 16.4 4.1625 16.25 4.7375 16.15C5.3125 16.05 5.9 16 6.5 16ZM13 17.05C13.7333 16.7 14.4708 16.4375 15.2125 16.2625C15.9542 16.0875 16.7167 16 17.5 16C18.1 16 18.6875 16.05 19.2625 16.15C19.8375 16.25 20.4167 16.4 21 16.6V6.7C20.45 6.46667 19.8792 6.29167 19.2875 6.175C18.6958 6.05833 18.1 6 17.5 6C16.7167 6 15.9417 6.1 15.175 6.3C14.4083 6.5 13.6833 6.8 13 7.2V17.05ZM12 20C11.2 19.3667 10.3333 18.875 9.4 18.525C8.46667 18.175 7.5 18 6.5 18C5.8 18 5.1125 18.0917 4.4375 18.275C3.7625 18.4583 3.11667 18.7167 2.5 19.05C2.15 19.2333 1.8125 19.225 1.4875 19.025C1.1625 18.825 1 18.5333 1 18.15V6.1C1 5.91667 1.04583 5.74167 1.1375 5.575C1.22917 5.40833 1.36667 5.28333 1.55 5.2C2.31667 4.8 3.11667 4.5 3.95 4.3C4.78333 4.1 5.63333 4 6.5 4C7.46667 4 8.4125 4.125 9.3375 4.375C10.2625 4.625 11.15 5 12 5.5C12.85 5 13.7375 4.625 14.6625 4.375C15.5875 4.125 16.5333 4 17.5 4C18.3667 4 19.2167 4.1 20.05 4.3C20.8833 4.5 21.6833 4.8 22.45 5.2C22.6333 5.28333 22.7708 5.40833 22.8625 5.575C22.9542 5.74167 23 5.91667 23 6.1V18.15C23 18.5333 22.8375 18.825 22.5125 19.025C22.1875 19.225 21.85 19.2333 21.5 19.05C20.8833 18.7167 20.2375 18.4583 19.5625 18.275C18.8875 18.0917 18.2 18 17.5 18C16.5 18 15.5333 18.175 14.6 18.525C13.6667 18.875 12.8 19.3667 12 20Z" fill="#1B17F5" />
            </svg>
            GUIDE
          </button>
          {false && <button className="playground" onClick={showPlayground}>
            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none">
              <path d="M10 16.5L16 12L10 7.5V16.5ZM12 2C6.48 2 2 6.48 2 12C2 17.52 6.48 22 12 22C17.52 22 22 17.52 22 12C22 6.48 17.52 2 12 2ZM12 20C7.59 20 4 16.41 4 12C4 7.59 7.59 4 12 4C16.41 4 20 7.59 20 12C20 16.41 16.41 20 12 20Z" fill="#72788D" />
            </svg>
            PLAYGROUND
          </button>}
        </div>
      </div>;
};

export const ModelPageCard = ({image, title, company, inferenceEnabled, metric, dimension, seq, task, price}) => {
  return <div className="model-page-card">
        <div className="model-page-card-title">
          {image && image !== "null" && <div className="model-page-card-logo">
              <img src={image} className="pointer-events-none" />
            </div>}

        <h1><span className="font-semibold">{title}</span> {company !== "null" && "| " + company}</h1>

        {inferenceEnabled === "true" && <div className="model-page-card-inference">
            <svg className="shrink-0" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none">
              <path d="M11.9995 2.01953C6.48953 2.01953 2.01953 6.48953 2.01953 11.9995C2.01953 17.5095 6.48953 21.9795 11.9995 21.9795C17.5095 21.9795 21.9795 17.5095 21.9795 11.9995C21.9795 6.48953 17.5095 2.01953 11.9995 2.01953ZM11.9995 19.9795C7.59953 19.9795 4.01953 16.3995 4.01953 11.9995C4.01953 7.59953 7.59953 4.01953 11.9995 4.01953C16.3995 4.01953 19.9795 7.59953 19.9795 11.9995C19.9795 16.3995 16.3995 19.9795 11.9995 19.9795ZM12.7495 4.99953L8.24953 13.4995H11.3895V18.9995L15.7495 10.4995H12.7495V4.99953Z" fill="var(--text-primary)" />
            </svg>
            Hosted
          </div>}
        </div>

        <div className="model-page-card-stats">
          {!!metric && metric.length > 0 && <div>
            <span>METRIC</span>
            <p>{metric.join(", ")}</p>
          </div>}

          {!!dimension && dimension.length > 0 && <div>
            <span>DIMENSION</span>
            <p>{dimension.join(", ")}</p>
          </div>}

          {!!seq && seq.length > 0 && <div>
            <span>MAX INPUT TOKENS</span>
            <p>{seq}</p>
          </div>}

          {!!task && task.length > 0 && <div>
            <span>TASK</span>
            <p>{task}</p>
          </div>}

          {price && price.length > 0 && <div className="price">
            <span>PRICE</span>
            <p>${price}</p>
          </div>}
        </div>
      </div>;
};

export const ModelPageHeader = () => {
  return <div style={{
    marginBottom: "2.1875rem"
  }}>
      <a href="/models" style={{
    display: "inline-flex",
    gap: "0.5rem",
    alignItems: "center",
    color: "#121142"
  }}>
        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" fill="none" style={{
    flexShrink: 0
  }}>
          <path d="M10.4703 4.94L9.53027 4L5.53027 8L9.53027 12L10.4703 11.06L7.41694 8L10.4703 4.94Z" fill="var(--brand-blue)" />
        </svg>
        Back to all models
      </a>
    </div>;
};

<div className="not-prose">
  <ModelPageHeader />

  <ModelPageCard image="https://cdn.sanity.io/images/vr8gru94/production/8e6637d244b5c43daff9dfc12be07c8f2f4e9016-250x250.svg" title="CLIP" company="OpenAI" inferenceEnabled="false" metric={["cosine", "dot product"]} dimension={["512", "768", "2048"]} seq="2048" task="embedding" price="" />

  <ModelPageTabs inferenceEnabled="false" />
</div>

<div className="model-page-guides">
  <div className="model-page-content">
    ### Overview

    CLIP (Contrastive Language–Image Pre-training) builds on a large body of work on zero-shot transfer, natural language supervision, and multimodal learning. It learns from unfiltered, highly varied, and highly noisy data, and is intended to be used in a zero-shot manner. CLIP struggles on more abstract or systematic tasks such as counting the number of objects in an image and on more complex tasks such as predicting how close the nearest car is in a photo.

    The model allows people to design their own classifiers and removes the need for task-specific training data.

    ### Using the model

    For best results, use a Jupyter Notebook to interact with this dataset.

    #### Installation:

    ```python theme={null}
    !pip install pinecone datasets transformers
    ```

    ### Create Index

    ```python theme={null}
    from pinecone import Pinecone, ServerlessSpec

    pc = Pinecone(api_key="API_KEY")

    # Create Index
    index_name = "clip-vit-base-patch32"

    if not pc.has_index(index_name):
      pc.create_index(
          name=index_name,
          dimension=512,
          metric="cosine",
          spec=ServerlessSpec(
              cloud='aws',
              region='us-east-1'
          )
      )

    index = pc.Index(index_name)
    ```

    ### Embed & Upsert

    ```python theme={null}

    # Embed data
    # We'll use an example dataset of images of animals and cities:

    from datasets import load_dataset


    data = load_dataset(
        "jamescalam/image-text-demo",
        split="train"
    )

    from transformers import CLIPProcessor, CLIPModel
    import torch

    model_id = "openai/clip-vit-base-patch32"

    processor = CLIPProcessor.from_pretrained(model_id)
    model = CLIPModel.from_pretrained(model_id)

    # move model to device if possible
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    model.to(device)


    # ClIP allows for both text and image embeddings

    def create_text_embeddings(text):
      text_embedding = processor(text=text,
          padding=True,
          images=None,
          return_tensors='pt').to(device)
        
      text_emb = model.get_text_features(**text_embedding)
      return text_emb[0]

    def create_image_embeddings(image):
      vals = processor(
          text=None,
          images=image,
          return_tensors='pt')['pixel_values'].to(device)
      image_embedding = model.get_image_features(vals)
      return image_embedding[0]


    # We will embed the images and search with text

    from IPython.display import Image 

    def apply_vectorization(data):

      data["image_embeddings"] = create_image_embeddings(data["image"])
      return data



    data = data.map(apply_vectorization)
    # add an id column for easy indexing later
    ids = [str(i) for i in range(0, data.num_rows)]
    data = data.add_column("id", ids)


    vectors = []
    for i in range(0, data.num_rows):
      d = data[i]
      vectors.append({
          "id": d["id"],
          "values": d["image_embeddings"],
          "metadata": {"caption": d["text"]}
      })

    index.upsert(
        vectors=vectors,
        namespace="ns1"
    )


    ```

    ### Query

    ```python theme={null}
    query = "Show me a photo of a city"

    x = create_text_embeddings(query).tolist()

    results = index.query(
        namespace="ns1",
        vector=x,
        top_k=3,
        include_values=False,
        include_metadata=True
    )

    print(results)


    def id_to_image_helper(id, data):
      # given id, renders the images and captions
      # resizes in order to speed up showing the image

      image = data[int(id)]
      print(image["text"])
      return image["image"].resize((500, 500))


    # view a specific result using the helper
      id_to_image_helper(results["matches"][0]["id"], data)




    ```
  </div>

  <div className="model-page-sidebar">
    <div className="iframe-placeholder">
      <iframe className="" src="https://www.pinecone.io/tools/index-creation/?indexName=CLIP&metrics=cosine,dot product&dimensions=512,768,2048&cloud=aws&region=us-east-1" width="100%" height="1000px" allow="clipboard-write allow-popups" allowTransparency="true" />
    </div>
  </div>
</div>

<div className="model-page-playground not-prose">
  Lorem Ipsum
</div>
