ullahhafeez321 commited on
Commit
de57b22
·
verified ·
1 Parent(s): 5454a70

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +159 -55
README.md CHANGED
@@ -3,7 +3,7 @@ language:
3
  - en
4
  license: apache-2.0
5
  library_name: transformers
6
- pipeline_tag: token-classification
7
  tags:
8
  - named-entity-recognition
9
  - ner
@@ -11,6 +11,7 @@ tags:
11
  - decoder-only
12
  - lora
13
  - conll2003
 
14
  base_model: Qwen/Qwen2.5-0.5B-Instruct
15
  datasets:
16
  - lhoestq/conll2003
@@ -20,24 +21,24 @@ datasets:
20
 
21
  ## Model Description
22
 
23
- This project is a fine-tuned **decoder-only Named Entity Recognition (NER) model** based on [`Qwen/Qwen2.5-0.5B-Instruct`](https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct).
24
 
25
- The model is trained to identify and extract named entities from English text and return them in structured JSON format.
26
 
27
- It recognizes four standard CoNLL-2003 entity categories:
28
 
29
  - **PER** – Person
30
  - **ORG** – Organization
31
  - **LOC** – Location
32
  - **MISC** – Miscellaneous
33
 
34
- The model uses **Low-Rank Adaptation (LoRA)** for parameter-efficient fine-tuning.
35
 
36
  ## Model Details
37
 
38
  | Property | Details |
39
  |---|---|
40
- | Model Name | English NER Decoder |
41
  | Base Model | Qwen/Qwen2.5-0.5B-Instruct |
42
  | Architecture | Decoder-only Transformer |
43
  | Fine-tuning Method | LoRA |
@@ -48,33 +49,44 @@ The model uses **Low-Rank Adaptation (LoRA)** for parameter-efficient fine-tunin
48
  | Language | English |
49
  | Task | Named Entity Recognition |
50
  | Output Format | JSON |
 
51
 
52
  ## Dataset
53
 
54
- The model was fine-tuned using the **CoNLL-2003 English Named Entity Recognition dataset**.
55
 
56
- Dataset: [`lhoestq/conll2003`](https://huggingface.co/datasets/lhoestq/conll2003)
57
 
58
  The dataset contains English sentences annotated with BIO-style named entity labels.
59
 
60
- The four entity categories are PER, ORG, LOC, and MISC.
 
 
 
 
 
 
 
61
 
62
  ## Training Configuration
63
 
64
  The model was fine-tuned using LoRA with the following configuration:
65
 
66
- - Base model: Qwen2.5-0.5B-Instruct
67
- - LoRA rank: 8
68
- - LoRA alpha: 16
69
- - LoRA dropout: 0.05
70
- - Target modules: q_proj, k_proj, v_proj, o_proj
71
- - Training epochs: 1
72
- - Training samples: Up to 8,000
73
- - Validation samples: 1,000
74
- - Training precision: FP16
75
- - Optimizer: AdamW
 
 
 
76
 
77
- The training was performed using a Kaggle GPU environment.
78
 
79
  ## Evaluation Results
80
 
@@ -89,11 +101,13 @@ The model was evaluated on a quick test subset of 100 English sentences using ex
89
  | False Positives | 17 |
90
  | False Negatives | 36 |
91
 
92
- **Note:** These results represent the 100-sample evaluation subset, not the complete test dataset. They should not be interpreted as a full-dataset benchmark.
93
 
94
  Additional manually selected English sentences were used to inspect entity extraction behavior.
95
 
96
- ## Example
 
 
97
 
98
  **Input:**
99
 
@@ -118,7 +132,7 @@ Elon Musk founded SpaceX.
118
  }
119
  ```
120
 
121
- Another example:
122
 
123
  **Input:**
124
 
@@ -131,6 +145,10 @@ Barack Obama visited Microsoft headquarters in Seattle.
131
  ```json
