Skip to content

Repository files navigation

Hong Kong Address Parser

GitHub Hugging Face

πŸ“ Overview

A fine-tuned Qwen2.5-3B-Instruct model for Hong Kong address parsing and formatting. The model splits unstructured address text into two structured lines following Hong Kong addressing conventions.

Features

  • 🏠 Address Parsing: Splits addresses into Line 1 (specific details) and Line 2 (general location)
  • πŸ‡­πŸ‡° Hong Kong Focus: Specialized for Hong Kong address formats (both English and Chinese)
  • πŸ”„ Multi-language Support: Handles English, Traditional Chinese, and Simplified Chinese
  • 🎯 High Accuracy: Fine-tuned on real Hong Kong address data
  • ⚑ Efficient: 4-bit quantization support for memory-efficient inference

πŸ“Š Performance Metrics

Metric Score (Typical)
Average Line 1 Similarity 75-90%
Average Line 2 Similarity 75-90%
Strict Both Lines Match 60-75%
Inference Speed ~0.5-1.0s/address

Prerequisites

  • Python 3.9+
  • CUDA-compatible GPU (24GB+ VRAM recommended)
  • Git LFS (for Hugging Face uploads)

Setup

  1. Clone the repository
git clone https://github.com/ymlee13/address_parsing.git
cd address_parsing
  1. Install dependencies
pip install -r requirements.txt
  1. Download the base model (optional if you just want to test)
git lfs install
git clone https://huggingface.co/Qwen/Qwen2.5-3B-Instruct

πŸ“¦ Model Weights

The fine-tuned LoRA adapter is available on Hugging Face Hub: ymlee13/Qwen2.5-3B-Instruct_Address_Formatter

The parser will automatically download the base model (Qwen/Qwen2.5-3B-Instruct) and the adapter from Hugging Face when you run the code for the first time.

πŸš€ Quick Start

Training the Model

  1. Prepare your data
python -c "from jsonl_converter import parse_database_file_to_nested_jsonl; parse_database_file_to_nested_jsonl('./data/database.txt', './data/database.jsonl')"
  1. Run training
jupyter notebook train_model.ipynb

Testing the Model

jupyter notebook test_model.ipynb

Using the Parser

from llm_parser import HKAddressParserLLM

# Initialize parser
parser = HKAddressParserLLM(
    base_model_path="Qwen/Qwen2.5-3B-Instruct",
    lora_path="ymlee13/Qwen2.5-3B-Instruct_Address_Formatter",
    conf_threshold=0.50,
    max_new_tokens=128
)

# Parse a single address
address = "δΉιΎθ§€ε‘˜ε€ι›²ζΌ’θ‘—61θ™Ÿε—ε―§ε€§ζ¨“εœ°εΊ«01θˆ–"
result = parser.parse(address)
print(f"Line 1: {result[1]}")
print(f"Line 2: {result[2]}")

# Batch parsing
addresses = [
    ("δΉιΎθ§€ε‘˜ε€ι›²ζΌ’θ‘—61θ™Ÿε—ε―§ε€§ζ¨“εœ°εΊ«01θˆ–", ""),
    ("ROOM 2107, 42/F, WINNING HEIGHTS, TSUEN WAN", ""),
]
results = parser.parse_batch(addresses, batch_size=2)

🎯 Demo

Try it yourself:

from llm_parser import HKAddressParserLLM

parser = HKAddressParserLLM(
    base_model_path="Qwen/Qwen2.5-3B-Instruct",
    lora_path="ymlee13/Qwen2.5-3B-Instruct_Address_Formatter"
)

test_cases = [
    "δΉιΎθ§€ε‘˜ε€ι›²ζΌ’θ‘—61θ™Ÿε—ε―§ε€§ζ¨“εœ°εΊ«01θˆ–",
    "RM 2107, 42/F, WINNING HEIGHTS, 277 CASTLE PEAK RD, TSUEN WAN"
]

for addr in test_cases:
    result = parser.parse(addr)
    print(f"Input: {addr}")
    print(f"Line 1: {result[1]}")
    print(f"Line 2: {result[2]}")
    print("-" * 50)

