DeDeckerThomas commited on
Commit
be2aa80
·
1 Parent(s): b0dfe16

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +213 -3
README.md CHANGED
@@ -1,3 +1,213 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+
3
+ language: en
4
+ license: mit
5
+ tags:
6
+ - keyphrase-extraction
7
+ datasets:
8
+ - midas/openkkp
9
+ metrics:
10
+ - seqeval
11
+ widget:
12
+ - text: "Keyphrase extraction is a technique in text analysis where you extract the important keyphrases from a text. Since this is a time-consuming process, Artificial Intelligence is used to automate it. Currently, classical machine learning methods, that use statistics and linguistics, are widely used for the extraction process. The fact that these methods have been widely used in the community has the advantage that there are many easy-to-use libraries. Now with the recent innovations in deep learning methods (such as recurrent neural networks and transformers, GANS, …), keyphrase extraction can be improved. These new methods also focus on the semantics and context of a document, which is quite an improvement."
13
+ example_title: "Example 1"
14
+ - text: "In this work, we explore how to learn task specific language models aimed towards learning rich representation of keyphrases from text documents. We experiment with different masking strategies for pre-training transformer language models (LMs) in discriminative as well as generative settings. In the discriminative setting, we introduce a new pre-training objective - Keyphrase Boundary Infilling with Replacement (KBIR), showing large gains in performance (up to 9.26 points in F1) over SOTA, when LM pre-trained using KBIR is fine-tuned for the task of keyphrase extraction. In the generative setting, we introduce a new pre-training setup for BART - KeyBART, that reproduces the keyphrases related to the input text in the CatSeq format, instead of the denoised original input. This also led to gains in performance (up to 4.33 points inF1@M) over SOTA for keyphrase generation. Additionally, we also fine-tune the pre-trained language models on named entity recognition(NER), question answering (QA), relation extraction (RE), abstractive summarization and achieve comparable performance with that of the SOTA, showing that learning rich representation of keyphrases is indeed beneficial for many other fundamental NLP tasks."
15
+ example_title: "Example 2"
16
+ model-index:
17
+ - name: DeDeckerThomas/keyphrase-extraction-distilbert-openkp
18
+ results:
19
+ - task:
20
+ type: keyphrase-extraction
21
+ name: Keyphrase Extraction
22
+ dataset:
23
+ type: midas/openkp
24
+ name: openkp
25
+ metrics:
26
+ - type: seqeval
27
+ value: 0.0
28
+ name: F1-score
29
+ ---
30
+ ** Work in progress **
31
+ # 🔑 Keyphrase Extraction model: distilbert-openkp
32
+ Keyphrase extraction is a technique in text analysis where you extract the important keyphrases from a text. Since this is a time-consuming process, Artificial Intelligence is used to automate it.
33
+ Currently, classical machine learning methods, that use statistics and linguistics, are widely used for the extraction process. The fact that these methods have been widely used in the community has the advantage that there are many easy-to-use libraries.
34
+ Now with the recent innovations in deep learning methods (such as recurrent neural networks and transformers, GANS, …), keyphrase extraction can be improved. These new methods also focus on the semantics and context of a document, which is quite an improvement.
35
+
36
+
37
+ ## 📓 Model Description
38
+ This model is a fine-tuned distilbert model on the openkp dataset.
39
+
40
+ The model is fine-tuned as a token classification problem where the text is labeled using the BIO scheme.
41
+
42
+ | Label | Description |
43
+ | ----- | ------------------------------- |
44
+ | B-KEY | At the beginning of a keyphrase |
45
+ | I-KEY | Inside a keyphrase |
46
+ | O | Outside a keyphrase |
47
+
48
+ ## ✋ Intended uses & limitations
49
+ ### 🛑 Limitations
50
+ <!--* This keyphrase extraction model is very domain-specific and will perform very well on abstracts of scientific papers. It's not recommended to use this model for other domains, but you are free to test it out.-->
51
+ * Only works for English documents.
52
+ * For a custom model, please consult the training notebook for more information (link incoming).
53
+
54
+ ### ❓ How to use
55
+ ```python
56
+ from transformers import (
57
+ TokenClassificationPipeline,
58
+ AutoModelForTokenClassification,
59
+ AutoTokenizer,
60
+ )
61
+ from transformers.pipelines import AggregationStrategy
62
+ import numpy as np
63
+
64
+ # Define keyphrase extraction pipeline
65
+ class KeyphraseExtractionPipeline(TokenClassificationPipeline):
66
+ def __init__(self, model, *args, **kwargs):
67
+ super().__init__(
68
+ model=AutoModelForTokenClassification.from_pretrained(model),
69
+ tokenizer=AutoTokenizer.from_pretrained(model),
70
+ *args,
71
+ **kwargs
72
+ )
73
+
74
+ def postprocess(self, model_outputs):
75
+ results = super().postprocess(
76
+ model_outputs=model_outputs,
77
+ aggregation_strategy=AggregationStrategy.SIMPLE,
78
+ )
79
+ return np.unique([result.get("word").strip() for result in results])
80
+
81
+ ```
82
+
83
+ ```python
84
+ # Load pipeline
85
+ model_name = "DeDeckerThomas/keyphrase-extraction-distilbert-openkp"
86
+ extractor = KeyphraseExtractionPipeline(model=model_name)
87
+ ```
88
+ ```python
89
+ # Inference
90
+ text = """
91
+ Keyphrase extraction is a technique in text analysis where you extract the important keyphrases from a text.
92
+ Since this is a time-consuming process, Artificial Intelligence is used to automate it.
93
+ Currently, classical machine learning methods, that use statistics and linguistics, are widely used for the extraction process.
94
+ The fact that these methods have been widely used in the community has the advantage that there are many easy-to-use libraries.
95
+ Now with the recent innovations in deep learning methods (such as recurrent neural networks and transformers, GANS, …),
96
+ keyphrase extraction can be improved. These new methods also focus on the semantics and context of a document, which is quite an improvement.
97
+ """.replace(
98
+ "\n", ""
99
+ )
100
+
101
+ keyphrases = extractor(text)
102
+
103
+ print(keyphrases)
104
+ ```
105
+
106
+ ```
107
+ # Output
108
+ ['Artificial Intelligence' 'GANS' 'Keyphrase extraction'
109
+ 'classical machine learning' 'deep learning methods'
110
+ 'keyphrase extraction' 'linguistics' 'recurrent neural networks'
111
+ 'semantics' 'statistics' 'text analysis' 'transformers']
112
+ ```
113
+
114
+ ## 📚 Training Dataset
115
+ OpenKP is a keyphrase extraction/generation dataset consisting of
116
+
117
+ You can find more information here: https://huggingface.co/datasets/midas/openkp
118
+
119
+ ## 👷‍♂️ Training procedure
120
+ For more in detail information, you can take a look at the training notebook (link incoming).
121
+
122
+ ### Training parameters
123
+
124
+ | Parameter | Value |
125
+ | --------- | ------|
126
+ | Learning Rate | 1e-4 |
127
+ | Epochs | 50 |
128
+ | Early Stopping Patience | 3 |
129
+
130
+ ### Preprocessing
131
+ The documents in the dataset are already preprocessed into list of words with the corresponding labels. The only thing that must be done is tokenization and the realignment of the labels so that they correspond with the right subword tokens.
132
+ ```python
133
+ def preprocess_fuction(all_samples_per_split):
134
+ tokenized_samples = tokenizer.batch_encode_plus(
135
+ all_samples_per_split[dataset_document_column],
136
+ padding="max_length",
137
+ truncation=True,
138
+ is_split_into_words=True,
139
+ max_length=max_length,
140
+ )
141
+ total_adjusted_labels = []
142
+ for k in range(0, len(tokenized_samples["input_ids"])):
143
+ prev_wid = -1
144
+ word_ids_list = tokenized_samples.word_ids(batch_index=k)
145
+ existing_label_ids = all_samples_per_split[dataset_biotags_column][k]
146
+ i = -1
147
+ adjusted_label_ids = []
148
+
149
+ for wid in word_ids_list:
150
+ if wid is None:
151
+ adjusted_label_ids.append(lbl2idx["O"])
152
+ elif wid != prev_wid:
153
+ i = i + 1
154
+ adjusted_label_ids.append(lbl2idx[existing_label_ids[i]])
155
+ prev_wid = wid
156
+ else:
157
+ adjusted_label_ids.append(
158
+ lbl2idx[
159
+ f"{'I' if existing_label_ids[i] == 'B' else existing_label_ids[i]}"
160
+ ]
161
+ )
162
+
163
+ total_adjusted_labels.append(adjusted_label_ids)
164
+ tokenized_samples["labels"] = total_adjusted_labels
165
+ return tokenized_samples
166
+ ```
167
+
168
+ ### Postprocessing
169
+ For the post-processing, you will need to filter out the B and I labeled tokens and concat the consecutive B and Is. As last you strip the keyphrase to ensure all spaces are removed.
170
+ ```python
171
+ # Define post_process functions
172
+ def concat_tokens_by_tag(keyphrases):
173
+ keyphrase_tokens = []
174
+ for id, label in keyphrases:
175
+ if label == "B":
176
+ keyphrase_tokens.append([id])
177
+ elif label == "I":
178
+ if len(keyphrase_tokens) > 0:
179
+ keyphrase_tokens[len(keyphrase_tokens) - 1].append(id)
180
+ return keyphrase_tokens
181
+
182
+
183
+ def extract_keyphrases(example, predictions, tokenizer, index=0):
184
+ keyphrases_list = [
185
+ (id, idx2label[label])
186
+ for id, label in zip(
187
+ np.array(example["input_ids"]).squeeze().tolist(), predictions[index]
188
+ )
189
+ if idx2label[label] in ["B", "I"]
190
+ ]
191
+
192
+ processed_keyphrases = concat_tokens_by_tag(keyphrases_list)
193
+ extracted_kps = tokenizer.batch_decode(
194
+ processed_keyphrases,
195
+ skip_special_tokens=True,
196
+ clean_up_tokenization_spaces=True,
197
+ )
198
+ return np.unique([kp.strip() for kp in extracted_kps])
199
+
200
+ ```
201
+ ## 📝 Evaluation results
202
+
203
+ One of the traditional evaluation methods is the precision, recall and F1-score @k,m where k is the number that stands for the first k predicted keyphrases and m for the average amount of predicted keyphrases.
204
+ The model achieves the following results on the OpenKP test set:
205
+
206
+ | Dataset | P@5 | R@5 | F1@5 | P@10 | R@10 | F1@10 | P@M | R@M | F1@M |
207
+ |:-----------------:|:----:|:----:|:----:|:----:|:----:|:-----:|:----:|:----:|:----:|
208
+ | OpenKP Test Set | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 |
209
+
210
+ For more information on the evaluation process, you can take a look at the keyphrase extraction evaluation notebook.
211
+
212
+ ## 🚨 Issues
213
+ Please feel free to contact Thomas De Decker for any problems with this model.