{ "cells": [ { "cell_type": "markdown", "id": "p1dCsisKaCPx", "metadata": { "id": "p1dCsisKaCPx" }, "source": [ "Install prerequisite packages." ] }, { "cell_type": "code", "execution_count": null, "id": "KtsIcBFOZ4Wc", "metadata": { "id": "KtsIcBFOZ4Wc" }, "outputs": [], "source": [ "!pip install -U datasets sentence-transformers pinecone-client tqdm" ] }, { "cell_type": "markdown", "id": "7bb8e8fd", "metadata": { "id": "7bb8e8fd" }, "source": [ "# YouTube Indexing and Queries\n", "\n", "In this notebook we will work through an example of indexing and querying the YouTube video transcriptions data. We start by loading the dataset." ] }, { "cell_type": "code", "execution_count": null, "id": "e43af099", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "e43af099", "outputId": "cda841cc-95be-4bb2-bc9b-7990a7748918" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Using custom data configuration pinecone--yt-transcriptions-b2ddba2bc158a89e\n", "Reusing dataset json (/Users/jamesbriggs/.cache/huggingface/datasets/json/pinecone--yt-transcriptions-b2ddba2bc158a89e/0.0.0/ac0ca5f5289a6cf108e706efcf040422dbbfa8e658dee6a819f20d76bb84d26b)\n" ] }, { "data": { "text/plain": [ "Dataset({\n", " features: ['video_id', 'text', 'start_second', 'end_second', 'url', 'title', 'thumbnail'],\n", " num_rows: 11298\n", "})" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from datasets import load_dataset\n", "\n", "ytt = load_dataset(\n", " \"pinecone/yt-transcriptions\",\n", " split=\"train\",\n", " revision=\"926a45\"\n", ")\n", "ytt" ] }, { "cell_type": "markdown", "id": "3db35007", "metadata": { "id": "3db35007" }, "source": [ "Each sample includes video-level information (ID, title, url and thumbnail) and snippet-level information (text, start_second, end_second)." ] }, { "cell_type": "code", "execution_count": null, "id": "e6e73d96", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "e6e73d96", "outputId": "1a807672-e5da-4c52-e10a-6066326a0b42" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'video_id': 'ZPewmEu7644', 'text': \" hi this is Jeff Dean welcome to applications of deep neural networks of Washington University in this video we're going to look at how we can use ganz to generate additional training data for the latest on my a I course and projects click subscribe in the bell next to it to be notified of every new video Dan's have a wide array of uses beyond just the face generation that you\", 'start_second': 0, 'end_second': 20, 'url': 'https://www.youtube.com/watch?v=ZPewmEu7644&t=0s', 'title': 'GANS for Semi-Supervised Learning in Keras (7.4)', 'thumbnail': 'https://i.ytimg.com/vi/ZPewmEu7644/maxresdefault.jpg'}\n" ] } ], "source": [ "for x in ytt:\n", " print(x)\n", " break" ] }, { "cell_type": "markdown", "id": "b38004c9", "metadata": { "id": "b38004c9" }, "source": [ "# Uploading Documents to Pinecone Index\n", "\n", "The next step is indexing this dataset in Pinecone, for this we need a sentence transformer model (to encode the text into embeddings), and a Pinecone index.\n", "\n", "We will initialize the sentence transformer first." ] }, { "cell_type": "code", "execution_count": null, "id": "7f6b8e87", "metadata": { "id": "7f6b8e87", "outputId": "67e7f141-84aa-416c-beee-ccfeefd433f8" }, "outputs": [ { "data": { "text/plain": [ "SentenceTransformer(\n", " (0): Transformer({'max_seq_length': 128, 'do_lower_case': False}) with Transformer model: MPNetModel \n", " (1): Pooling({'word_embedding_dimension': 768, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': True, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False})\n", " (2): Normalize()\n", ")" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sentence_transformers import SentenceTransformer\n", "\n", "retriever = SentenceTransformer('flax-sentence-embeddings/all_datasets_v3_mpnet-base')\n", "retriever" ] }, { "cell_type": "markdown", "id": "a02ff222", "metadata": { "id": "a02ff222" }, "source": [ "We can see the embedding dimension of `768` above, we will need this when creating our Pinecone index." ] }, { "cell_type": "code", "execution_count": null, "id": "69fa3db9", "metadata": { "id": "69fa3db9", "outputId": "1b00b6d9-7cb4-409d-f97d-6085ff52d69c" }, "outputs": [ { "data": { "text/plain": [ "768" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "embed_dim = retriever.get_sentence_embedding_dimension()\n", "embed_dim" ] }, { "cell_type": "markdown", "id": "bcea76e1", "metadata": { "id": "bcea76e1" }, "source": [ "Now we can initialize our index." ] }, { "cell_type": "code", "execution_count": null, "id": "fc94564a", "metadata": { "id": "fc94564a" }, "outputs": [], "source": [ "import pinecone\n", "\n", "# get api key from app.pinecone.io\n", "pinecone.init(\n", " api_key=\"<>\",\n", " environment=\"us-west1-gcp\"\n", ")\n", "\n", "# create index\n", "pinecone.create_index(\n", " \"youtube-search\",\n", " dimension=embed_dim,\n", " metric=\"cosine\"\n", ")\n", "\n", "# connect to new index\n", "index = pinecone.Index(\"youtube-search\")" ] }, { "cell_type": "markdown", "id": "7b936d2b", "metadata": { "id": "7b936d2b" }, "source": [ "We will index our data in batches of `64`, the data we insert into our index will contain records (eg *documents*) containing a unique document/snippet ID, embedding, and metadata in the format:\n", "\n", "```json\n", "{\n", " 'doc-id',\n", " [0.0, 0.3, 0.1, ...],\n", " {'title': '???', 'start_seconds': 12, ...}\n", "}\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "a71fca5f", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "referenced_widgets": [ "428965e30ec84e56b9e8b7be96be8320" ] }, "id": "a71fca5f", "outputId": "11b97ee1-ee4e-4ad9-fdde-9a2e2a13e048" }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "428965e30ec84e56b9e8b7be96be8320", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/177 [00:00