{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU",
    "gpuClass": "standard"
  },
  "cells": [
    {
      "cell_type": "code",
      "source": [
        "!pip install open_clip_torch transformers"
      ],
      "metadata": {
        "id": "JvaEkx8Cyvhg"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "execution_count": 18,
      "metadata": {
        "id": "vE4lFFkKyotX"
      },
      "outputs": [],
      "source": [
        "import open_clip\n",
        "import torch\n",
        "\n",
        "model, _, transform = open_clip.create_model_and_transforms(\n",
        "  model_name=\"coca_ViT-L-14\",\n",
        "  pretrained=\"mscoco_finetuned_laion2B-s13B-b90k\"\n",
        ")"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "!wget https://i.imgur.com/8H7XCH0.jpg -O cat.jpg"
      ],
      "metadata": {
        "id": "oOaE1AmDyth_"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from IPython.display import Image\n",
        "Image('cat.jpg')"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 407
        },
        "id": "Y9Q6bhVA2L01",
        "outputId": "1b920080-e8cd-4d2f-fb23-30e6f1b612f9"
      },
      "execution_count": 19,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "image/jpeg": "[encoded data, 59392 characters]\n",
            "text/plain": [
              "<IPython.core.display.Image object>"
            ]
          },
          "metadata": {},
          "execution_count": 19
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from PIL import Image\n",
        "im = Image.open(\"cat.jpg\").convert(\"RGB\")\n",
        "im = transform(im).unsqueeze(0)\n",
        "\n",
        "with torch.no_grad(), torch.cuda.amp.autocast():\n",
        "  generated = model.generate(im)\n",
        "\n",
        "print(open_clip.decode(generated[0]).split(\"<end_of_text>\")[0].replace(\"<start_of_text>\", \"\"))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "byZKXMGzyr5Y",
        "outputId": "122eb099-6704-4e3c-fa7c-a05dd87ce64f"
      },
      "execution_count": 22,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "an orange and white cat on top of a turtle . \n"
          ]
        }
      ]
    }
  ]
}