{ "cells": [ { "cell_type": "markdown", "id": "0ca77529", "metadata": {}, "source": [ "---\n", "# **Embeddings Notebook for scikit-learn/iris dataset**\n", "---" ] }, { "cell_type": "markdown", "id": "ca62cfc7", "metadata": {}, "source": [ "## 1. Setup necessary libraries and load the dataset" ] }, { "cell_type": "code", "execution_count": null, "id": "80132099", "metadata": {}, "outputs": [], "source": [ "# Install and import necessary libraries.\n", "!pip install pandas sentence-transformers faiss-cpu" ] }, { "cell_type": "code", "execution_count": null, "id": "bd10aa7b", "metadata": {}, "outputs": [], "source": [ "from sentence_transformers import SentenceTransformer\n", "import faiss" ] }, { "cell_type": "code", "execution_count": null, "id": "01fb2f8e", "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": "d16d4199", "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": "01765f8f", "metadata": {}, "source": [ "## 2. Loading embedding model and creating FAISS index" ] }, { "cell_type": "code", "execution_count": null, "id": "30c844c1", "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": "c452fd90", "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": "f402d7a5", "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": "78bdea9c", "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": "a55a9c05", "metadata": {}, "source": [ "## 3. Perform a text search" ] }, { "cell_type": "code", "execution_count": null, "id": "9036d2e7", "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": "3f6f304f", "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": "3d1857fe", "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 }