Spaces:
Runtime error
Runtime error
File size: 6,718 Bytes
eeebb7c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"import gradio as gr\n",
"import openai\n",
"import pandas as pd \n",
"import numpy as np\n",
"\n",
"openai.api_key=\"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\"\n",
"from openai.embeddings_utils import get_embedding\n",
"from openai.embeddings_utils import cosine_similarity"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [],
"source": [
"def similarity(input_text):\n",
" df= pd.read_csv(\"meg_embeddings.csv\")\n",
" df['embedding'] = df['embedding'].apply(eval).apply(np.array)\n",
" input = input_text\n",
" input_vector = get_embedding(input, engine=\"text-embedding-ada-002\")\n",
" df[\"similarities\"] = df['embedding'].apply(lambda x: cosine_similarity(x, input_vector))\n",
" sorted_df =df.sort_values(\"similarities\", ascending=False)\n",
" top_row = sorted_df.loc[0]\n",
" return sorted_df.iloc[0][\"text\"]\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"For example, one participant said they got ‘annoyed by everything’, yet ‘I often don’t even want to like, point out things that technicians are doing that I'm getting annoyed by because I feel like it’s not worth the trouble’\n"
]
}
],
"source": [
"input_text = \"\"\"For example, one participant said they got ‘annoyed by everything’, yet ‘I often don’t even want to like, point out things that technicians are doing that I'm getting annoyed by because I feel like it’s not worth the trouble’\"\"\" \n",
"print(similarity(input_text))\n"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"c:\\Users\\vaibh\\anaconda3\\lib\\site-packages\\gradio\\inputs.py:27: UserWarning: Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components\n",
" warnings.warn(\n",
"c:\\Users\\vaibh\\anaconda3\\lib\\site-packages\\gradio\\deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect\n",
" warnings.warn(value)\n",
"c:\\Users\\vaibh\\anaconda3\\lib\\site-packages\\gradio\\deprecation.py:40: UserWarning: `numeric` parameter is deprecated, and it has no effect\n",
" warnings.warn(value)\n",
"c:\\Users\\vaibh\\anaconda3\\lib\\site-packages\\gradio\\outputs.py:197: UserWarning: Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components\n",
" warnings.warn(\n",
"c:\\Users\\vaibh\\anaconda3\\lib\\site-packages\\gradio\\deprecation.py:40: UserWarning: The 'type' parameter has been deprecated. Use the Number component instead.\n",
" warnings.warn(value)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running on local URL: http://127.0.0.1:7882\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7882/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": []
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"input_text = gr.inputs.Textbox(label=\"Input Text\")\n",
"\n",
"\n",
"output_label = gr.outputs.Label(label=\"Similarity Text\")\n",
"\n",
"gr.Interface(fn=similarity, inputs=[input_text], outputs=output_label, title=\"Semantic Similarity Checker\", description=\"Check if input text is semantically similar to file saved locally using Ada text embeddings.\").launch()\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Unnamed: 0.1</th>\n",
" <th>Unnamed: 0</th>\n",
" <th>text</th>\n",
" <th>embedding</th>\n",
" <th>similarities</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>70</th>\n",
" <td>70</td>\n",
" <td>70</td>\n",
" <td>One veterinarian described ‘break[ing] this cy...</td>\n",
" <td>[-0.021491553634405136, 0.005639533046633005, ...</td>\n",
" <td>0.837271</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Unnamed: 0.1 Unnamed: 0 \\\n",
"70 70 70 \n",
"\n",
" text \\\n",
"70 One veterinarian described ‘break[ing] this cy... \n",
"\n",
" embedding similarities \n",
"70 [-0.021491553634405136, 0.005639533046633005, ... 0.837271 "
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
|