{ "cells": [ { "cell_type": "markdown", "id": "de636f8d", "metadata": {}, "source": [ "---\n", "# **Embeddings Notebook for scikit-learn/iris dataset**\n", "---" ] }, { "cell_type": "markdown", "id": "78be71fc", "metadata": {}, "source": [ "## 1. Setup necessary libraries and load the dataset" ] }, { "cell_type": "code", "execution_count": null, "id": "d46ffaa5", "metadata": {}, "outputs": [], "source": [ "# Install and import necessary libraries.\n", "!pip install pandas sentence-transformers faiss-cpu" ] }, { "cell_type": "code", "execution_count": null, "id": "b626ffea", "metadata": {}, "outputs": [], "source": [ "from sentence_transformers import SentenceTransformer\n", "import faiss" ] }, { "cell_type": "code", "execution_count": null, "id": "f27849e3", "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": "caec8151", "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": "07c92bc1", "metadata": {}, "source": [ "## 2. Loading embedding model and creating FAISS index" ] }, { "cell_type": "code", "execution_count": null, "id": "47c16d19", "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": "a26c66dc", "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": "0df3ef3e", "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": "aa031f7b", "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": "1101291a", "metadata": {}, "source": [ "## 3. Perform a text search" ] }, { "cell_type": "code", "execution_count": null, "id": "2a7f57f0", "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": "8f24048c", "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": "a056cb93", "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 }