ymlee13 commited on
Commit
1050a4e
·
verified ·
1 Parent(s): d2b5061

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +214 -1
README.md CHANGED
@@ -19,4 +19,217 @@ widget:
19
  example_title: "Chinese Address Example"
20
  - text: "ROOM 2107, 42/F, WINNING HEIGHTS, 277 CASTLE PEAK ROAD, TSUEN WAN, NEW TERRITORIES"
21
  example_title: "English Address Example"
22
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  example_title: "Chinese Address Example"
20
  - text: "ROOM 2107, 42/F, WINNING HEIGHTS, 277 CASTLE PEAK ROAD, TSUEN WAN, NEW TERRITORIES"
21
  example_title: "English Address Example"
22
+ ---
23
+
24
+ # Qwen2.5-3B-Instruct Address Formatter (LoRA Adapter)
25
+
26
+ This repository contains the LoRA adapter weights for fine-tuning the `Qwen/Qwen2.5-3B-Instruct` model on the task of **Hong Kong address parsing and formatting**.
27
+
28
+ The model is designed to split unstructured, messy address text into two clean, structured lines following Hong Kong addressing conventions:
29
+ - **Line 1**: Specific location details (e.g., floor, unit, building name)
30
+ - **Line 2**: General location details (e.g., street, district, region)
31
+
32
+ ---
33
+
34
+ ## Model Details
35
+
36
+ | Attribute | Value |
37
+ |-----------|-------|
38
+ | **Base Model** | [Qwen/Qwen2.5-3B-Instruct](https://huggingface.co/Qwen/Qwen2.5-3B-Instruct) |
39
+ | **Fine-tuning Method** | LoRA (Low-Rank Adaptation) |
40
+ | **Task** | Address Parsing & Formatting |
41
+ | **Language Support** | English, Traditional Chinese, Simplified Chinese |
42
+ | **Quantization** | 4-bit (NF4) for training; supports fp16/4-bit for inference |
43
+ | **License** | MIT |
44
+
45
+ ### Training Configuration
46
+
47
+ | Hyperparameter | Value |
48
+ |----------------|-------|
49
+ | LoRA Rank (`r`) | 8 |
50
+ | LoRA Alpha (`lora_alpha`) | 16 |
51
+ | LoRA Dropout | 0.15 |
52
+ | Target Modules | `q_proj`, `k_proj`, `v_proj`, `o_proj`, `gate_proj`, `up_proj`, `down_proj` |
53
+ | Learning Rate | 2e-5 |
54
+ | Epochs | 3 |
55
+ | Batch Size (per device) | 4 |
56
+ | Gradient Accumulation Steps | 2 |
57
+ | Optimizer | paged_adamw_8bit |
58
+ | Scheduler | Cosine |
59
+ | Weight Decay | 0.01 |
60
+ | Max Sequence Length | 256 |
61
+
62
+ ### Post-Processing Features
63
+
64
+ The associated inference pipeline includes intelligent post-processing:
65
+ - **Character Pooling**: Matches model output characters to the original input.
66
+ - **Omission Detection & Re-insertion**: Identifies missing characters and re-inserts them into the correct line.
67
+ - **Language Detection**: Handles Chinese and English addresses with different strategies.
68
+ - **Traditional Chinese Conversion**: Converts Simplified Chinese to Traditional using OpenCC.
69
+ - **English Spell Checking**: Identifies and suggests corrections for misspelled words.
70
+
71
+ ---
72
+
73
+ ## Intended Use
74
+
75
+ This model is intended for **Hong Kong address formatting** tasks, particularly for cleaning and structuring address data for logistics, e-commerce, and administrative applications.
76
+
77
+ ### Quick Start
78
+
79
+ Load the adapter with the base model using the `peft` library:
80
+
81
+ ```python
82
+ import torch
83
+ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
84
+ from peft import PeftModel
85
+
86
+ # Load base model (with optional 4-bit quantization for memory efficiency)
87
+ base_model_name = "Qwen/Qwen2.5-3B-Instruct"
88
+ adapter_name = "ymlee13/Qwen2.5-3B-Instruct_Address_Formatter"
89
+
90
+ quantization_config = BitsAndBytesConfig(
91
+ load_in_4bit=True,
92
+ bnb_4bit_quant_type="nf4",
93
+ bnb_4bit_compute_dtype=torch.bfloat16,
94
+ bnb_4bit_use_double_quant=True,
95
+ )
96
+
97
+ model = AutoModelForCausalLM.from_pretrained(
98
+ base_model_name,
99
+ quantization_config=quantization_config,
100
+ device_map="auto"
101
+ )
102
+ tokenizer = AutoTokenizer.from_pretrained(base_model_name)
103
+ model = PeftModel.from_pretrained(model, adapter_name)
104
+
105
+ # Inference
106
+ def format_address(address_text):
107
+ messages = [
108
+ {"role": "system", "content": "You are an address formatting assistant. You always return the formatted address with 'Line 1:' and 'Line 2:'."},
109
+ {"role": "user", "content": address_text},
110
+ ]
111
+ prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
112
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
113
+
114
+ with torch.no_grad():
115
+ outputs = model.generate(**inputs, max_new_tokens=100, pad_token_id=tokenizer.eos_token_id)
116
+
117
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
118
+ # Parse lines
119
+ import re
120
+ l1_match = re.search(r"Line\s*1:\s*(.+)", response, re.IGNORECASE)
121
+ l2_match = re.search(r"Line\s*2:\s*(.+)", response, re.IGNORECASE)
122
+ line1 = l1_match.group(1).strip() if l1_match else ""
123
+ line2 = l2_match.group(1).strip() if l2_match else ""
124
+ return line1, line2
125
+
126
+ # Example usage
127
+ print(format_address("九龍觀塘區雲漢街61號南寧大樓地庫01舖"))
128
+ # Output: ('南寧大樓地庫01舖', '九龍觀塘區雲漢街61號')
129
+ ```
130
+ For a complete inference pipeline with batch processing and advanced post-processing, please see the full project on GitHub: ymlee13/address_parsing
131
+
132
+ ---
133
+ ## Training Data
134
+ The model was fine-tuned on a dataset of 570+ Hong Kong addresses generated from real geospatial data.
135
+
136
+ ### Data Sources
137
+ - **ALS-GeoJSON**: Hong Kong government dataset containing detailed address information.
138
+ - **Synthetic Generation**: Custom script (gen_real_address.ipynb) was used to create structured examples from the raw geospatial features.
139
+
140
+ ### Data Format
141
+ Each training example consists of:
142
+ - Input: A messy, unstructured address string.
143
+ - Output: A structured JSON object with line1 and line2.
144
+
145
+ Example:
146
+ ``` json
147
+ "input": "九龍觀塘區雲漢街61號南寧大樓地庫01舖",
148
+ "output": {"line1": "南寧大樓地庫01舖", "line2": "九龍觀塘區雲漢街61號"}
149
+ ```
150
+
151
+ ---
152
+ ## Performance & Evaluation
153
+
154
+ ### Evaluation Metrics
155
+ The model was evaluated on a held-out test set using character-level similarity (SequenceMatcher ratio).
156
+
157
+ | Metric | Score (Typical) |
158
+ |--------|-----------------|
159
+ | Average Line 1 Similarity | 75-90% |
160
+ | Average Line 2 Similarity | 75-90% |
161
+ | Strict Both Lines Match | 60-75% |
162
+ | Inference Speed | ~0.5-1.0s/address |
163
+
164
+ ### Test Examples
165
+
166
+ ### Chinese Address
167
+ Input:
168
+ ```text
169
+ 九龍觀塘區雲漢街61號南寧大樓地庫01舖
170
+ ```
171
+
172
+ Output:
173
+ ```text
174
+ Line 1: 南寧大樓地庫01舖
175
+ Line 2: 九龍觀塘區雲漢街61號
176
+ ```
177
+
178
+ ### English Address
179
+ Input:
180
+ ```text
181
+ ROOM 2107, 42/F, WINNING HEIGHTS, 277 CASTLE PEAK ROAD, TSUEN WAN, NEW TERRITORIES
182
+ ```
183
+
184
+ Output:
185
+ ```text
186
+ Line 1: FLAT 2107, 42/F, WINNING HEIGHTS
187
+ Line 2: 277 CASTLE PEAK ROAD, TSUEN WAN, NEW TERRITORIES
188
+ ```
189
+
190
+ ---
191
+ ## Limitations & Known Issues
192
+ - **Geographic Scope**: Primarily optimized for Hong Kong addresses. Performance may degrade on addresses from other regions.
193
+ - **Format Variations**: The model may struggle with highly unconventional or incomplete addresses.
194
+ - **Language Mixing**: While it handles both Chinese and English, addresses with heavy code-switching may produce less consistent results.
195
+ - **Hardware**: 4-bit quantization is recommended for GPUs with <16GB VRAM. For older GPUs (e.g., Tesla P40/Pascal), the inference script automatically disables 4-bit and uses fp16 to avoid compatibility issues.
196
+
197
+ ---
198
+ ## Usage Recommendations
199
+
200
+ ### Hardware Requirements
201
+ | Mode | VRAM (Approx.) |
202
+ |--------|-----------------|
203
+ | Training (4-bit) | 12-14 GB |
204
+ | Inference (4-bit) | 6-8 GB |
205
+ | Inference (fp16) | 10-12 GB |
206
+
207
+ ### Environment
208
+ ```bash
209
+ # Install dependencies
210
+ pip install torch transformers accelerate peft bitsandbytes
211
+ ```
212
+
213
+ ### Citation
214
+ If you use this model in your research or project, please consider citing:
215
+ ```bbibtex
216
+ @misc{ymlee13_2024_address_parser,
217
+ author = {Lee, Y.M.},
218
+ title = {Qwen2.5-3B-Instruct Address Formatter},
219
+ year = {2024},
220
+ publisher = {Hugging Face},
221
+ howpublished = {\url{https://huggingface.co/ymlee13/Qwen2.5-3B-Instruct_Address_Formatter}}
222
+ }
223
+ ```
224
+
225
+ ## Acknowledgements
226
+ - Qwen Team for the excellent base model.
227
+ - Hugging Face for the transformers and peft libraries.
228
+ - Hong Kong Government for providing the ALS-GeoJSON dataset.
229
+ - Open Source Community for tools like trl, bitsandbytes, and accelerate.
230
+
231
+ ## Contact & Support
232
+ For questions, issues, or collaborations:
233
+ - Author: ymlee13
234
+ - GitHub: ymlee13/address_parsing
235
+ - Hugging Face: ymlee13