{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!pip install svgling\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!pip install transformers datasets evaluate seqeval" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from datasets import load_dataset\n", "import pandas as pd\n", "\n", "wnut = load_dataset(\"wnut_17\")\n", "df = pd.DataFrame(wnut[\"train\"]) # Assuming 'train' split, you can choose other splits\n", "\n", "# Save the DataFrame to a CSV file\n", "df.to_csv('dataset.csv', index=False) # Change 'dataset.csv' to your desired file name\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "wnut[\"train\"][0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import nltk\n", "import ssl\n", "\n", "try:\n", " _create_unverified_https_context = ssl._create_unverified_context\n", "except AttributeError:\n", " pass\n", "else:\n", " ssl._create_default_https_context = _create_unverified_https_context\n", "\n", "nltk.download('punkt')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "label_list = wnut[\"train\"].features[f\"ner_tags\"].feature.names\n", "label_list" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from transformers import AutoTokenizer\n", "\n", "tokenizer = AutoTokenizer.from_pretrained(\"distilbert/distilbert-base-uncased\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "example = wnut[\"train\"][0]\n", "tokenized_input = tokenizer(example[\"tokens\"], is_split_into_words=True)\n", "tokens = tokenizer.convert_ids_to_tokens(tokenized_input[\"input_ids\"])\n", "tokens" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def tokenize_and_align_labels(examples):\n", " tokenized_inputs = tokenizer(examples[\"tokens\"], truncation=True, is_split_into_words=True)\n", "\n", " labels = []\n", " for i, label in enumerate(examples[f\"ner_tags\"]):\n", " word_ids = tokenized_inputs.word_ids(batch_index=i) # Map tokens to their respective word.\n", " previous_word_idx = None\n", " label_ids = []\n", " for word_idx in word_ids: # Set the special tokens to -100.\n", " if word_idx is None:\n", " label_ids.append(-100)\n", " elif word_idx != previous_word_idx: # Only label the first token of a given word.\n", " label_ids.append(label[word_idx])\n", " else:\n", " label_ids.append(-100)\n", " previous_word_idx = word_idx\n", " labels.append(label_ids)\n", "\n", " tokenized_inputs[\"labels\"] = labels\n", " return tokenized_inputs" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tokenized_wnut = wnut.map(tokenize_and_align_labels, batched=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tokenized_wnut = wnut.map(tokenize_and_align_labels, batched=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import evaluate\n", "\n", "seqeval = evaluate.load(\"seqeval\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from transformers import DataCollatorForTokenClassification\n", "\n", "data_collator = DataCollatorForTokenClassification(tokenizer=tokenizer)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import evaluate\n", "\n", "seqeval = evaluate.load(\"seqeval\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "labels = [label_list[i] for i in example[f\"ner_tags\"]]\n", "\n", "\n", "def compute_metrics(p):\n", " predictions, labels = p\n", " predictions = np.argmax(predictions, axis=2)\n", "\n", " true_predictions = [\n", " [label_list[p] for (p, l) in zip(prediction, label) if l != -100]\n", " for prediction, label in zip(predictions, labels)\n", " ]\n", " true_labels = [\n", " [label_list[l] for (p, l) in zip(prediction, label) if l != -100]\n", " for prediction, label in zip(predictions, labels)\n", " ]\n", "\n", " results = seqeval.compute(predictions=true_predictions, references=true_labels)\n", " return {\n", " \"precision\": results[\"overall_precision\"],\n", " \"recall\": results[\"overall_recall\"],\n", " \"f1\": results[\"overall_f1\"],\n", " \"accuracy\": results[\"overall_accuracy\"],\n", " }" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "id2label = {\n", " 0: \"O\",\n", " 1: \"B-corporation\",\n", " 2: \"I-corporation\",\n", " 3: \"B-creative-work\",\n", " 4: \"I-creative-work\",\n", " 5: \"B-group\",\n", " 6: \"I-group\",\n", " 7: \"B-location\",\n", " 8: \"I-location\",\n", " 9: \"B-person\",\n", " 10: \"I-person\",\n", " 11: \"B-product\",\n", " 12: \"I-product\",\n", "}\n", "label2id = {\n", " \"O\": 0,\n", " \"B-corporation\": 1,\n", " \"I-corporation\": 2,\n", " \"B-creative-work\": 3,\n", " \"I-creative-work\": 4,\n", " \"B-group\": 5,\n", " \"I-group\": 6,\n", " \"B-location\": 7,\n", " \"I-location\": 8,\n", " \"B-person\": 9,\n", " \"I-person\": 10,\n", " \"B-product\": 11,\n", " \"I-product\": 12,\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from transformers import AutoModelForTokenClassification, TrainingArguments, Trainer\n", "\n", "model = AutoModelForTokenClassification.from_pretrained(\n", " \"distilbert/distilbert-base-uncased\", num_labels=13, id2label=id2label, label2id=label2id\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "training_args = TrainingArguments(\n", " output_dir=\"my_awesome_wnut_model\",\n", " learning_rate=2e-5,\n", " per_device_train_batch_size=16,\n", " per_device_eval_batch_size=16,\n", " num_train_epochs=2,\n", " weight_decay=0.01,\n", " evaluation_strategy=\"epoch\",\n", " save_strategy=\"epoch\",\n", " load_best_model_at_end=True,\n", " push_to_hub=False,\n", ")\n", "\n", "trainer = Trainer(\n", " model=model,\n", " args=training_args,\n", " train_dataset=tokenized_wnut[\"train\"],\n", " eval_dataset=tokenized_wnut[\"test\"],\n", " tokenizer=tokenizer,\n", " data_collator=data_collator,\n", " compute_metrics=compute_metrics,\n", ")\n", "\n", "trainer.train()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from transformers import pipeline\n", "\n", "text = \"Let's meet for Lunch Tomorrow at 12 PM at the Italian restaurant on Main Street. Simon\"\n", "classifier = pipeline(\"ner\", model=model, tokenizer=tokenizer)\n", "classifier(text)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.12.1" } }, "nbformat": 4, "nbformat_minor": 2 }