File size: 4,767 Bytes
98de445 | 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 | {
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "200ebff3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"env: TOKENIZERS_PARALLELISM=false\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import warnings\n",
"warnings.filterwarnings('ignore')\n",
"\n",
"import json\n",
"from IPython.display import Markdown, display\n",
"\n",
"import numpy as np\n",
"import os\n",
"import pandas as pd\n",
"from random import choice, shuffle\n",
"import re\n",
"from textwrap import TextWrapper\n",
"from time import sleep\n",
"from tqdm import tqdm\n",
"\n",
"def dump(data): print(json.dumps(data, indent=4, sort_keys=True))\n",
"\n",
"def print_long(text, width=140, indent=0, initial_indent=0, ofp=None):\n",
" wrapper = TextWrapper(initial_indent=\" \" * initial_indent, subsequent_indent=\" \" * indent, width=width)\n",
" if ofp is None: print(\"\\n\".join(wrapper.wrap(text)))\n",
"\n",
"from dotenv import load_dotenv, find_dotenv\n",
"\n",
"%env TOKENIZERS_PARALLELISM=false\n",
"%load_ext autoreload\n",
"%autoreload 2\n",
"\n",
"load_dotenv(\"/home/ec2-user/.env\")\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "97c78754",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "845f3b7b8d5448b9869e73c1c9c1e840",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"VBox(children=(HTML(value='<center> <img\\nsrc=https://huggingface.co/front/assets/huggingface_logo-noborder.sv…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from huggingface_hub import notebook_login\n",
"notebook_login()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "1835f0aa",
"metadata": {},
"outputs": [],
"source": [
"from matplotlib import pyplot as plt\n",
"from sentence_transformers import SentenceTransformer\n",
"from umap import UMAP\n",
"\n",
"\n",
"#model = SentenceTransformer('all-MiniLM-L6-v2')\n",
"model = SentenceTransformer('sentence-transformers/all-mpnet-base-v2')\n",
"embeddings = model.encode(texts, show_progress_bar=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4f2b18a1",
"metadata": {},
"outputs": [],
"source": [
"\n",
"texts = TYPE_DF.sentence.values\n",
"\n",
"model = SentenceTransformer('all-MiniLM-L6-v2')\n",
"\n",
"BATCH_SIZE = 128\n",
"embeddings = model.encode(texts, batch_size=BATCH_SIZE, show_progress_bar=True)\n",
"\n",
"reducer = UMAP(metric='cosine')\n",
"embeddings_2d = reducer.fit_transform(embeddings)\n",
"\n",
"plt.rcParams['figure.dpi'] = 300\n",
"\n",
"plt.title(f'UMAP Projected Embeddings of {len(texts)} Has-Nationality relationship type evidence')\n",
"plt.scatter(embeddings_2d[:, 0], embeddings_2d[:, 1], s=0.1, alpha=0.2)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "b2a1bbd0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Data-Prep.ipynb\r\n"
]
}
],
"source": [
"!ls\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d93c5eec",
"metadata": {},
"outputs": [],
"source": [
"from huggingface_hub import HfApi\n",
"api = HfApi()\n",
"api.upload_file(\n",
" path_or_fileobj=\"./Data-Prep.ipynb\",\n",
" path_in_repo=\"Data-Prep.ipynb\",\n",
" repo_id=\"username/test-dataset\",\n",
" repo_type=\"dataset\",\n",
")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "python-3-9",
"language": "python",
"name": "python-3-9"
},
"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.19"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|