{ "cells": [ { "cell_type": "markdown", "source": [ "https://github.com/lilianweng/emoji-semantic-search/blob/main/server/app.py" ], "metadata": { "id": "c-4HGfHNbNI4" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "lIYdn1woOS1n" }, "outputs": [], "source": [ "!pip install -U -q transformers sentence-transformers datasets jsonlines" ] }, { "cell_type": "code", "source": [ "!wget https://github.com/lilianweng/emoji-semantic-search/raw/refs/heads/main/server/emoji-embeddings.jsonl.gz" ], "metadata": { "id": "Vg_Yz2KJVaGo", "outputId": "9da3682e-e166-43ee-dde5-8e40dfa9af77", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "--2024-12-08 08:15:22-- https://github.com/lilianweng/emoji-semantic-search/raw/refs/heads/main/server/emoji-embeddings.jsonl.gz\n", "Resolving github.com (github.com)... 140.82.114.3\n", "Connecting to github.com (github.com)|140.82.114.3|:443... connected.\n", "HTTP request sent, awaiting response... 302 Found\n", "Location: https://raw.githubusercontent.com/lilianweng/emoji-semantic-search/refs/heads/main/server/emoji-embeddings.jsonl.gz [following]\n", "--2024-12-08 08:15:22-- https://raw.githubusercontent.com/lilianweng/emoji-semantic-search/refs/heads/main/server/emoji-embeddings.jsonl.gz\n", "Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.110.133, 185.199.109.133, 185.199.108.133, ...\n", "Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.110.133|:443... connected.\n", "HTTP request sent, awaiting response... 200 OK\n", "Length: 49086598 (47M) [application/octet-stream]\n", "Saving to: ‘emoji-embeddings.jsonl.gz’\n", "\n", "emoji-embeddings.js 100%[===================>] 46.81M 287MB/s in 0.2s \n", "\n", "2024-12-08 08:15:24 (287 MB/s) - ‘emoji-embeddings.jsonl.gz’ saved [49086598/49086598]\n", "\n" ] } ] }, { "cell_type": "code", "source": [ "import gzip\n", "import os\n", "import jsonlines\n", "import pandas as pd\n", "\n", "# Filepath to the compressed embeddings file\n", "EMBED_FILE = \"./emoji-embeddings.jsonl.gz\" # Replace with the correct path\n", "\n", "def load_emoji_embeddings_to_dataframe(embed_file: str) -> pd.DataFrame:\n", " \"\"\"\n", " Loads emoji embeddings and metadata into a pandas DataFrame.\n", "\n", " Args:\n", " embed_file (str): Path to the gzip-compressed JSONL file containing emoji embeddings.\n", "\n", " Returns:\n", " pd.DataFrame: A DataFrame with columns for emoji, message, and embedding.\n", " \"\"\"\n", " with gzip.GzipFile(fileobj=open(embed_file, \"rb\"), mode=\"rb\") as fin:\n", " emoji_info = list(jsonlines.Reader(fin))\n", "\n", " # Convert the data into a DataFrame\n", " df = pd.DataFrame(emoji_info)\n", "\n", " # Retain \"embed\" as a list within a single column\n", " df[\"embed\"] = df[\"embed\"].apply(lambda x: list(x))\n", " return df\n", "\n", "# Usage\n", "emoji_df = load_emoji_embeddings_to_dataframe(EMBED_FILE)\n", "\n", "# Display the structure of the DataFrame (columns: emoji, message, embed)\n", "print(emoji_df.head())" ], "metadata": { "id": "u3hDX1RGVmLL", "outputId": "82db94e0-3fb6-4b32-8278-f703289bda3d", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ " emoji message \\\n", "0 🥇 1st place medal \n", "1 🥈 2nd place medal \n", "2 🥉 3rd place medal \n", "3 🆎 ab button (blood type) \n", "4 🏧 automated teller machine \n", "\n", " embed \n", "0 [-0.017666909843683243, -0.0048620556481182575... \n", "1 [-0.02306770719587803, -0.0014851171290501952,... \n", "2 [-0.02444339357316494, -0.0023657570127397776,... \n", "3 [-0.02489371970295906, -0.024508081376552582, ... \n", "4 [-0.02711508981883526, -0.017816530540585518, ... \n" ] } ] }, { "cell_type": "code", "source": [ "# Requires sentence_transformers>=2.7.0\n", "\n", "from sentence_transformers import SentenceTransformer\n", "from sentence_transformers.util import cos_sim, semantic_search\n", "\n", "sentences = ['That is a happy person', 'That is a very happy person']\n", "\n", "model = SentenceTransformer('Alibaba-NLP/gte-large-en-v1.5', trust_remote_code=True)\n", "embeddings = model.encode(sentences)\n", "print(cos_sim(embeddings[0], embeddings[1]))" ], "metadata": { "id": "1gL6zdczWNdc", "outputId": "c6a93064-3a7d-44a8-c0cf-d286775a371f", "colab": { "base_uri": "https://localhost:8080/", "height": 522, "referenced_widgets": [ "a71e869c97e54ac7bb4853a5da21ceae", "d691fc510fce434287ea8ed326afc36b", "ac60f85f718c43d0811ae18e670ce1e9", "793fa71d77114bf387313c49cc9164b3", "23b100d68972483c81d9ab95898c35b8", "fb9ba51df9434f6699ace8aff54c2d3e", "931e9bcacbb04d53a65a7d392b5a393c", "ce11e7dd6c4648999af2939bddf4e41a", "e7a34f1b1c5149648602a54cbb36ad1a", "d7b009d044f74ae09630f9a2151679a1", "e8fca0c985324c1dbb3c4073eb65a3f6", "fd73c70aed964b20844db3d736a15fdf", "c30f15dd3d8841a286c3cc75f9549cac", "cba348922e4243f7b05a093f503b0fea", "32ad1ab96b284597b13cd6206f7af909", "264f17a9ed644da2885b5ba9a221f959", "ace97fa77e584cf989debddb30f23902", "f038b9a1dc1840b1b2422a2ade5f4d9f", "165b4c1d2d024b94b7db5661af3548d6", "1d128486b03e4c57a6aed7c129cb96cb", "c3164b6294f74d50b227c00bfcaf83c2", "50366bc010f74beda1995a30b61eb453", "d3a8519d463c4c3c99b67139619c0142", "761cb4786c75449baf8277bf7b5984fc", "f2d13d9005f6417183a9de8cd2f23081", "fea01bcd68b54f99bca51668a0b4624d", "112fe6a7be3d48e88c27b3922b115293", "31aa59118e044ffbb2e24633da18b71f", "4acec2fc50004b26911ac9d0ca083678", "bcadd98fe0e74236b1d4296507a26efe", "c4feb0f13bd04ee9a0a8c11a490c1090", "98888cd9afb049eca66d706bbcff54fa", "5e4030cfd89d468e8e4e10daf1de7670", "6e2706b85701405fa0a4b4b09e98a622", "ce4c0c55b0014907b3acbe0dc41dd296", "bc2be83a357b404f9d4057b6749145e1", "bd5d97e3dada42e2868349e5913740a5", "fb77ea473f62417aa4dfaf95a7a7d96d", "e1c6e91d77f349ed9c2d60b257c5166b", "1d8225a54cc04bf78af832a54401d854", "adc1425bc9014912b93c353a47bafd4a", "4a89ce4a488749458cd6859d1e1e7c47", "26b614a267ab481e8c32e91759dbdc8b", "a0a968eb5c21402e9eab4d8bb9e0c99c", "f86d9bc8a9b74733964a794b50e66d34", "950912bbf8f14dd89707d185fe591418", "3ccfe94fed694437aa0366cff7cb0d9f", "7d696c118078425a9eb78b404a56d409", "cf70a3ef7b51480c89c2630158526a4e", "62d013aaa77c473fba80c7e63b5fab11", "080ac6e31dbe4712865882cf0dec30fd", "3b3526ded5d94520baadc94d5281a25b", "07a14bb813754a53a77bafda42826377", "8fc3a6689f744247ab847c4c821537a6", "ff60a577bb9d43439c59ba89b9789c2c", "28241e00c2a841f4a322c7f962964769", "f2ffaaf1bf6343cebd49a413a606d334", "366f12337c0a49c6ba5e0a2b625c7dac", "baec3a8427c34a51aa0f2f75ffccdaa3", "e040933f65444fa7b3c00482ab4a4d83", "78d1f24136df47cf910e848ac07b87ae", "f0fdc12408214b6087f5a64c52846f09", "8a7c249c02a647489d00b8c64faaa494", "dc6b20c21e5c418cb87d04a1cd9d3750", "f31cc2ebbcac4b37bbb4a49c37f5dd37", "8c67f2d6e24e422da483238953f0aae8", "c44fbb34b4a443748f16886c6782c018", "57c00f12fb5d4750ae86ff3030c6babb", "465b6d061add448ea746faed72e08314", "e88e91321de44dd083c4b53af63110ca", "7a8c33cbbe8940f7ae3a57f260394b95", "1b6795a0edbb4889b35c2a20498cce03", "f82be38199ee4389a0f0f931b8772356", "ba90e9e9cec5464d890436d7821f2f89", "7b8dd78ca6a14c8b979fc1175ac8c985", "0dcbf71d5e614f6f9d49d9b47fa4ab4f", "4e724ca19c1245f8b9a0207eb1473c8d", "32a7648614494234a666cd6fab7a3589", "92b8a8b0fa324a7e87bfa2679581a7f3", "0f9af74c01304a9da9145b254c58682a", "265f7f8810c1462189b97a08226f5895", "25b7cf36bf6144f69ea29fb89764fdd9", "cd41c2db678e47b495c8be7ba3c76c72", "46eda31be3ca4af8b4989c71413de34a", "5bada29107ff4ddb8f0e02b88c06e4a2", "12fb9fdaf95a49ea8006c6c22f77e858", "413fd4f79fde4c7bbc76ff305ff47ae3", "c0abd3893fac4558943a868db1f3eb01", "b82f63b9711243ae84208fa962ff8874", "4953834da9224147b0cb8b6fdbdcda31", "d724a33906884444b69a54a8e71a95a6", "40bd88564d244d8fb25f679ad4f43d14", "88d3cee730e643afaa3f9d6dabf49b80", "c392e76ad0a14726a843b6cd85d470b8", "75ba7a7b7aaf40fe8e5325a66185a4a4", "6172d6ac7945433297c7281c71c6d504", "6664354e9b8944eb8f64c9396509ce5c", "e903e8896600475686209b3c3f8e6f26", "b6f40333c3624135b1a1175496df9f45", "7801f75bb27e4e68941559a92b896933", "5a85b7bf9f4749daba4969c9a1e6ac88", "987dcb10ab40466faef3a4189f1eefe6", "bc328133b9e94e9993a1f5bcbd463924", "5dbe0c18ba884f33babee7801a757026", "26d75734fe87409a888696adc0432ec1", "3bc5d614dd074147b2e4188bb5c58a0f", "3eb3dc5403104d909ce5bc370dfc11dd", "5bd72e90ccb24f6a8f8b542bc54839a3", "ad31259f59164664b0ebeda18bd53a60", "aa1a38c98b9e4bf8b1eaddf845010327", "7a94fb24dfa743dab16b8ffd765eeae7", "42830fa96f234883b3ff10da6650961f", "7d6da3caba6e4fc186a17f0135639cd1", "2c58540693404bd9bdc7179ffa6b1c2d", "467b6881a2de4f21889d2d43dfa606d8", "242ccde6f6904ab0aebeffde9d1a696e", "7992b83045ff4292920a079bba464f52", "02cb915e009a484bb9f6e33212f507f5", "bf2a28d3ad8a46e88f0ca26ad344de27", "0eb87ad81a07415d900a4cd13a79c033", "93f13043ed3740ebb86895c5056c94c5", "f05d72d9e3c742f897c9381be492948f", "13fb36f2f78d43d79826f45a100de4d2", "4972d3eff3ae4e87813d2219594b1719", "d646d0c3dc5b4393925e54f6b633fa8f", "4e5addfbc7b643f29eed22c3ea925e9b", "f8f660b0da9c46939deea0aa7d5bc0aa", "98cecfa3dbf3493cbda8a72237850af3", "fd502532293941d08666e958b40374af", "b3f5e00552ad4a3f81fc111475002228", "db030ad14d4e492e9b14f16283b05e90", "b9c855d32eb94505a16cb409cdbf9ab5" ] } }, "execution_count": null, "outputs": [ { "output_type": "display_data", "data": { "text/plain": [ "modules.json: 0%| | 0.00/229 [00:00, ?B/s]" ], "application/vnd.jupyter.widget-view+json": { "version_major": 2, "version_minor": 0, "model_id": "a71e869c97e54ac7bb4853a5da21ceae" } }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "README.md: 0%| | 0.00/71.8k [00:00, ?B/s]" ], "application/vnd.jupyter.widget-view+json": { "version_major": 2, "version_minor": 0, "model_id": "fd73c70aed964b20844db3d736a15fdf" } }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "sentence_bert_config.json: 0%| | 0.00/54.0 [00:00, ?B/s]" ], "application/vnd.jupyter.widget-view+json": { "version_major": 2, "version_minor": 0, "model_id": "d3a8519d463c4c3c99b67139619c0142" } }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "config.json: 0%| | 0.00/1.35k [00:00, ?B/s]" ], "application/vnd.jupyter.widget-view+json": { "version_major": 2, "version_minor": 0, "model_id": "6e2706b85701405fa0a4b4b09e98a622" } }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "configuration.py: 0%| | 0.00/7.13k [00:00, ?B/s]" ], "application/vnd.jupyter.widget-view+json": { "version_major": 2, "version_minor": 0, "model_id": "f86d9bc8a9b74733964a794b50e66d34" } }, "metadata": {} }, { "output_type": "stream", "name": "stderr", "text": [ "A new version of the following files was downloaded from https://huggingface.co/Alibaba-NLP/new-impl:\n", "- configuration.py\n", ". Make sure to double-check they do not contain any added malicious code. To avoid downloading new versions of the code file, you can pin a revision.\n" ] }, { "output_type": "display_data", "data": { "text/plain": [ "modeling.py: 0%| | 0.00/59.0k [00:00, ?B/s]" ], "application/vnd.jupyter.widget-view+json": { "version_major": 2, "version_minor": 0, "model_id": "28241e00c2a841f4a322c7f962964769" } }, "metadata": {} }, { "output_type": "stream", "name": "stderr", "text": [ "A new version of the following files was downloaded from https://huggingface.co/Alibaba-NLP/new-impl:\n", "- modeling.py\n", ". Make sure to double-check they do not contain any added malicious code. To avoid downloading new versions of the code file, you can pin a revision.\n" ] }, { "output_type": "display_data", "data": { "text/plain": [ "model.safetensors: 0%| | 0.00/1.74G [00:00, ?B/s]" ], "application/vnd.jupyter.widget-view+json": { "version_major": 2, "version_minor": 0, "model_id": "c44fbb34b4a443748f16886c6782c018" } }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "tokenizer_config.json: 0%| | 0.00/1.38k [00:00, ?B/s]" ], "application/vnd.jupyter.widget-view+json": { "version_major": 2, "version_minor": 0, "model_id": "32a7648614494234a666cd6fab7a3589" } }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "vocab.txt: 0%| | 0.00/232k [00:00, ?B/s]" ], "application/vnd.jupyter.widget-view+json": { "version_major": 2, "version_minor": 0, "model_id": "b82f63b9711243ae84208fa962ff8874" } }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "tokenizer.json: 0%| | 0.00/712k [00:00, ?B/s]" ], "application/vnd.jupyter.widget-view+json": { "version_major": 2, "version_minor": 0, "model_id": "7801f75bb27e4e68941559a92b896933" } }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "special_tokens_map.json: 0%| | 0.00/695 [00:00, ?B/s]" ], "application/vnd.jupyter.widget-view+json": { "version_major": 2, "version_minor": 0, "model_id": "7a94fb24dfa743dab16b8ffd765eeae7" } }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "1_Pooling/config.json: 0%| | 0.00/297 [00:00, ?B/s]" ], "application/vnd.jupyter.widget-view+json": { "version_major": 2, "version_minor": 0, "model_id": "f05d72d9e3c742f897c9381be492948f" } }, "metadata": {} }, { "output_type": "stream", "name": "stdout", "text": [ "tensor([[0.9769]])\n" ] } ] }, { "cell_type": "code", "source": [ "texts = emoji_df.message.tolist()\n", "emoji_embeds = model.encode(texts)" ], "metadata": { "id": "P5gji_0cXN8e" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "emoji_embeds" ], "metadata": { "id": "xKFOPmv-XcNB", "outputId": "82ddf06b-1345-4aed-87fe-b2589a43c98f", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([[-0.70429766, 0.40987685, -0.40623984, ..., 0.34447536,\n", " -1.1676097 , 0.60690004],\n", " [-0.8834242 , 0.41458154, -0.52103883, ..., 0.6835418 ,\n", " -0.8694557 , 0.26585358],\n", " [-0.8940514 , 0.4233047 , -1.1000963 , ..., -0.2655996 ,\n", " -0.5408825 , 0.51726294],\n", " ...,\n", " [-1.0322247 , 0.31219995, -0.61489904, ..., 0.3374904 ,\n", " -0.7023771 , -1.6981932 ],\n", " [-0.16666836, 0.6253868 , -0.15234776, ..., 1.9760971 ,\n", " -0.6701496 , -0.44981623],\n", " [-0.5770006 , -0.17587984, 0.08439391, ..., 1.3222466 ,\n", " 0.20345928, 0.0735295 ]], dtype=float32)" ] }, "metadata": {}, "execution_count": 11 } ] }, { "cell_type": "code", "source": [ "from sentence_transformers.util import cos_sim, semantic_search\n", "query_text = \"urgent exclamation\"\n", "query_embed = model.encode(query_text)\n", "hits = semantic_search(query_embed, emoji_embeds, top_k=5)" ], "metadata": { "id": "9sgbZdMdX21h" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "hits" ], "metadata": { "id": "rgBO_YU-Yjd2", "outputId": "88bea4ec-fbad-4367-fb4f-b01106c58322", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[[{'corpus_id': 3153, 'score': 0.7861719727516174},\n", " {'corpus_id': 877, 'score': 0.7329143285751343},\n", " {'corpus_id': 1031, 'score': 0.7237887382507324},\n", " {'corpus_id': 3154, 'score': 0.721806526184082},\n", " {'corpus_id': 2749, 'score': 0.70970618724823}]]" ] }, "metadata": {}, "execution_count": 14 } ] }, { "cell_type": "code", "source": [ "emoji_df.loc[2749]" ], "metadata": { "id": "PbYHZ0YcYkav", "outputId": "ee778775-c9b3-4cad-ed5d-097f3b4d68d1", "colab": { "base_uri": "https://localhost:8080/", "height": 178 } }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "emoji ❗\n", "message heavy exclamation mark symbol\n", "embed [-0.011823254637420177, -0.02413378655910492, ...\n", "Name: 2749, dtype: object" ], "text/html": [ "
\n", " | 2749 | \n", "
---|---|
emoji | \n", "❗ | \n", "
message | \n", "heavy exclamation mark symbol | \n", "
embed | \n", "[-0.011823254637420177, -0.02413378655910492, ... | \n", "