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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"id": "200ebff3",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [
|
| 9 |
+
{
|
| 10 |
+
"name": "stdout",
|
| 11 |
+
"output_type": "stream",
|
| 12 |
+
"text": [
|
| 13 |
+
"env: TOKENIZERS_PARALLELISM=false\n"
|
| 14 |
+
]
|
| 15 |
+
},
|
| 16 |
+
{
|
| 17 |
+
"data": {
|
| 18 |
+
"text/plain": [
|
| 19 |
+
"True"
|
| 20 |
+
]
|
| 21 |
+
},
|
| 22 |
+
"execution_count": 1,
|
| 23 |
+
"metadata": {},
|
| 24 |
+
"output_type": "execute_result"
|
| 25 |
+
}
|
| 26 |
+
],
|
| 27 |
+
"source": [
|
| 28 |
+
"import warnings\n",
|
| 29 |
+
"warnings.filterwarnings('ignore')\n",
|
| 30 |
+
"\n",
|
| 31 |
+
"import json\n",
|
| 32 |
+
"from IPython.display import Markdown, display\n",
|
| 33 |
+
"\n",
|
| 34 |
+
"import numpy as np\n",
|
| 35 |
+
"import os\n",
|
| 36 |
+
"import pandas as pd\n",
|
| 37 |
+
"from random import choice, shuffle\n",
|
| 38 |
+
"import re\n",
|
| 39 |
+
"from textwrap import TextWrapper\n",
|
| 40 |
+
"from time import sleep\n",
|
| 41 |
+
"from tqdm import tqdm\n",
|
| 42 |
+
"\n",
|
| 43 |
+
"def dump(data): print(json.dumps(data, indent=4, sort_keys=True))\n",
|
| 44 |
+
"\n",
|
| 45 |
+
"def print_long(text, width=140, indent=0, initial_indent=0, ofp=None):\n",
|
| 46 |
+
" wrapper = TextWrapper(initial_indent=\" \" * initial_indent, subsequent_indent=\" \" * indent, width=width)\n",
|
| 47 |
+
" if ofp is None: print(\"\\n\".join(wrapper.wrap(text)))\n",
|
| 48 |
+
"\n",
|
| 49 |
+
"from dotenv import load_dotenv, find_dotenv\n",
|
| 50 |
+
"\n",
|
| 51 |
+
"%env TOKENIZERS_PARALLELISM=false\n",
|
| 52 |
+
"%load_ext autoreload\n",
|
| 53 |
+
"%autoreload 2\n",
|
| 54 |
+
"\n",
|
| 55 |
+
"load_dotenv(\"/home/ec2-user/.env\")\n"
|
| 56 |
+
]
|
| 57 |
+
},
|
| 58 |
+
{
|
| 59 |
+
"cell_type": "code",
|
| 60 |
+
"execution_count": 3,
|
| 61 |
+
"id": "97c78754",
|
| 62 |
+
"metadata": {},
|
| 63 |
+
"outputs": [
|
| 64 |
+
{
|
| 65 |
+
"data": {
|
| 66 |
+
"application/vnd.jupyter.widget-view+json": {
|
| 67 |
+
"model_id": "845f3b7b8d5448b9869e73c1c9c1e840",
|
| 68 |
+
"version_major": 2,
|
| 69 |
+
"version_minor": 0
|
| 70 |
+
},
|
| 71 |
+
"text/plain": [
|
| 72 |
+
"VBox(children=(HTML(value='<center> <img\\nsrc=https://huggingface.co/front/assets/huggingface_logo-noborder.sv…"
|
| 73 |
+
]
|
| 74 |
+
},
|
| 75 |
+
"metadata": {},
|
| 76 |
+
"output_type": "display_data"
|
| 77 |
+
}
|
| 78 |
+
],
|
| 79 |
+
"source": [
|
| 80 |
+
"from huggingface_hub import notebook_login\n",
|
| 81 |
+
"notebook_login()"
|
| 82 |
+
]
|
| 83 |
+
},
|
| 84 |
+
{
|
| 85 |
+
"cell_type": "code",
|
| 86 |
+
"execution_count": 5,
|
| 87 |
+
"id": "1835f0aa",
|
| 88 |
+
"metadata": {},
|
| 89 |
+
"outputs": [],
|
| 90 |
+
"source": [
|
| 91 |
+
"from matplotlib import pyplot as plt\n",
|
| 92 |
+
"from sentence_transformers import SentenceTransformer\n",
|
| 93 |
+
"from umap import UMAP\n",
|
| 94 |
+
"\n",
|
| 95 |
+
"\n",
|
| 96 |
+
"#model = SentenceTransformer('all-MiniLM-L6-v2')\n",
|
| 97 |
+
"model = SentenceTransformer('sentence-transformers/all-mpnet-base-v2')\n",
|
| 98 |
+
"embeddings = model.encode(texts, show_progress_bar=True)"
|
| 99 |
+
]
|
| 100 |
+
},
|
| 101 |
+
{
|
| 102 |
+
"cell_type": "code",
|
| 103 |
+
"execution_count": null,
|
| 104 |
+
"id": "4f2b18a1",
|
| 105 |
+
"metadata": {},
|
| 106 |
+
"outputs": [],
|
| 107 |
+
"source": [
|
| 108 |
+
"\n",
|
| 109 |
+
"texts = TYPE_DF.sentence.values\n",
|
| 110 |
+
"\n",
|
| 111 |
+
"model = SentenceTransformer('all-MiniLM-L6-v2')\n",
|
| 112 |
+
"\n",
|
| 113 |
+
"BATCH_SIZE = 128\n",
|
| 114 |
+
"embeddings = model.encode(texts, batch_size=BATCH_SIZE, show_progress_bar=True)\n",
|
| 115 |
+
"\n",
|
| 116 |
+
"reducer = UMAP(metric='cosine')\n",
|
| 117 |
+
"embeddings_2d = reducer.fit_transform(embeddings)\n",
|
| 118 |
+
"\n",
|
| 119 |
+
"plt.rcParams['figure.dpi'] = 300\n",
|
| 120 |
+
"\n",
|
| 121 |
+
"plt.title(f'UMAP Projected Embeddings of {len(texts)} Has-Nationality relationship type evidence')\n",
|
| 122 |
+
"plt.scatter(embeddings_2d[:, 0], embeddings_2d[:, 1], s=0.1, alpha=0.2)\n",
|
| 123 |
+
"plt.show()"
|
| 124 |
+
]
|
| 125 |
+
},
|
| 126 |
+
{
|
| 127 |
+
"cell_type": "code",
|
| 128 |
+
"execution_count": 6,
|
| 129 |
+
"id": "b2a1bbd0",
|
| 130 |
+
"metadata": {},
|
| 131 |
+
"outputs": [
|
| 132 |
+
{
|
| 133 |
+
"name": "stdout",
|
| 134 |
+
"output_type": "stream",
|
| 135 |
+
"text": [
|
| 136 |
+
"Data-Prep.ipynb\r\n"
|
| 137 |
+
]
|
| 138 |
+
}
|
| 139 |
+
],
|
| 140 |
+
"source": [
|
| 141 |
+
"!ls\n"
|
| 142 |
+
]
|
| 143 |
+
},
|
| 144 |
+
{
|
| 145 |
+
"cell_type": "code",
|
| 146 |
+
"execution_count": null,
|
| 147 |
+
"id": "d93c5eec",
|
| 148 |
+
"metadata": {},
|
| 149 |
+
"outputs": [],
|
| 150 |
+
"source": [
|
| 151 |
+
"from huggingface_hub import HfApi\n",
|
| 152 |
+
"api = HfApi()\n",
|
| 153 |
+
"api.upload_file(\n",
|
| 154 |
+
" path_or_fileobj=\"./Data-Prep.ipynb\",\n",
|
| 155 |
+
" path_in_repo=\"Data-Prep.ipynb\",\n",
|
| 156 |
+
" repo_id=\"username/test-dataset\",\n",
|
| 157 |
+
" repo_type=\"dataset\",\n",
|
| 158 |
+
")"
|
| 159 |
+
]
|
| 160 |
+
}
|
| 161 |
+
],
|
| 162 |
+
"metadata": {
|
| 163 |
+
"kernelspec": {
|
| 164 |
+
"display_name": "python-3-9",
|
| 165 |
+
"language": "python",
|
| 166 |
+
"name": "python-3-9"
|
| 167 |
+
},
|
| 168 |
+
"language_info": {
|
| 169 |
+
"codemirror_mode": {
|
| 170 |
+
"name": "ipython",
|
| 171 |
+
"version": 3
|
| 172 |
+
},
|
| 173 |
+
"file_extension": ".py",
|
| 174 |
+
"mimetype": "text/x-python",
|
| 175 |
+
"name": "python",
|
| 176 |
+
"nbconvert_exporter": "python",
|
| 177 |
+
"pygments_lexer": "ipython3",
|
| 178 |
+
"version": "3.9.19"
|
| 179 |
+
},
|
| 180 |
+
"toc": {
|
| 181 |
+
"base_numbering": 1,
|
| 182 |
+
"nav_menu": {},
|
| 183 |
+
"number_sections": true,
|
| 184 |
+
"sideBar": true,
|
| 185 |
+
"skip_h1_title": false,
|
| 186 |
+
"title_cell": "Table of Contents",
|
| 187 |
+
"title_sidebar": "Contents",
|
| 188 |
+
"toc_cell": false,
|
| 189 |
+
"toc_position": {},
|
| 190 |
+
"toc_section_display": true,
|
| 191 |
+
"toc_window_display": false
|
| 192 |
+
}
|
| 193 |
+
},
|
| 194 |
+
"nbformat": 4,
|
| 195 |
+
"nbformat_minor": 5
|
| 196 |
+
}
|