File size: 9,879 Bytes
0c53782
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6826a9ff-3dbb-42f1-b5ea-b2cd0cbabf44",
   "metadata": {},
   "outputs": [],
   "source": [
    "from transformers import (\n",
    "    AutoConfig,\n",
    "    AutoModelForSequenceClassification,\n",
    "    AutoTokenizer,\n",
    "    DataCollatorWithPadding,\n",
    "    EvalPrediction,\n",
    "    HfArgumentParser,\n",
    "    Trainer,\n",
    "    TrainingArguments,\n",
    "    default_data_collator,\n",
    "    set_seed,\n",
    ")\n",
    "import json\n",
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "477afce3-a672-4154-b5f4-b5cee162a6f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "model = AutoModelForSequenceClassification.from_pretrained(\"sentiment-malay-deberta/checkpoint-88000\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d3d5a606-859e-4b26-a892-937296299ca8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'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"
     ]
    }
   ],
   "source": [
    "from transformers import pipeline\n",
    "sentiment_analysis = pipeline(\"sentiment-analysis\",model=\"sentiment-malay-deberta/checkpoint-88000\")\n",
    "print(sentiment_analysis([\"teloq hang\",\"tak prihatin fikir kita ni apa\",\"bossku\",\"macam la dia tu hebat\",\"eh salah\",\"belagak pandai\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d8069cbf-8e5b-43a0-83b1-b00bf00e8526",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = []\n",
    "\n",
    "with open('combined-data-sentiment.jsonl') as f:\n",
    "    for x in f:\n",
    "        result = json.loads(x)\n",
    "        if result['split'] != 'train':\n",
    "            data.append(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "bcdce6cf-34ab-4bdc-a317-48f1d9c187cf",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 10857/10857 [15:37<00:00, 11.58it/s]\n"
     ]
    }
   ],
   "source": [
    "batch_size = 3\n",
    "predicted_labels = []\n",
    "for i in tqdm(range(0, len(data), batch_size)):\n",
    "    batch = data[i:i + batch_size]\n",
    "    batch_predictions = sentiment_analysis([x['text'] for x in batch])\n",
    "\n",
    "    for predictions in batch_predictions:\n",
    "        max_score_label = predictions['label']\n",
    "\n",
    "        predicted_labels.append(max_score_label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "0ee8f722-4e53-4245-b27a-59224be2edf1",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/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",
      "  _warn_prf(average, modifier, msg_start, len(result))\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "    negative    0.94533   0.97434   0.95962     20108\n",
      "     neutral    0.00000   0.00000   0.00000       699\n",
      "    positive    0.92857   0.93505   0.93180     11762\n",
      "\n",
      "    accuracy                        0.93924     32569\n",
      "   macro avg    0.62463   0.63646   0.63047     32569\n",
      "weighted avg    0.91899   0.93924   0.92897     32569\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/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",
      "  _warn_prf(average, modifier, msg_start, len(result))\n",
      "/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",
      "  _warn_prf(average, modifier, msg_start, len(result))\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import classification_report\n",
    "\n",
    "print(classification_report([x['sentiment'] for x in data],predicted_labels, digits = 5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "59487536-1153-4ba7-80fc-e46a8080bfd8",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/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",
      "  warnings.warn(\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5d8b05ef5fd444df96cb959d9829509b",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "model.safetensors:   0%|          | 0.00/283M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "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)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.push_to_hub('deberta-v3-xsmall-malay-sentiment', organization='malaysia-ai', safe_serialization=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "81de36e9-5e7e-4583-9c30-d835f7985858",
   "metadata": {},
   "outputs": [],
   "source": [
    "tokenizer = AutoTokenizer.from_pretrained(\"sentiment-malay-deberta/checkpoint-88000\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2ed2f170-8a46-47dd-9064-6cc0f2322230",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0ff3af039e6d43ce8e3b3d164bc0991d",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "spm.model:   0%|          | 0.00/2.46M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "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)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tokenizer.push_to_hub('deberta-v3-xsmall-malay-sentiment', organization='malaysia-ai', safe_serialization=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a5c3a962-d1d2-40cc-b5b8-7c8d016dcc7d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a9fec7de33ce48a2be756da4cfd46a97",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "combined-data-sentiment.jsonl:   0%|          | 0.00/652M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "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)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from huggingface_hub import HfApi\n",
    "api = HfApi()\n",
    "api.upload_file(\n",
    "    path_or_fileobj=\"combined-data-sentiment.jsonl\",\n",
    "    path_in_repo=\"README.md\",\n",
    "    repo_id=\"aisyahhrazak/combined-malay-sentiment\",\n",
    "    repo_type=\"dataset\",\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv",
   "language": "python",
   "name": "venv"
  },
  "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.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}