{ "cells": [ { "cell_type": "markdown", "id": "1e770825", "metadata": {}, "source": [ "---\n", "# **Embeddings Notebook for scikit-learn/iris dataset**\n", "---" ] }, { "cell_type": "markdown", "id": "1c8e5109", "metadata": {}, "source": [ "## 1. Setup necessary libraries and load the dataset" ] }, { "cell_type": "code", "execution_count": null, "id": "6c9cae3d", "metadata": {}, "outputs": [], "source": [ "# Install and import necessary libraries.\n", "!pip install pandas sentence-transformers faiss-cpu" ] }, { "cell_type": "code", "execution_count": null, "id": "53f2f84a", "metadata": {}, "outputs": [], "source": [ "from sentence_transformers import SentenceTransformer\n", "import faiss" ] }, { "cell_type": "code", "execution_count": null, "id": "152710e6", "metadata": {}, "outputs": [], "source": [ "# Load the dataset as a DataFrame\n", "import pandas as pd\n", "\n", "df = pd.read_csv(\"hf://datasets/scikit-learn/iris/Iris.csv\")" ] }, { "cell_type": "code", "execution_count": null, "id": "35085609", "metadata": {}, "outputs": [], "source": [ "# Specify the column name that contains the text data to generate embeddings\n", "column_to_generate_embeddings = 'Species'" ] }, { "cell_type": "markdown", "id": "a774b21f", "metadata": {}, "source": [ "## 2. Loading embedding model and creating FAISS index" ] }, { "cell_type": "code", "execution_count": null, "id": "bd3d6e6f", "metadata": {}, "outputs": [], "source": [ "# Remove duplicate entries based on the specified column\n", "df = df.drop_duplicates(subset=column_to_generate_embeddings)" ] }, { "cell_type": "code", "execution_count": null, "id": "4bac4e77", "metadata": {}, "outputs": [], "source": [ "# Convert the column data to a list of text entries\n", "text_list = df[column_to_generate_embeddings].tolist()" ] }, { "cell_type": "code", "execution_count": null, "id": "5258d88e", "metadata": {}, "outputs": [], "source": [ "# Specify the embedding model you want to use\n", "model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')" ] }, { "cell_type": "code", "execution_count": null, "id": "4f274463", "metadata": {}, "outputs": [], "source": [ "vectors = model.encode(text_list)\n", "vector_dimension = vectors.shape[1]\n", "\n", "# Initialize the FAISS index with the appropriate dimension (384 for this model)\n", "index = faiss.IndexFlatL2(vector_dimension)\n", "\n", "# Encode the text list into embeddings and add them to the FAISS index\n", "index.add(vectors)" ] }, { "cell_type": "markdown", "id": "8c2bebdd", "metadata": {}, "source": [ "## 3. Perform a text search" ] }, { "cell_type": "code", "execution_count": null, "id": "b3966941", "metadata": {}, "outputs": [], "source": [ "# Specify the text you want to search for in the list\n", "text_to_search = text_list[0]\n", "print(f\"Text to search: {text_to_search}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "2322f493", "metadata": {}, "outputs": [], "source": [ "# Generate the embedding for the search query\n", "query_embedding = model.encode([text_to_search])" ] }, { "cell_type": "code", "execution_count": null, "id": "f67cd14a", "metadata": {}, "outputs": [], "source": [ "# Perform the search to find the 'k' nearest neighbors (adjust 'k' as needed)\n", "D, I = index.search(query_embedding, k=10)\n", "\n", "# Print the similar documents\n", "print(f\"Similar documents: {[text_list[i] for i in I[0]]}\")" ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 5 }