132
  {
133
  "entities": [
 
 
 
 
134
  {
135
  "text": "Microsoft",
136
  "type": "ORG"
@@ -143,71 +161,152 @@ Barack Obama visited Microsoft headquarters in Seattle.
143
  }
144
  ```
145
 
146
- ## Intended Use
147
-
148
- This model is intended for:
149
-
150
- - English named entity extraction.
151
- - Natural Language Processing (NLP) applications.
152
- - Information extraction systems.
153
- - Text analysis and document processing.
154
- - Educational and research projects.
155
- - Integration with sentiment classification systems.
156
-
157
- ## Limitations
158
-
159
- - The model may occasionally miss named entities.
160
- - Some entities may receive incorrect classifications.
161
- - Entity boundaries and spelling may occasionally be incorrect.
162
- - Performance can vary across different domains and writing styles.
163
- - The reported evaluation score is based on a limited 100-sample test subset.
164
- - The model is not intended to replace human verification in critical applications.
165
 
166
  ## How to Use
167
 
168
- Install the required libraries:
169
 
170
  ```bash
171
- pip install transformers peft accelerate torch
172
  ```
173
 
174
- Load the base model and LoRA adapter:
175
 
176
  ```python
177
  import torch
178
  from transformers import AutoTokenizer, AutoModelForCausalLM
179
- from peft import PeftModel
180
-
181
- BASE_MODEL = "Qwen/Qwen2.5-0.5B-Instruct"
182
 
183
- # Replace with your Hugging Face repository ID
184
  ADAPTER_MODEL = "ullahhafeez321/Qwen-NER-Decoder"
185
 
186
- tokenizer = AutoTokenizer.from_pretrained(ADAPTER_MODEL)
 
 
187
 
 
 
 
 
 
 
 
188
  base_model = AutoModelForCausalLM.from_pretrained(
189
  BASE_MODEL,
190
- torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
 
 
 
191
  device_map="auto"
192
  )
193
 
 
194
  model = PeftModel.from_pretrained(
195
  base_model,
196
  ADAPTER_MODEL
197
  )
198
 
199
  model.eval()
 
 
200
  ```
201
 
202
- The adapter must be loaded together with its original base model.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
 
204
  ## Model Architecture
205
 
206
- This project uses a decoder-only Transformer architecture.
207
 
208
  The model generates structured entity extraction results from an English input sentence.
209
 
210
- LoRA fine-tuning adapts selected attention projection layers without updating all the original base-model parameters.
 
 
211
 
212
  ## Future Improvements
213
 
@@ -216,6 +315,7 @@ LoRA fine-tuning adapts selected attention projection layers without updating al
216
  - Evaluate performance on the complete test dataset.
217
  - Improve entity boundary detection.
218
  - Expand evaluation across different English text domains.
 
219
  - Integrate with an encoder-only BERT sentiment classification model.
220
 
221
  ## Acknowledgements
@@ -226,10 +326,14 @@ LoRA fine-tuning adapts selected attention projection layers without updating al
226
 
227
  ## License
228
 
229
- Please refer to the base model and dataset licenses before redistributing or using this fine-tuned adapter.
230
 
231
  ---
232
 
233
  **Project:** English Named Entity Recognition using a Decoder-only Transformer with LoRA Fine-tuning.
234
 
 
 
 
 
235
  **Frameworks:** PyTorch, Transformers, PEFT, Hugging Face Datasets.
 
3
  - en
4
  license: apache-2.0
5
  library_name: transformers
6
+ pipeline_tag: text-generation
7
  tags:
8
  - named-entity-recognition
9
  - ner
 
11
  - decoder-only
12
  - lora
13
  - conll2003
14
+ - information-extraction
15
  base_model: Qwen/Qwen2.5-0.5B-Instruct
16
  datasets:
17
  - lhoestq/conll2003
 
21
 
22
  ## Model Description
23
 
24
+ **Qwen-NER-Decoder** is a fine-tuned decoder-only Named Entity Recognition (NER) model based on [Qwen/Qwen2.5-0.5B-Instruct](https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct).
25
 
26
+ The model identifies and extracts named entities from English text and generates structured JSON output.
27
 
28
+ It is fine-tuned on the CoNLL-2003 English Named Entity Recognition dataset and recognizes four entity categories:
29
 
30
  - **PER** – Person
31
  - **ORG** – Organization
32
  - **LOC** – Location
33
  - **MISC** – Miscellaneous
34
 
35
+ The model uses **Low-Rank Adaptation (LoRA)** for parameter-efficient fine-tuning. The Hugging Face repository contains the LoRA adapter, so it must be loaded together with its original base model.
36
 
37
  ## Model Details
38
 
39
  | Property | Details |
40
  |---|---|
41
+ | Model Name | Qwen-NER-Decoder |
42
  | Base Model | Qwen/Qwen2.5-0.5B-Instruct |
43
  | Architecture | Decoder-only Transformer |
44
  | Fine-tuning Method | LoRA |
 
49
  | Language | English |
50
  | Task | Named Entity Recognition |
51
  | Output Format | JSON |
52
+ | Training Framework | PyTorch, Transformers, PEFT |
53
 
54
  ## Dataset
55
 
56
+ The model was fine-tuned using the [CoNLL-2003 English NER dataset](https://huggingface.co/datasets/lhoestq/conll2003).
57
 
58
+ Dataset repository: `lhoestq/conll2003`
59
 
60
  The dataset contains English sentences annotated with BIO-style named entity labels.
61
 
62
+ The four entity categories are:
63
+
64
+ | Label | Description |
65
+ |---|---|
66
+ | PER | Person |
67
+ | ORG | Organization |
68
+ | LOC | Location |
69
+ | MISC | Miscellaneous |
70
 
71
  ## Training Configuration
72
 
73
  The model was fine-tuned using LoRA with the following configuration:
74
 
75
+ | Configuration | Value |
76
+ |---|---|
77
+ | Base Model | Qwen2.5-0.5B-Instruct |
78
+ | LoRA Rank | 8 |
79
+ | LoRA Alpha | 16 |
80
+ | LoRA Dropout | 0.05 |
81
+ | Target Modules | q_proj, k_proj, v_proj, o_proj |
82
+ | Training Epochs | 1 |
83
+ | Training Samples | Up to 8,000 |
84
+ | Validation Samples | 1,000 |
85
+ | Training Precision | FP16 |
86
+ | Optimizer | AdamW |
87
+ | Training Environment | Kaggle GPU |
88
 
89
+ LoRA adapts selected attention projection layers without updating all the parameters of the original base model.
90
 
91
  ## Evaluation Results
92
 
 
101
  | False Positives | 17 |
102
  | False Negatives | 36 |
103
 
104
+ **Evaluation note:** These results represent the 100-sample evaluation subset, not the complete test dataset. They should not be interpreted as a full-dataset benchmark.
105
 
106
  Additional manually selected English sentences were used to inspect entity extraction behavior.
107
 
108
+ ## Example Predictions
109
+
110
+ ### Example 1
111
 
112
  **Input:**
113
 
 
132
  }
133
  ```
134
 
135
+ ### Example 2
136
 
137
  **Input:**
138
 
 
145
  ```json
146
  {
147
  "entities": [
148
+ {
149
+ "text": "Barack Obama",
150
+ "type": "PER"
151
+ },
152
  {
153
  "text": "Microsoft",
154
  "type": "ORG"
 
161
  }
162
  ```
163
 
164
+ *Examples illustrate the intended output structure.*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
 
166
  ## How to Use
167
 
168
+ ### 1. Install Dependencies
169
 
170
  ```bash
171
+ pip install torch transformers peft accelerate safetensors
172
  ```
173
 
174
+ ### 2. Load the Base Model and LoRA Adapter
175
 
176
  ```python
177
  import torch
178
  from transformers import AutoTokenizer, AutoModelForCausalLM
179
+ from peft import PeftConfig, PeftModel
 
 
180
 
 
181
  ADAPTER_MODEL = "ullahhafeez321/Qwen-NER-Decoder"
182
 
183
+ # Read the original base model from the adapter configuration
184
+ adapter_config = PeftConfig.from_pretrained(ADAPTER_MODEL)
185
+ BASE_MODEL = adapter_config.base_model_name_or_path
186
 
187
+ # Load tokenizer from the base model
188
+ tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
189
+
190
+ if tokenizer.pad_token_id is None:
191
+ tokenizer.pad_token = tokenizer.eos_token
192
+
193
+ # Load the original Qwen base model
194
  base_model = AutoModelForCausalLM.from_pretrained(
195
  BASE_MODEL,
196
+ torch_dtype=(
197
+ torch.float16 if torch.cuda.is_available()
198
+ else torch.float32
199
+ ),
200
  device_map="auto"
201
  )
202
 
203
+ # Attach the fine-tuned LoRA adapter
204
  model = PeftModel.from_pretrained(
205
  base_model,
206
  ADAPTER_MODEL
207
  )
208
 
209
  model.eval()
210
+
211
+ print("Qwen NER model loaded successfully!")
212
  ```
213
 
214
+ ### 3. Run Inference
215
+
216
+ The prompt used during inference should follow the same instruction and output format used during fine-tuning.
217
+
218
+ The following is an illustrative JSON extraction example:
219
+
220
+ ```python
221
+ sentence = "Albert Einstein worked at Princeton University."
222
+
223
+ prompt = f"""
224
+ Extract named entities from the following sentence.
225
+
226
+ Entity types:
227
+ PER = Person
228
+ ORG = Organization
229
+ LOC = Location
230
+ MISC = Miscellaneous
231
+
232
+ Return only a JSON array:
233
+
234
+ [
235
+ {{"text": "entity name", "type": "PER"}}
236
+ ]
237
+
238
+ If no entities are found, return [].
239
+
240
+ Sentence: {sentence}
241
+
242
+ Entities:
243
+ """
244
+
245
+ inputs = tokenizer(
246
+ prompt,
247
+ return_tensors="pt",
248
+ truncation=True,
249
+ max_length=1024
250
+ )
251
+
252
+ input_device = model.get_input_embeddings().weight.device
253
+ inputs = {
254
+ key: value.to(input_device)
255
+ for key, value in inputs.items()
256
+ }
257
+
258
+ with torch.inference_mode():
259
+ outputs = model.generate(
260
+ **inputs,
261
+ max_new_tokens=150,
262
+ do_sample=False,
263
+ pad_token_id=tokenizer.pad_token_id,
264
+ eos_token_id=tokenizer.eos_token_id
265
+ )
266
+
267
+ generated_tokens = outputs[0][inputs["input_ids"].shape[1]:]
268
+
269
+ result = tokenizer.decode(
270
+ generated_tokens,
271
+ skip_special_tokens=True
272
+ )
273
+
274
+ print(result)
275
+ ```
276
+
277
+ **Note:** This example demonstrates the inference process. For the best results, use the exact prompt template and target format used to train the uploaded adapter.
278
+
279
+ ## Intended Use
280
+
281
+ This model is intended for:
282
+
283
+ - English named entity recognition.
284
+ - Natural Language Processing applications.
285
+ - Information extraction systems.
286
+ - Text analysis and document processing.
287
+ - Educational and research projects.
288
+ - Integration with sentiment classification systems.
289
+ - Applications that require structured entity extraction.
290
+
291
+ ## Limitations
292
+
293
+ - The model may occasionally miss named entities.
294
+ - Some entities may receive incorrect classifications.
295
+ - Entity boundaries and spelling may occasionally be incorrect.
296
+ - Performance can vary across different domains and writing styles.
297
+ - The reported evaluation score is based on a limited 100-sample test subset.
298
+ - The model is intended for English text and may not perform reliably on other languages.
299
+ - The model is not intended to replace human verification in critical applications.
300
 
301
  ## Model Architecture
302
 
303
+ This project uses a decoder-only Transformer architecture based on Qwen2.5-0.5B-Instruct.
304
 
305
  The model generates structured entity extraction results from an English input sentence.
306
 
307
+ LoRA fine-tuning adapts selected attention projection layers while preserving the original base model weights.
308
+
309
+ The uploaded Hugging Face repository contains the fine-tuned adapter rather than a complete standalone model checkpoint.
310
 
311
  ## Future Improvements
312
 
 
315
  - Evaluate performance on the complete test dataset.
316
  - Improve entity boundary detection.
317
  - Expand evaluation across different English text domains.
318
+ - Improve inference consistency and structured JSON generation.
319
  - Integrate with an encoder-only BERT sentiment classification model.
320
 
321
  ## Acknowledgements
 
326
 
327
  ## License
328
 
329
+ The adapter repository declares the Apache-2.0 license. Please also review the original base model and dataset licenses and their respective usage conditions before redistribution or commercial use.
330
 
331
  ---
332
 
333
  **Project:** English Named Entity Recognition using a Decoder-only Transformer with LoRA Fine-tuning.
334
 
335
+ **Model Repository:** `ullahhafeez321/Qwen-NER-Decoder`
336
+
337
+ **Base Model:** `Qwen/Qwen2.5-0.5B-Instruct`
338
+
339
  **Frameworks:** PyTorch, Transformers, PEFT, Hugging Face Datasets.