utils: add demo notebook to show how to load dataset with Flair library
Browse files- FlairDatasetLoader.ipynb +169 -0
FlairDatasetLoader.ipynb
ADDED
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"id": "d78b2b50-9c54-441e-a835-b28cb0f8e096",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [
|
9 |
+
{
|
10 |
+
"name": "stderr",
|
11 |
+
"output_type": "stream",
|
12 |
+
"text": [
|
13 |
+
"2024-11-29 23:21:49.836344: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.\n",
|
14 |
+
"2024-11-29 23:21:49.836729: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.\n",
|
15 |
+
"2024-11-29 23:21:49.838569: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.\n",
|
16 |
+
"2024-11-29 23:21:49.843832: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:477] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered\n",
|
17 |
+
"WARNING: All log messages before absl::InitializeLog() is called are written to STDERR\n",
|
18 |
+
"E0000 00:00:1732918909.852690 10769 cuda_dnn.cc:8310] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered\n",
|
19 |
+
"E0000 00:00:1732918909.855335 10769 cuda_blas.cc:1418] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n",
|
20 |
+
"2024-11-29 23:21:49.864609: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n",
|
21 |
+
"To enable the following instructions: AVX2 AVX_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n"
|
22 |
+
]
|
23 |
+
}
|
24 |
+
],
|
25 |
+
"source": [
|
26 |
+
"import flair\n",
|
27 |
+
"\n",
|
28 |
+
"from flair.datasets import ClassificationCorpus\n",
|
29 |
+
"\n",
|
30 |
+
"from huggingface_hub import hf_hub_download\n",
|
31 |
+
"\n",
|
32 |
+
"from pathlib import Path\n",
|
33 |
+
"from typing import Optional, Union"
|
34 |
+
]
|
35 |
+
},
|
36 |
+
{
|
37 |
+
"cell_type": "code",
|
38 |
+
"execution_count": 2,
|
39 |
+
"id": "404f1a80-5dcd-44cf-a37c-731553bebafc",
|
40 |
+
"metadata": {},
|
41 |
+
"outputs": [],
|
42 |
+
"source": [
|
43 |
+
"class SENTI_ANNO(ClassificationCorpus):\n",
|
44 |
+
" def __init__(\n",
|
45 |
+
" self,\n",
|
46 |
+
" base_path: Optional[Union[str, Path]] = None,\n",
|
47 |
+
" in_memory: bool = True,\n",
|
48 |
+
" **corpusargs,\n",
|
49 |
+
" ) -> None:\n",
|
50 |
+
" base_path = flair.cache_root / \"datasets\" if not base_path else Path(base_path)\n",
|
51 |
+
" dataset_name = self.__class__.__name__.lower()\n",
|
52 |
+
" data_folder = base_path / dataset_name\n",
|
53 |
+
" data_path = flair.cache_root / \"datasets\" / dataset_name\n",
|
54 |
+
"\n",
|
55 |
+
" for split in [\"train\", \"dev\", \"test\"]:\n",
|
56 |
+
" hf_hub_download(repo_id=\"stefan-it/senti-anno\", repo_type=\"dataset\",\n",
|
57 |
+
" filename=f\"{split}.txt\", token=True, local_dir=data_folder)\n",
|
58 |
+
"\n",
|
59 |
+
" super().__init__(\n",
|
60 |
+
" data_path,\n",
|
61 |
+
" **corpusargs,\n",
|
62 |
+
" )"
|
63 |
+
]
|
64 |
+
},
|
65 |
+
{
|
66 |
+
"cell_type": "code",
|
67 |
+
"execution_count": 3,
|
68 |
+
"id": "146baf74-5208-4a46-bb1f-0b652bae92c0",
|
69 |
+
"metadata": {},
|
70 |
+
"outputs": [
|
71 |
+
{
|
72 |
+
"data": {
|
73 |
+
"application/vnd.jupyter.widget-view+json": {
|
74 |
+
"model_id": "cfb52f4fd9bd47d496df3c39a460aac3",
|
75 |
+
"version_major": 2,
|
76 |
+
"version_minor": 0
|
77 |
+
},
|
78 |
+
"text/plain": [
|
79 |
+
"train.txt: 0%| | 0.00/210k [00:00<?, ?B/s]"
|
80 |
+
]
|
81 |
+
},
|
82 |
+
"metadata": {},
|
83 |
+
"output_type": "display_data"
|
84 |
+
},
|
85 |
+
{
|
86 |
+
"data": {
|
87 |
+
"application/vnd.jupyter.widget-view+json": {
|
88 |
+
"model_id": "ee09bd462f6f4837bd3d21ff21faa3c8",
|
89 |
+
"version_major": 2,
|
90 |
+
"version_minor": 0
|
91 |
+
},
|
92 |
+
"text/plain": [
|
93 |
+
"dev.txt: 0%| | 0.00/26.2k [00:00<?, ?B/s]"
|
94 |
+
]
|
95 |
+
},
|
96 |
+
"metadata": {},
|
97 |
+
"output_type": "display_data"
|
98 |
+
},
|
99 |
+
{
|
100 |
+
"data": {
|
101 |
+
"application/vnd.jupyter.widget-view+json": {
|
102 |
+
"model_id": "db9260082d304746875ef8fa6b7c6869",
|
103 |
+
"version_major": 2,
|
104 |
+
"version_minor": 0
|
105 |
+
},
|
106 |
+
"text/plain": [
|
107 |
+
"test.txt: 0%| | 0.00/26.3k [00:00<?, ?B/s]"
|
108 |
+
]
|
109 |
+
},
|
110 |
+
"metadata": {},
|
111 |
+
"output_type": "display_data"
|
112 |
+
},
|
113 |
+
{
|
114 |
+
"name": "stdout",
|
115 |
+
"output_type": "stream",
|
116 |
+
"text": [
|
117 |
+
"2024-11-29 23:24:50,712 Reading data from /home/stefan/.flair/datasets/senti_anno\n",
|
118 |
+
"2024-11-29 23:24:50,713 Train: /home/stefan/.flair/datasets/senti_anno/train.txt\n",
|
119 |
+
"2024-11-29 23:24:50,713 Dev: /home/stefan/.flair/datasets/senti_anno/dev.txt\n",
|
120 |
+
"2024-11-29 23:24:50,714 Test: /home/stefan/.flair/datasets/senti_anno/test.txt\n",
|
121 |
+
"2024-11-29 23:24:50,725 Initialized corpus /home/stefan/.flair/datasets/senti_anno (label type name is 'class')\n"
|
122 |
+
]
|
123 |
+
}
|
124 |
+
],
|
125 |
+
"source": [
|
126 |
+
"corpus = SENTI_ANNO()"
|
127 |
+
]
|
128 |
+
},
|
129 |
+
{
|
130 |
+
"cell_type": "code",
|
131 |
+
"execution_count": 4,
|
132 |
+
"id": "0b4badbf-6e62-4a72-af41-cce3811007ae",
|
133 |
+
"metadata": {},
|
134 |
+
"outputs": [
|
135 |
+
{
|
136 |
+
"name": "stdout",
|
137 |
+
"output_type": "stream",
|
138 |
+
"text": [
|
139 |
+
"Corpus: 741 train + 93 dev + 95 test sentences\n"
|
140 |
+
]
|
141 |
+
}
|
142 |
+
],
|
143 |
+
"source": [
|
144 |
+
"print(str(corpus))"
|
145 |
+
]
|
146 |
+
}
|
147 |
+
],
|
148 |
+
"metadata": {
|
149 |
+
"kernelspec": {
|
150 |
+
"display_name": "Python 3 (ipykernel)",
|
151 |
+
"language": "python",
|
152 |
+
"name": "python3"
|
153 |
+
},
|
154 |
+
"language_info": {
|
155 |
+
"codemirror_mode": {
|
156 |
+
"name": "ipython",
|
157 |
+
"version": 3
|
158 |
+
},
|
159 |
+
"file_extension": ".py",
|
160 |
+
"mimetype": "text/x-python",
|
161 |
+
"name": "python",
|
162 |
+
"nbconvert_exporter": "python",
|
163 |
+
"pygments_lexer": "ipython3",
|
164 |
+
"version": "3.12.3"
|
165 |
+
}
|
166 |
+
},
|
167 |
+
"nbformat": 4,
|
168 |
+
"nbformat_minor": 5
|
169 |
+
}
|