Upload 7 files
Browse files- README.md +157 -0
- config (1).json +35 -0
- model (2).safetensors +3 -0
- special_tokens_map (1).json +7 -0
- tokenizer (1).json +0 -0
- tokenizer_config (1).json +56 -0
- vocab (1).txt +0 -0
README.md
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# 🧠 Keyphrase Extraction with BERT (Fine-Tuned on `midas/inspec`)
|
| 3 |
+
|
| 4 |
+
This repository contains a complete pipeline to **fine-tune BERT** for **Keyphrase Extraction** using the [`midas/inspec`](https://huggingface.co/datasets/midas/inspec) dataset. The model performs sequence labeling with BIO tags to extract meaningful phrases from scientific text.
|
| 5 |
+
|
| 6 |
+
---
|
| 7 |
+
|
| 8 |
+
## 🔧 Features
|
| 9 |
+
|
| 10 |
+
- ✅ Preprocessed dataset with BIO-tagged tokens
|
| 11 |
+
- ✅ Fine-tuning BERT (`bert-base-cased`) using Hugging Face Transformers
|
| 12 |
+
- ✅ Token-label alignment
|
| 13 |
+
- ✅ Evaluation using `seqeval` metrics (Precision, Recall, F1)
|
| 14 |
+
- ✅ Inference pipeline to extract keyphrases
|
| 15 |
+
- ✅ CUDA-enabled for GPU acceleration
|
| 16 |
+
|
| 17 |
+
---
|
| 18 |
+
|
| 19 |
+
## 📂 Dataset
|
| 20 |
+
|
| 21 |
+
**Source:** [`midas/inspec`](https://huggingface.co/datasets/midas/inspec)
|
| 22 |
+
|
| 23 |
+
- Fields:
|
| 24 |
+
- `document`: List of tokenized words (already split)
|
| 25 |
+
- `doc_bio_tags`: BIO-format labels for keyphrases
|
| 26 |
+
- Splits:
|
| 27 |
+
- `train`: 1000 samples
|
| 28 |
+
- `validation`: 500 samples
|
| 29 |
+
- `test`: 500 samples
|
| 30 |
+
|
| 31 |
+
---
|
| 32 |
+
|
| 33 |
+
## 🚀 Setup & Installation
|
| 34 |
+
|
| 35 |
+
```bash
|
| 36 |
+
git clone https://github.com/your-username/keyphrase-bert-inspec
|
| 37 |
+
cd keyphrase-bert-inspec
|
| 38 |
+
|
| 39 |
+
pip install -r requirements.txt
|
| 40 |
+
```
|
| 41 |
+
|
| 42 |
+
### `requirements.txt`
|
| 43 |
+
|
| 44 |
+
```text
|
| 45 |
+
datasets
|
| 46 |
+
transformers
|
| 47 |
+
evaluate
|
| 48 |
+
seqeval
|
| 49 |
+
```
|
| 50 |
+
|
| 51 |
+
---
|
| 52 |
+
|
| 53 |
+
## 🧪 Training
|
| 54 |
+
|
| 55 |
+
```python
|
| 56 |
+
from datasets import load_dataset
|
| 57 |
+
from transformers import AutoTokenizer, AutoModelForTokenClassification, TrainingArguments, Trainer
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
1. Load and preprocess data with aligned BIO labels
|
| 61 |
+
2. Fine-tune `bert-base-cased` on the dataset
|
| 62 |
+
3. Evaluate and save model artifacts
|
| 63 |
+
|
| 64 |
+
### Training Script Overview:
|
| 65 |
+
|
| 66 |
+
```python
|
| 67 |
+
trainer = Trainer(
|
| 68 |
+
model=model,
|
| 69 |
+
args=training_args,
|
| 70 |
+
train_dataset=tokenized_datasets["train"],
|
| 71 |
+
eval_dataset=tokenized_datasets["validation"],
|
| 72 |
+
tokenizer=tokenizer,
|
| 73 |
+
data_collator=data_collator,
|
| 74 |
+
compute_metrics=compute_metrics,
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
trainer.train()
|
| 78 |
+
trainer.save_model("keyphrase-bert-inspec")
|
| 79 |
+
```
|
| 80 |
+
|
| 81 |
+
---
|
| 82 |
+
|
| 83 |
+
## 📊 Evaluation Metrics
|
| 84 |
+
|
| 85 |
+
```python
|
| 86 |
+
{
|
| 87 |
+
"precision": 0.84,
|
| 88 |
+
"recall": 0.81,
|
| 89 |
+
"f1": 0.825,
|
| 90 |
+
"accuracy": 0.88
|
| 91 |
+
}
|
| 92 |
+
```
|
| 93 |
+
|
| 94 |
+
---
|
| 95 |
+
|
| 96 |
+
## 🔍 Inference Example
|
| 97 |
+
|
| 98 |
+
```python
|
| 99 |
+
from transformers import pipeline
|
| 100 |
+
|
| 101 |
+
ner_pipeline = pipeline(
|
| 102 |
+
"ner",
|
| 103 |
+
model="keyphrase-bert-inspec",
|
| 104 |
+
tokenizer="keyphrase-bert-inspec",
|
| 105 |
+
aggregation_strategy="simple"
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
text = "Information-based semantics is a theory in the philosophy of mind."
|
| 109 |
+
results = ner_pipeline(text)
|
| 110 |
+
|
| 111 |
+
for r in results:
|
| 112 |
+
print(f"{r['word']} ({r['entity_group']}) - {r['score']:.2f}")
|
| 113 |
+
```
|
| 114 |
+
|
| 115 |
+
### Sample Output
|
| 116 |
+
|
| 117 |
+
```
|
| 118 |
+
🟢 Extracted Keyphrases:
|
| 119 |
+
- Information-based semantics (score: 0.94)
|
| 120 |
+
- philosophy of mind (score: 0.91)
|
| 121 |
+
```
|
| 122 |
+
|
| 123 |
+
---
|
| 124 |
+
|
| 125 |
+
## 💾 Model Artifacts
|
| 126 |
+
|
| 127 |
+
After training, the model and tokenizer are saved as:
|
| 128 |
+
|
| 129 |
+
```
|
| 130 |
+
keyphrase-bert-inspec/
|
| 131 |
+
├── config.json
|
| 132 |
+
├── pytorch_model.bin
|
| 133 |
+
├── tokenizer_config.json
|
| 134 |
+
├── vocab.txt
|
| 135 |
+
```
|
| 136 |
+
|
| 137 |
+
---
|
| 138 |
+
|
| 139 |
+
## 📌 Future Improvements
|
| 140 |
+
|
| 141 |
+
- Add postprocessing to group fragmented tokens
|
| 142 |
+
- Use a larger dataset (like `scientific_keyphrases`)
|
| 143 |
+
- Convert to a web app using Gradio or Streamlit
|
| 144 |
+
|
| 145 |
+
---
|
| 146 |
+
|
| 147 |
+
## 👨🔬 Author
|
| 148 |
+
|
| 149 |
+
**Your Name**
|
| 150 |
+
GitHub: [@your-username](https://github.com/your-username)
|
| 151 |
+
Contact: your.email@example.com
|
| 152 |
+
|
| 153 |
+
---
|
| 154 |
+
|
| 155 |
+
## 📄 License
|
| 156 |
+
|
| 157 |
+
MIT License. See `LICENSE` file.
|
config (1).json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"BertForTokenClassification"
|
| 4 |
+
],
|
| 5 |
+
"attention_probs_dropout_prob": 0.1,
|
| 6 |
+
"classifier_dropout": null,
|
| 7 |
+
"gradient_checkpointing": false,
|
| 8 |
+
"hidden_act": "gelu",
|
| 9 |
+
"hidden_dropout_prob": 0.1,
|
| 10 |
+
"hidden_size": 768,
|
| 11 |
+
"id2label": {
|
| 12 |
+
"0": "O",
|
| 13 |
+
"1": "B",
|
| 14 |
+
"2": "I"
|
| 15 |
+
},
|
| 16 |
+
"initializer_range": 0.02,
|
| 17 |
+
"intermediate_size": 3072,
|
| 18 |
+
"label2id": {
|
| 19 |
+
"B": 1,
|
| 20 |
+
"I": 2,
|
| 21 |
+
"O": 0
|
| 22 |
+
},
|
| 23 |
+
"layer_norm_eps": 1e-12,
|
| 24 |
+
"max_position_embeddings": 512,
|
| 25 |
+
"model_type": "bert",
|
| 26 |
+
"num_attention_heads": 12,
|
| 27 |
+
"num_hidden_layers": 12,
|
| 28 |
+
"pad_token_id": 0,
|
| 29 |
+
"position_embedding_type": "absolute",
|
| 30 |
+
"torch_dtype": "float16",
|
| 31 |
+
"transformers_version": "4.51.3",
|
| 32 |
+
"type_vocab_size": 2,
|
| 33 |
+
"use_cache": true,
|
| 34 |
+
"vocab_size": 28996
|
| 35 |
+
}
|
model (2).safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bb746d06f9e1af11b95f06a2ba6440f24907a80bff33075dabea15b969fb880e
|
| 3 |
+
size 215467198
|
special_tokens_map (1).json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cls_token": "[CLS]",
|
| 3 |
+
"mask_token": "[MASK]",
|
| 4 |
+
"pad_token": "[PAD]",
|
| 5 |
+
"sep_token": "[SEP]",
|
| 6 |
+
"unk_token": "[UNK]"
|
| 7 |
+
}
|
tokenizer (1).json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config (1).json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"added_tokens_decoder": {
|
| 3 |
+
"0": {
|
| 4 |
+
"content": "[PAD]",
|
| 5 |
+
"lstrip": false,
|
| 6 |
+
"normalized": false,
|
| 7 |
+
"rstrip": false,
|
| 8 |
+
"single_word": false,
|
| 9 |
+
"special": true
|
| 10 |
+
},
|
| 11 |
+
"100": {
|
| 12 |
+
"content": "[UNK]",
|
| 13 |
+
"lstrip": false,
|
| 14 |
+
"normalized": false,
|
| 15 |
+
"rstrip": false,
|
| 16 |
+
"single_word": false,
|
| 17 |
+
"special": true
|
| 18 |
+
},
|
| 19 |
+
"101": {
|
| 20 |
+
"content": "[CLS]",
|
| 21 |
+
"lstrip": false,
|
| 22 |
+
"normalized": false,
|
| 23 |
+
"rstrip": false,
|
| 24 |
+
"single_word": false,
|
| 25 |
+
"special": true
|
| 26 |
+
},
|
| 27 |
+
"102": {
|
| 28 |
+
"content": "[SEP]",
|
| 29 |
+
"lstrip": false,
|
| 30 |
+
"normalized": false,
|
| 31 |
+
"rstrip": false,
|
| 32 |
+
"single_word": false,
|
| 33 |
+
"special": true
|
| 34 |
+
},
|
| 35 |
+
"103": {
|
| 36 |
+
"content": "[MASK]",
|
| 37 |
+
"lstrip": false,
|
| 38 |
+
"normalized": false,
|
| 39 |
+
"rstrip": false,
|
| 40 |
+
"single_word": false,
|
| 41 |
+
"special": true
|
| 42 |
+
}
|
| 43 |
+
},
|
| 44 |
+
"clean_up_tokenization_spaces": false,
|
| 45 |
+
"cls_token": "[CLS]",
|
| 46 |
+
"do_lower_case": false,
|
| 47 |
+
"extra_special_tokens": {},
|
| 48 |
+
"mask_token": "[MASK]",
|
| 49 |
+
"model_max_length": 512,
|
| 50 |
+
"pad_token": "[PAD]",
|
| 51 |
+
"sep_token": "[SEP]",
|
| 52 |
+
"strip_accents": null,
|
| 53 |
+
"tokenize_chinese_chars": true,
|
| 54 |
+
"tokenizer_class": "BertTokenizer",
|
| 55 |
+
"unk_token": "[UNK]"
|
| 56 |
+
}
|
vocab (1).txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|