File size: 7,205 Bytes
cef43b2
 
 
 
604701b
 
cef43b2
 
 
604701b
 
cef43b2
 
 
 
604701b
 
cef43b2
 
 
604701b
 
 
 
 
 
 
 
 
 
 
 
cef43b2
 
604701b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cef43b2
604701b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cef43b2
 
 
 
604701b
cef43b2
604701b
cef43b2
604701b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cef43b2
 
 
604701b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cef43b2
 
 
 
 
604701b
cef43b2
 
 
 
 
 
 
604701b
cef43b2
604701b
cef43b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "aa1a8952",
   "metadata": {},
   "outputs": [],
   "source": [
    "#import libraries\n",
    "from transformers import pipeline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5493cee5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "9b94e18c676540d1ad8312149bdbcc7c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "model.safetensors:   0%|          | 0.00/431M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "586b43b1b9e2484c9b8636f9b41ab5e2",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "tokenizer_config.json:   0%|          | 0.00/1.19k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d6c722044e074fd58b9cf1c67fa48551",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "vocab.txt:   0%|          | 0.00/213k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "95002c472f794e3b9b96dcfba697395e",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "tokenizer.json:   0%|          | 0.00/669k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b71e5e8241de454c9bd5f7066b3f5413",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "special_tokens_map.json:   0%|          | 0.00/125 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#reference appropriate Hugging Face model\n",
    "model_name = 'koakande/bert-finetuned-ner'\n",
    "\n",
    "# Load token classification pipeline modelfrom Hugging Face\n",
    "model = pipeline(\"token-classification\", model=model_name, aggregation_strategy=\"simple\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f59488e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'koakande/bert-finetuned-ner'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e6b97a0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'entity_group': 'PER',\n",
       "  'score': 0.99741244,\n",
       "  'word': 'Kabeer',\n",
       "  'start': 12,\n",
       "  'end': 18},\n",
       " {'entity_group': 'ORG',\n",
       "  'score': 0.9985826,\n",
       "  'word': 'OVO',\n",
       "  'start': 61,\n",
       "  'end': 64},\n",
       " {'entity_group': 'LOC',\n",
       "  'score': 0.99884343,\n",
       "  'word': 'UK',\n",
       "  'start': 72,\n",
       "  'end': 74}]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "msg = \"Hello, I am Kabeer. I work as a machine learning engineer at OVO in the UK\"\n",
    "model(msg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7c54c0ca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"Hello, I am <span style='border: 2px solid green;'>Kabeer</span>. I work as a machine learning engineer at <span style='border: 2px solid green;'>OVO</span> in the <span style='border: 2px solid green;'>UK</span>\""
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# write a prediction method for the model\n",
    "def predict_entities(text):\n",
    "    # Use the loaded model to identify entities in the text\n",
    "    entities = model(text)\n",
    "    # Highlight identified entities in the input text\n",
    "    highlighted_text = text\n",
    "    for entity in entities:\n",
    "        entity_text = text[entity['start']:entity['end']]\n",
    "        replacement = f\"<span style='border: 2px solid green;'>{entity_text}</span>\"\n",
    "        highlighted_text = highlighted_text.replace(entity_text, replacement)\n",
    "    return highlighted_text\n",
    "\n",
    "predict_entities(msg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4d784554",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7860\n",
      "\n",
      "To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# gradio interface\n",
    "import gradio as gr\n",
    "\n",
    "title = \"Named Entity Recognizer\"\n",
    "\n",
    "description = \"\"\"\n",
    "This model has been trained to identify entities in a given text. It returns the input text with the entities highlighted in green. Give it a try!\n",
    "\"\"\"\n",
    "\n",
    "article = \"The model is trained using bert-finetuned-ner.\"\n",
    "\n",
    "iface  = gr.Interface(\n",
    "    fn=predict_entities,\n",
    "    inputs=gr.Textbox(lines=5, placeholder=\"Enter text...\"),\n",
    "    outputs=gr.HTML(),\n",
    "    title=title,\n",
    "    description=description,\n",
    "    article=article,\n",
    "    examples=[[\"Hello, I am Kabeer. I work as a machine learning engineer at OVO in the UK\"], [\"This is Maryam who is a Leicester based NHS Doctor\"]],\n",
    ")\n",
    "\n",
    "iface.launch()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4f930b57",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "named_entity__kernel",
   "language": "python",
   "name": "named_entity__kernel"
  },
  "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.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}