πŸ“ Project Structure

address_parsing/
β”œβ”€β”€ README.md                 # This file
β”œβ”€β”€ requirements.txt          # Python dependencies
β”œβ”€β”€ .gitignore               # Git ignore file
β”œβ”€β”€ train_model.ipynb        # Fine-tuning notebook
β”œβ”€β”€ test_model.ipynb         # Testing and evaluation notebook
β”œβ”€β”€ llm_parser.ipynb         # Inference wrapper notebook
β”œβ”€β”€ jsonl_converter.ipynb    # Data conversion utility
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ database.txt         # Training data (input|line1|line2)
β”‚   β”œβ”€β”€ database.jsonl       # Converted training data
β”‚   β”œβ”€β”€ test_data/
β”‚   β”‚   └── test_data.txt    # Test data examples
β”‚   └── gen_real_address.ipynb # Generate synthetic addresses
└── models/                  # Model storage (not in repo)
    β”œβ”€β”€ Qwen2.5-3B-Instruct/ # Base model
    └── Qwen2.5-3B-Instruct_Address_Formatter/ # Fine-tuned adapters

##πŸ”§ Training (Optional) If you want to retrain the model:

1. Prepare your data

# Convert txt to jsonl format
python -c "from jsonl_converter import parse_database_file_to_nested_jsonl; parse_database_file_to_nested_jsonl('./data/database.txt', './data/database.jsonl')"

2. Run training

jupyter notebook train_model.ipynb

3. Evaluate the model

jupyter notebook test_model.ipynb

Training Configuration

  • Base Model: Qwen/Qwen2.5-3B-Instruct
  • Fine-tuning Method: LoRA (Low-Rank Adaptation)
  • Quantization: 4-bit (NF4)
  • Learning Rate: 2e-5
  • Epochs: 3
  • Batch Size: 4 (per device)
  • Gradient Accumulation: 2
  • LoRA Rank: 8
  • LoRA Alpha: 16
  • Target Modules: q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj

Post-processing Features

  • Character pooling and reconstruction
  • Intelligent omission detection and re-insertion
  • Language detection (Chinese/English)
  • Traditional Chinese conversion
  • English spell checking

##πŸ“Š Dataset The model was trained on a dataset of 570+ Hong Kong addresses generated from real geospatial data (ALS-GeoJSON from HK government data portal). Each address is split into:

  • Line 1: Specific location (floor, unit, building)
  • Line 2: General location (street, district, region) Data format:
[original input] | [line 1] | [line 2]

##πŸ€– Inference Examples

Chinese Address

Input:

δΉιΎθ§€ε‘˜ε€ι›²ζΌ’θ‘—61θ™Ÿε—ε―§ε€§ζ¨“εœ°εΊ«01θˆ–

Output:

Line 1: ε—ε―§ε€§ζ¨“εœ°εΊ«01θˆ–
Line 2: δΉιΎθ§€ε‘˜ε€ι›²ζΌ’θ‘—61θ™Ÿ

English Address

Input:

ROOM 2107, 42/F, WINNING HEIGHTS, 277 CASTLE PEAK ROAD, TSUEN WAN, NEW TERRITORIES

Output:

Line 1: FLAT 2107, 42/F, WINNING HEIGHTS
Line 2: 277 CASTLE PEAK ROAD, TSUEN WAN, NEW TERRITORIES

πŸ“ˆ Performance Optimization

For Tesla P40 / Pascal GPUs

The parser includes special handling for older GPUs:

  • Disables Flash Attention
  • Uses fp16 instead of 4-bit quantization when needed
  • Memory-efficient batching

Memory Usage

  • Training: ~12-14GB VRAM
  • Inference: ~6-8GB VRAM

πŸ“š References

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • Qwen for the base model
  • Hugging Face for the transformers library
  • Hong Kong Government for the ALS-GeoJSON dataset

πŸ“§ Contact

  • Author: ymlee13
  • Hugging Face: @ymlee13
  • GitHub: ymlee13

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages