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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 2,
|
6 |
+
"id": "6826a9ff-3dbb-42f1-b5ea-b2cd0cbabf44",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [],
|
9 |
+
"source": [
|
10 |
+
"from transformers import (\n",
|
11 |
+
" AutoConfig,\n",
|
12 |
+
" AutoModelForSequenceClassification,\n",
|
13 |
+
" AutoTokenizer,\n",
|
14 |
+
" DataCollatorWithPadding,\n",
|
15 |
+
" EvalPrediction,\n",
|
16 |
+
" HfArgumentParser,\n",
|
17 |
+
" Trainer,\n",
|
18 |
+
" TrainingArguments,\n",
|
19 |
+
" default_data_collator,\n",
|
20 |
+
" set_seed,\n",
|
21 |
+
")\n",
|
22 |
+
"import json\n",
|
23 |
+
"from tqdm import tqdm"
|
24 |
+
]
|
25 |
+
},
|
26 |
+
{
|
27 |
+
"cell_type": "code",
|
28 |
+
"execution_count": 4,
|
29 |
+
"id": "477afce3-a672-4154-b5f4-b5cee162a6f7",
|
30 |
+
"metadata": {},
|
31 |
+
"outputs": [],
|
32 |
+
"source": [
|
33 |
+
"model = AutoModelForSequenceClassification.from_pretrained(\"sentiment-malay-deberta/checkpoint-88000\")"
|
34 |
+
]
|
35 |
+
},
|
36 |
+
{
|
37 |
+
"cell_type": "code",
|
38 |
+
"execution_count": 3,
|
39 |
+
"id": "d3d5a606-859e-4b26-a892-937296299ca8",
|
40 |
+
"metadata": {},
|
41 |
+
"outputs": [
|
42 |
+
{
|
43 |
+
"name": "stdout",
|
44 |
+
"output_type": "stream",
|
45 |
+
"text": [
|
46 |
+
"[{'label': 'negative', 'score': 0.6645314693450928}, {'label': 'negative', 'score': 0.9972461462020874}, {'label': 'positive', 'score': 0.9917457699775696}, {'label': 'positive', 'score': 0.9973276853561401}, {'label': 'negative', 'score': 0.9989587068557739}, {'label': 'negative', 'score': 0.9864065051078796}]\n"
|
47 |
+
]
|
48 |
+
}
|
49 |
+
],
|
50 |
+
"source": [
|
51 |
+
"from transformers import pipeline\n",
|
52 |
+
"sentiment_analysis = pipeline(\"sentiment-analysis\",model=\"sentiment-malay-deberta/checkpoint-88000\")\n",
|
53 |
+
"print(sentiment_analysis([\"teloq hang\",\"tak prihatin fikir kita ni apa\",\"bossku\",\"macam la dia tu hebat\",\"eh salah\",\"belagak pandai\"]))"
|
54 |
+
]
|
55 |
+
},
|
56 |
+
{
|
57 |
+
"cell_type": "code",
|
58 |
+
"execution_count": 11,
|
59 |
+
"id": "d8069cbf-8e5b-43a0-83b1-b00bf00e8526",
|
60 |
+
"metadata": {},
|
61 |
+
"outputs": [],
|
62 |
+
"source": [
|
63 |
+
"data = []\n",
|
64 |
+
"\n",
|
65 |
+
"with open('combined-data-sentiment.jsonl') as f:\n",
|
66 |
+
" for x in f:\n",
|
67 |
+
" result = json.loads(x)\n",
|
68 |
+
" if result['split'] != 'train':\n",
|
69 |
+
" data.append(result)"
|
70 |
+
]
|
71 |
+
},
|
72 |
+
{
|
73 |
+
"cell_type": "code",
|
74 |
+
"execution_count": 26,
|
75 |
+
"id": "bcdce6cf-34ab-4bdc-a317-48f1d9c187cf",
|
76 |
+
"metadata": {},
|
77 |
+
"outputs": [
|
78 |
+
{
|
79 |
+
"name": "stderr",
|
80 |
+
"output_type": "stream",
|
81 |
+
"text": [
|
82 |
+
"100%|██████████████████████████████████████████████████████████████████████████████████████████| 10857/10857 [15:37<00:00, 11.58it/s]\n"
|
83 |
+
]
|
84 |
+
}
|
85 |
+
],
|
86 |
+
"source": [
|
87 |
+
"batch_size = 3\n",
|
88 |
+
"predicted_labels = []\n",
|
89 |
+
"for i in tqdm(range(0, len(data), batch_size)):\n",
|
90 |
+
" batch = data[i:i + batch_size]\n",
|
91 |
+
" batch_predictions = sentiment_analysis([x['text'] for x in batch])\n",
|
92 |
+
"\n",
|
93 |
+
" for predictions in batch_predictions:\n",
|
94 |
+
" max_score_label = predictions['label']\n",
|
95 |
+
"\n",
|
96 |
+
" predicted_labels.append(max_score_label)"
|
97 |
+
]
|
98 |
+
},
|
99 |
+
{
|
100 |
+
"cell_type": "code",
|
101 |
+
"execution_count": 27,
|
102 |
+
"id": "0ee8f722-4e53-4245-b27a-59224be2edf1",
|
103 |
+
"metadata": {},
|
104 |
+
"outputs": [
|
105 |
+
{
|
106 |
+
"name": "stderr",
|
107 |
+
"output_type": "stream",
|
108 |
+
"text": [
|
109 |
+
"/home/aisyah/.local/lib/python3.8/site-packages/sklearn/metrics/_classification.py:1471: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.\n",
|
110 |
+
" _warn_prf(average, modifier, msg_start, len(result))\n"
|
111 |
+
]
|
112 |
+
},
|
113 |
+
{
|
114 |
+
"name": "stdout",
|
115 |
+
"output_type": "stream",
|
116 |
+
"text": [
|
117 |
+
" precision recall f1-score support\n",
|
118 |
+
"\n",
|
119 |
+
" negative 0.94533 0.97434 0.95962 20108\n",
|
120 |
+
" neutral 0.00000 0.00000 0.00000 699\n",
|
121 |
+
" positive 0.92857 0.93505 0.93180 11762\n",
|
122 |
+
"\n",
|
123 |
+
" accuracy 0.93924 32569\n",
|
124 |
+
" macro avg 0.62463 0.63646 0.63047 32569\n",
|
125 |
+
"weighted avg 0.91899 0.93924 0.92897 32569\n",
|
126 |
+
"\n"
|
127 |
+
]
|
128 |
+
},
|
129 |
+
{
|
130 |
+
"name": "stderr",
|
131 |
+
"output_type": "stream",
|
132 |
+
"text": [
|
133 |
+
"/home/aisyah/.local/lib/python3.8/site-packages/sklearn/metrics/_classification.py:1471: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.\n",
|
134 |
+
" _warn_prf(average, modifier, msg_start, len(result))\n",
|
135 |
+
"/home/aisyah/.local/lib/python3.8/site-packages/sklearn/metrics/_classification.py:1471: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.\n",
|
136 |
+
" _warn_prf(average, modifier, msg_start, len(result))\n"
|
137 |
+
]
|
138 |
+
}
|
139 |
+
],
|
140 |
+
"source": [
|
141 |
+
"from sklearn.metrics import classification_report\n",
|
142 |
+
"\n",
|
143 |
+
"print(classification_report([x['sentiment'] for x in data],predicted_labels, digits = 5))"
|
144 |
+
]
|
145 |
+
},
|
146 |
+
{
|
147 |
+
"cell_type": "code",
|
148 |
+
"execution_count": 5,
|
149 |
+
"id": "59487536-1153-4ba7-80fc-e46a8080bfd8",
|
150 |
+
"metadata": {},
|
151 |
+
"outputs": [
|
152 |
+
{
|
153 |
+
"name": "stderr",
|
154 |
+
"output_type": "stream",
|
155 |
+
"text": [
|
156 |
+
"/home/aisyah/.local/lib/python3.8/site-packages/transformers/utils/hub.py:671: UserWarning: The `organization` argument is deprecated and will be removed in v5 of Transformers. Set your organization directly in the `repo_id` passed instead (`repo_id={organization}/{model_id}`).\n",
|
157 |
+
" warnings.warn(\n"
|
158 |
+
]
|
159 |
+
},
|
160 |
+
{
|
161 |
+
"data": {
|
162 |
+
"application/vnd.jupyter.widget-view+json": {
|
163 |
+
"model_id": "5d8b05ef5fd444df96cb959d9829509b",
|
164 |
+
"version_major": 2,
|
165 |
+
"version_minor": 0
|
166 |
+
},
|
167 |
+
"text/plain": [
|
168 |
+
"model.safetensors: 0%| | 0.00/283M [00:00<?, ?B/s]"
|
169 |
+
]
|
170 |
+
},
|
171 |
+
"metadata": {},
|
172 |
+
"output_type": "display_data"
|
173 |
+
},
|
174 |
+
{
|
175 |
+
"data": {
|
176 |
+
"text/plain": [
|
177 |
+
"CommitInfo(commit_url='https://huggingface.co/malaysia-ai/deberta-v3-xsmall-malay-sentiment/commit/544700f30bbb273144933f93417cd87cad9b4c94', commit_message='Upload DebertaV2ForSequenceClassification', commit_description='', oid='544700f30bbb273144933f93417cd87cad9b4c94', pr_url=None, pr_revision=None, pr_num=None)"
|
178 |
+
]
|
179 |
+
},
|
180 |
+
"execution_count": 5,
|
181 |
+
"metadata": {},
|
182 |
+
"output_type": "execute_result"
|
183 |
+
}
|
184 |
+
],
|
185 |
+
"source": [
|
186 |
+
"model.push_to_hub('deberta-v3-xsmall-malay-sentiment', organization='malaysia-ai', safe_serialization=True)"
|
187 |
+
]
|
188 |
+
},
|
189 |
+
{
|
190 |
+
"cell_type": "code",
|
191 |
+
"execution_count": 7,
|
192 |
+
"id": "81de36e9-5e7e-4583-9c30-d835f7985858",
|
193 |
+
"metadata": {},
|
194 |
+
"outputs": [],
|
195 |
+
"source": [
|
196 |
+
"tokenizer = AutoTokenizer.from_pretrained(\"sentiment-malay-deberta/checkpoint-88000\")"
|
197 |
+
]
|
198 |
+
},
|
199 |
+
{
|
200 |
+
"cell_type": "code",
|
201 |
+
"execution_count": 8,
|
202 |
+
"id": "2ed2f170-8a46-47dd-9064-6cc0f2322230",
|
203 |
+
"metadata": {},
|
204 |
+
"outputs": [
|
205 |
+
{
|
206 |
+
"data": {
|
207 |
+
"application/vnd.jupyter.widget-view+json": {
|
208 |
+
"model_id": "0ff3af039e6d43ce8e3b3d164bc0991d",
|
209 |
+
"version_major": 2,
|
210 |
+
"version_minor": 0
|
211 |
+
},
|
212 |
+
"text/plain": [
|
213 |
+
"spm.model: 0%| | 0.00/2.46M [00:00<?, ?B/s]"
|
214 |
+
]
|
215 |
+
},
|
216 |
+
"metadata": {},
|
217 |
+
"output_type": "display_data"
|
218 |
+
},
|
219 |
+
{
|
220 |
+
"data": {
|
221 |
+
"text/plain": [
|
222 |
+
"CommitInfo(commit_url='https://huggingface.co/malaysia-ai/deberta-v3-xsmall-malay-sentiment/commit/3ecb0599c10fb975720251091ff9b3ef2eced5b7', commit_message='Upload tokenizer', commit_description='', oid='3ecb0599c10fb975720251091ff9b3ef2eced5b7', pr_url=None, pr_revision=None, pr_num=None)"
|
223 |
+
]
|
224 |
+
},
|
225 |
+
"execution_count": 8,
|
226 |
+
"metadata": {},
|
227 |
+
"output_type": "execute_result"
|
228 |
+
}
|
229 |
+
],
|
230 |
+
"source": [
|
231 |
+
"tokenizer.push_to_hub('deberta-v3-xsmall-malay-sentiment', organization='malaysia-ai', safe_serialization=True)"
|
232 |
+
]
|
233 |
+
},
|
234 |
+
{
|
235 |
+
"cell_type": "code",
|
236 |
+
"execution_count": 9,
|
237 |
+
"id": "a5c3a962-d1d2-40cc-b5b8-7c8d016dcc7d",
|
238 |
+
"metadata": {},
|
239 |
+
"outputs": [
|
240 |
+
{
|
241 |
+
"data": {
|
242 |
+
"application/vnd.jupyter.widget-view+json": {
|
243 |
+
"model_id": "a9fec7de33ce48a2be756da4cfd46a97",
|
244 |
+
"version_major": 2,
|
245 |
+
"version_minor": 0
|
246 |
+
},
|
247 |
+
"text/plain": [
|
248 |
+
"combined-data-sentiment.jsonl: 0%| | 0.00/652M [00:00<?, ?B/s]"
|
249 |
+
]
|
250 |
+
},
|
251 |
+
"metadata": {},
|
252 |
+
"output_type": "display_data"
|
253 |
+
},
|
254 |
+
{
|
255 |
+
"data": {
|
256 |
+
"text/plain": [
|
257 |
+
"CommitInfo(commit_url='https://huggingface.co/datasets/aisyahhrazak/combined-malay-sentiment/commit/9ab06163578a4743a657f66d3b39a38e014d8c4d', commit_message='Upload README.md with huggingface_hub', commit_description='', oid='9ab06163578a4743a657f66d3b39a38e014d8c4d', pr_url=None, pr_revision=None, pr_num=None)"
|
258 |
+
]
|
259 |
+
},
|
260 |
+
"execution_count": 9,
|
261 |
+
"metadata": {},
|
262 |
+
"output_type": "execute_result"
|
263 |
+
}
|
264 |
+
],
|
265 |
+
"source": [
|
266 |
+
"from huggingface_hub import HfApi\n",
|
267 |
+
"api = HfApi()\n",
|
268 |
+
"api.upload_file(\n",
|
269 |
+
" path_or_fileobj=\"combined-data-sentiment.jsonl\",\n",
|
270 |
+
" path_in_repo=\"README.md\",\n",
|
271 |
+
" repo_id=\"aisyahhrazak/combined-malay-sentiment\",\n",
|
272 |
+
" repo_type=\"dataset\",\n",
|
273 |
+
")"
|
274 |
+
]
|
275 |
+
}
|
276 |
+
],
|
277 |
+
"metadata": {
|
278 |
+
"kernelspec": {
|
279 |
+
"display_name": "venv",
|
280 |
+
"language": "python",
|
281 |
+
"name": "venv"
|
282 |
+
},
|
283 |
+
"language_info": {
|
284 |
+
"codemirror_mode": {
|
285 |
+
"name": "ipython",
|
286 |
+
"version": 3
|
287 |
+
},
|
288 |
+
"file_extension": ".py",
|
289 |
+
"mimetype": "text/x-python",
|
290 |
+
"name": "python",
|
291 |
+
"nbconvert_exporter": "python",
|
292 |
+
"pygments_lexer": "ipython3",
|
293 |
+
"version": "3.8.10"
|
294 |
+
}
|
295 |
+
},
|
296 |
+
"nbformat": 4,
|
297 |
+
"nbformat_minor": 5
|
298 |
+
}
|