india-address-parser 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- india_address_parser-0.1.0/LICENSE +202 -0
- india_address_parser-0.1.0/PKG-INFO +94 -0
- india_address_parser-0.1.0/README.md +74 -0
- india_address_parser-0.1.0/india_address_parser/__init__.py +4 -0
- india_address_parser-0.1.0/india_address_parser/cli.py +26 -0
- india_address_parser-0.1.0/india_address_parser/parser.py +76 -0
- india_address_parser-0.1.0/india_address_parser.egg-info/PKG-INFO +94 -0
- india_address_parser-0.1.0/india_address_parser.egg-info/SOURCES.txt +13 -0
- india_address_parser-0.1.0/india_address_parser.egg-info/dependency_links.txt +1 -0
- india_address_parser-0.1.0/india_address_parser.egg-info/entry_points.txt +2 -0
- india_address_parser-0.1.0/india_address_parser.egg-info/requires.txt +5 -0
- india_address_parser-0.1.0/india_address_parser.egg-info/top_level.txt +1 -0
- india_address_parser-0.1.0/pyproject.toml +32 -0
- india_address_parser-0.1.0/setup.cfg +4 -0
- india_address_parser-0.1.0/tests/test_parser.py +69 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: india-address-parser
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Parse messy Indian addresses into structured JSON with a small local model (GGUF).
|
|
5
|
+
License-Expression: Apache-2.0
|
|
6
|
+
Project-URL: Model, https://huggingface.co/abr-6024/indian-address-parser-1.5B
|
|
7
|
+
Project-URL: GGUF, https://huggingface.co/abr-6024/indian-address-parser-1.5B-GGUF
|
|
8
|
+
Project-URL: Dataset, https://huggingface.co/datasets/abr-6024/indian-address-parsing-synthetic
|
|
9
|
+
Keywords: address,india,parser,gguf,llama.cpp,nlp
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
12
|
+
Requires-Python: >=3.9
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: llama-cpp-python>=0.3.0
|
|
16
|
+
Requires-Dist: huggingface_hub>=0.23
|
|
17
|
+
Provides-Extra: test
|
|
18
|
+
Requires-Dist: pytest; extra == "test"
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
# india-address-parser
|
|
22
|
+
|
|
23
|
+
Parse messy Indian addresses into structured fields with a small local model (1.5B, runs on CPU, no API key).
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
from india_address_parser import parse
|
|
27
|
+
|
|
28
|
+
parse("D.No 14/2, Gandhi Road, Pollachi, Coimbatore - 642001")
|
|
29
|
+
# {'flat': '14/2', 'building': '', 'street': 'Gandhi Road', 'landmark': '', 'area': 'Pollachi',
|
|
30
|
+
# 'city': 'Coimbatore', 'state': 'Tamil Nadu', 'pincode': '642001', 'phone': ''}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
The model is [abr-6024/indian-address-parser-1.5B](https://huggingface.co/abr-6024/indian-address-parser-1.5B),
|
|
34
|
+
a fine-tune of Qwen2.5-1.5B-Instruct, run through [llama.cpp](https://github.com/ggml-org/llama.cpp) as a
|
|
35
|
+
[GGUF file](https://huggingface.co/abr-6024/indian-address-parser-1.5B-GGUF).
|
|
36
|
+
|
|
37
|
+
## Install
|
|
38
|
+
|
|
39
|
+
```
|
|
40
|
+
pip install india-address-parser
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The first call downloads the model (Q4_K_M, about 1 GB) from Hugging Face and caches it. After that it runs offline.
|
|
44
|
+
`llama-cpp-python` is installed as a dependency. If pip has to compile it and fails, install a prebuilt CPU wheel first:
|
|
45
|
+
|
|
46
|
+
```
|
|
47
|
+
pip install llama-cpp-python --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cpu
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Use
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
from india_address_parser import AddressParser
|
|
54
|
+
|
|
55
|
+
p = AddressParser() # loads the model once
|
|
56
|
+
p.parse("Flat 3B, Sri Murugan Apartments, Peelamedu, Coimbatore 641004, 9442012345")
|
|
57
|
+
p.parse_many(["12, Mettu Street, Karaikudi, Sivaganga District 630001", "..."])
|
|
58
|
+
|
|
59
|
+
AddressParser(model_path="my.gguf") # use a local GGUF file instead of downloading
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Command line:
|
|
63
|
+
|
|
64
|
+
```
|
|
65
|
+
india-address-parser "No 21, East Car Street, Srirangam, Tiruchirappalli 620006"
|
|
66
|
+
cat addresses.txt | india-address-parser # one address per line, one JSON object per line out
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Every result has exactly these keys: `flat, building, street, landmark, area, city, state, pincode, phone`.
|
|
70
|
+
Anything not written in the address is an empty string.
|
|
71
|
+
|
|
72
|
+
## Conventions
|
|
73
|
+
|
|
74
|
+
- `city` is the **district** name (or the metro name for big cities), not necessarily the town written in the address.
|
|
75
|
+
- `area` is the locality, village or post-office name. `flat` is the door or house number only.
|
|
76
|
+
- The model does not guess a city that is not written. It fills the state when the city or pincode implies it.
|
|
77
|
+
|
|
78
|
+
## Accuracy and limits
|
|
79
|
+
|
|
80
|
+
The default file (Q4_K_M) scored 94.0% full-record exact match on 200 random synthetic test addresses, 29 of 30 on a
|
|
81
|
+
hand-written Tamil Nadu set and 28 of 36 on a hand-written mixed-India set. The original unquantized model scored
|
|
82
|
+
94.4% on all 800 synthetic test addresses. The hand-written sets were written by the model's author, so they are not
|
|
83
|
+
independent benchmarks. Details: [GGUF model card](https://huggingface.co/abr-6024/indian-address-parser-1.5B-GGUF).
|
|
84
|
+
|
|
85
|
+
- The model was trained on synthetic address text over real India Post geography. **Accuracy on real customer
|
|
86
|
+
addresses has not been measured** and is likely lower. Test it on your own data before relying on it.
|
|
87
|
+
- Bare-number addresses such as `15 Gandhi Salai, Tirupur` can lose the house number.
|
|
88
|
+
- Pincodes are extracted, not validated against the locality. English / Latin script only.
|
|
89
|
+
- `parse` raises `ValueError` if the model does not return a JSON object. Handle that in production code.
|
|
90
|
+
|
|
91
|
+
## Credits and license
|
|
92
|
+
|
|
93
|
+
Code: Apache-2.0. Base model: Qwen2.5-1.5B-Instruct (Apache-2.0). Training geography: All India Pincode Directory,
|
|
94
|
+
Department of Posts, Government of India (data.gov.in, GODL-India).
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# india-address-parser
|
|
2
|
+
|
|
3
|
+
Parse messy Indian addresses into structured fields with a small local model (1.5B, runs on CPU, no API key).
|
|
4
|
+
|
|
5
|
+
```python
|
|
6
|
+
from india_address_parser import parse
|
|
7
|
+
|
|
8
|
+
parse("D.No 14/2, Gandhi Road, Pollachi, Coimbatore - 642001")
|
|
9
|
+
# {'flat': '14/2', 'building': '', 'street': 'Gandhi Road', 'landmark': '', 'area': 'Pollachi',
|
|
10
|
+
# 'city': 'Coimbatore', 'state': 'Tamil Nadu', 'pincode': '642001', 'phone': ''}
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
The model is [abr-6024/indian-address-parser-1.5B](https://huggingface.co/abr-6024/indian-address-parser-1.5B),
|
|
14
|
+
a fine-tune of Qwen2.5-1.5B-Instruct, run through [llama.cpp](https://github.com/ggml-org/llama.cpp) as a
|
|
15
|
+
[GGUF file](https://huggingface.co/abr-6024/indian-address-parser-1.5B-GGUF).
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
pip install india-address-parser
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
The first call downloads the model (Q4_K_M, about 1 GB) from Hugging Face and caches it. After that it runs offline.
|
|
24
|
+
`llama-cpp-python` is installed as a dependency. If pip has to compile it and fails, install a prebuilt CPU wheel first:
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
pip install llama-cpp-python --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cpu
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Use
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
from india_address_parser import AddressParser
|
|
34
|
+
|
|
35
|
+
p = AddressParser() # loads the model once
|
|
36
|
+
p.parse("Flat 3B, Sri Murugan Apartments, Peelamedu, Coimbatore 641004, 9442012345")
|
|
37
|
+
p.parse_many(["12, Mettu Street, Karaikudi, Sivaganga District 630001", "..."])
|
|
38
|
+
|
|
39
|
+
AddressParser(model_path="my.gguf") # use a local GGUF file instead of downloading
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Command line:
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
india-address-parser "No 21, East Car Street, Srirangam, Tiruchirappalli 620006"
|
|
46
|
+
cat addresses.txt | india-address-parser # one address per line, one JSON object per line out
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Every result has exactly these keys: `flat, building, street, landmark, area, city, state, pincode, phone`.
|
|
50
|
+
Anything not written in the address is an empty string.
|
|
51
|
+
|
|
52
|
+
## Conventions
|
|
53
|
+
|
|
54
|
+
- `city` is the **district** name (or the metro name for big cities), not necessarily the town written in the address.
|
|
55
|
+
- `area` is the locality, village or post-office name. `flat` is the door or house number only.
|
|
56
|
+
- The model does not guess a city that is not written. It fills the state when the city or pincode implies it.
|
|
57
|
+
|
|
58
|
+
## Accuracy and limits
|
|
59
|
+
|
|
60
|
+
The default file (Q4_K_M) scored 94.0% full-record exact match on 200 random synthetic test addresses, 29 of 30 on a
|
|
61
|
+
hand-written Tamil Nadu set and 28 of 36 on a hand-written mixed-India set. The original unquantized model scored
|
|
62
|
+
94.4% on all 800 synthetic test addresses. The hand-written sets were written by the model's author, so they are not
|
|
63
|
+
independent benchmarks. Details: [GGUF model card](https://huggingface.co/abr-6024/indian-address-parser-1.5B-GGUF).
|
|
64
|
+
|
|
65
|
+
- The model was trained on synthetic address text over real India Post geography. **Accuracy on real customer
|
|
66
|
+
addresses has not been measured** and is likely lower. Test it on your own data before relying on it.
|
|
67
|
+
- Bare-number addresses such as `15 Gandhi Salai, Tirupur` can lose the house number.
|
|
68
|
+
- Pincodes are extracted, not validated against the locality. English / Latin script only.
|
|
69
|
+
- `parse` raises `ValueError` if the model does not return a JSON object. Handle that in production code.
|
|
70
|
+
|
|
71
|
+
## Credits and license
|
|
72
|
+
|
|
73
|
+
Code: Apache-2.0. Base model: Qwen2.5-1.5B-Instruct (Apache-2.0). Training geography: All India Pincode Directory,
|
|
74
|
+
Department of Posts, Government of India (data.gov.in, GODL-India).
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import json
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
from . import __version__
|
|
6
|
+
from .parser import AddressParser
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def main(argv=None):
|
|
10
|
+
ap = argparse.ArgumentParser(prog="india-address-parser",
|
|
11
|
+
description="Parse Indian addresses into JSON with a local model.")
|
|
12
|
+
ap.add_argument("address", nargs="*", help="address text; if omitted, one address per line is read from stdin")
|
|
13
|
+
ap.add_argument("--model", help="path to a local .gguf file (default: download Q4_K_M from Hugging Face)")
|
|
14
|
+
ap.add_argument("--version", action="version", version=__version__)
|
|
15
|
+
args = ap.parse_args(argv)
|
|
16
|
+
|
|
17
|
+
addresses = [" ".join(args.address)] if args.address else [l.strip() for l in sys.stdin if l.strip()]
|
|
18
|
+
if not addresses:
|
|
19
|
+
ap.error("no address given")
|
|
20
|
+
parser = AddressParser(model_path=args.model)
|
|
21
|
+
for a in addresses:
|
|
22
|
+
print(json.dumps(parser.parse(a), ensure_ascii=False))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
if __name__ == "__main__":
|
|
26
|
+
main()
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""Parse messy Indian addresses into structured fields with a local GGUF model."""
|
|
2
|
+
import json
|
|
3
|
+
from typing import Dict, Iterable, List, Optional
|
|
4
|
+
|
|
5
|
+
FIELDS = ["flat", "building", "street", "landmark", "area", "city", "state", "pincode", "phone"]
|
|
6
|
+
|
|
7
|
+
# Must match the system prompt the model was trained with.
|
|
8
|
+
SYSTEM = ("You are an Indian address parser. Convert the address into JSON with exactly these keys: "
|
|
9
|
+
"flat, building, street, landmark, area, city, state, pincode, phone. "
|
|
10
|
+
"Use an empty string for anything missing. Output only JSON.")
|
|
11
|
+
|
|
12
|
+
DEFAULT_REPO = "abr-6024/indian-address-parser-1.5B-GGUF"
|
|
13
|
+
DEFAULT_FILE = "*Q4_K_M.gguf"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _clean(raw: str) -> Dict[str, str]:
|
|
17
|
+
"""Turn the model's text into a dict with exactly the nine expected string fields."""
|
|
18
|
+
try:
|
|
19
|
+
data = json.loads(raw)
|
|
20
|
+
except json.JSONDecodeError as exc:
|
|
21
|
+
raise ValueError(f"model did not return valid JSON: {raw!r}") from exc
|
|
22
|
+
if not isinstance(data, dict):
|
|
23
|
+
raise ValueError(f"model returned JSON that is not an object: {raw!r}")
|
|
24
|
+
return {k: "" if data.get(k) is None else str(data[k]) for k in FIELDS}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class AddressParser:
|
|
28
|
+
"""Loads the model once; call :meth:`parse` as many times as you like.
|
|
29
|
+
|
|
30
|
+
By default the Q4_K_M file (about 1 GB) is downloaded from Hugging Face on first use and cached.
|
|
31
|
+
Pass ``model_path`` to use a local .gguf file instead.
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
def __init__(self, model_path: Optional[str] = None, repo_id: str = DEFAULT_REPO,
|
|
35
|
+
filename: str = DEFAULT_FILE, n_ctx: int = 1024, n_threads: Optional[int] = None,
|
|
36
|
+
verbose: bool = False):
|
|
37
|
+
try:
|
|
38
|
+
from llama_cpp import Llama
|
|
39
|
+
except ImportError as exc: # pragma: no cover
|
|
40
|
+
raise ImportError("llama-cpp-python is required: pip install llama-cpp-python") from exc
|
|
41
|
+
kwargs = dict(n_ctx=n_ctx, verbose=verbose)
|
|
42
|
+
if n_threads:
|
|
43
|
+
kwargs["n_threads"] = n_threads
|
|
44
|
+
if model_path:
|
|
45
|
+
self._llm = Llama(model_path=model_path, **kwargs)
|
|
46
|
+
else:
|
|
47
|
+
self._llm = Llama.from_pretrained(repo_id=repo_id, filename=filename, **kwargs)
|
|
48
|
+
|
|
49
|
+
def parse(self, address: str) -> Dict[str, str]:
|
|
50
|
+
"""Return a dict with keys flat, building, street, landmark, area, city, state, pincode, phone.
|
|
51
|
+
|
|
52
|
+
Fields not written in the address are empty strings. ``city`` is the district (or metro) name.
|
|
53
|
+
Raises ValueError if the model output is not a JSON object.
|
|
54
|
+
"""
|
|
55
|
+
if not address or not address.strip():
|
|
56
|
+
raise ValueError("address is empty")
|
|
57
|
+
out = self._llm.create_chat_completion(
|
|
58
|
+
messages=[{"role": "system", "content": SYSTEM}, {"role": "user", "content": address.strip()}],
|
|
59
|
+
temperature=0.0,
|
|
60
|
+
max_tokens=200,
|
|
61
|
+
)
|
|
62
|
+
return _clean(out["choices"][0]["message"]["content"])
|
|
63
|
+
|
|
64
|
+
def parse_many(self, addresses: Iterable[str]) -> List[Dict[str, str]]:
|
|
65
|
+
return [self.parse(a) for a in addresses]
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
_default: Optional[AddressParser] = None
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def parse(address: str) -> Dict[str, str]:
|
|
72
|
+
"""Parse one address with a shared default model (downloaded and loaded on the first call)."""
|
|
73
|
+
global _default
|
|
74
|
+
if _default is None:
|
|
75
|
+
_default = AddressParser()
|
|
76
|
+
return _default.parse(address)
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: india-address-parser
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Parse messy Indian addresses into structured JSON with a small local model (GGUF).
|
|
5
|
+
License-Expression: Apache-2.0
|
|
6
|
+
Project-URL: Model, https://huggingface.co/abr-6024/indian-address-parser-1.5B
|
|
7
|
+
Project-URL: GGUF, https://huggingface.co/abr-6024/indian-address-parser-1.5B-GGUF
|
|
8
|
+
Project-URL: Dataset, https://huggingface.co/datasets/abr-6024/indian-address-parsing-synthetic
|
|
9
|
+
Keywords: address,india,parser,gguf,llama.cpp,nlp
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
12
|
+
Requires-Python: >=3.9
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: llama-cpp-python>=0.3.0
|
|
16
|
+
Requires-Dist: huggingface_hub>=0.23
|
|
17
|
+
Provides-Extra: test
|
|
18
|
+
Requires-Dist: pytest; extra == "test"
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
# india-address-parser
|
|
22
|
+
|
|
23
|
+
Parse messy Indian addresses into structured fields with a small local model (1.5B, runs on CPU, no API key).
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
from india_address_parser import parse
|
|
27
|
+
|
|
28
|
+
parse("D.No 14/2, Gandhi Road, Pollachi, Coimbatore - 642001")
|
|
29
|
+
# {'flat': '14/2', 'building': '', 'street': 'Gandhi Road', 'landmark': '', 'area': 'Pollachi',
|
|
30
|
+
# 'city': 'Coimbatore', 'state': 'Tamil Nadu', 'pincode': '642001', 'phone': ''}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
The model is [abr-6024/indian-address-parser-1.5B](https://huggingface.co/abr-6024/indian-address-parser-1.5B),
|
|
34
|
+
a fine-tune of Qwen2.5-1.5B-Instruct, run through [llama.cpp](https://github.com/ggml-org/llama.cpp) as a
|
|
35
|
+
[GGUF file](https://huggingface.co/abr-6024/indian-address-parser-1.5B-GGUF).
|
|
36
|
+
|
|
37
|
+
## Install
|
|
38
|
+
|
|
39
|
+
```
|
|
40
|
+
pip install india-address-parser
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The first call downloads the model (Q4_K_M, about 1 GB) from Hugging Face and caches it. After that it runs offline.
|
|
44
|
+
`llama-cpp-python` is installed as a dependency. If pip has to compile it and fails, install a prebuilt CPU wheel first:
|
|
45
|
+
|
|
46
|
+
```
|
|
47
|
+
pip install llama-cpp-python --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cpu
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Use
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
from india_address_parser import AddressParser
|
|
54
|
+
|
|
55
|
+
p = AddressParser() # loads the model once
|
|
56
|
+
p.parse("Flat 3B, Sri Murugan Apartments, Peelamedu, Coimbatore 641004, 9442012345")
|
|
57
|
+
p.parse_many(["12, Mettu Street, Karaikudi, Sivaganga District 630001", "..."])
|
|
58
|
+
|
|
59
|
+
AddressParser(model_path="my.gguf") # use a local GGUF file instead of downloading
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Command line:
|
|
63
|
+
|
|
64
|
+
```
|
|
65
|
+
india-address-parser "No 21, East Car Street, Srirangam, Tiruchirappalli 620006"
|
|
66
|
+
cat addresses.txt | india-address-parser # one address per line, one JSON object per line out
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Every result has exactly these keys: `flat, building, street, landmark, area, city, state, pincode, phone`.
|
|
70
|
+
Anything not written in the address is an empty string.
|
|
71
|
+
|
|
72
|
+
## Conventions
|
|
73
|
+
|
|
74
|
+
- `city` is the **district** name (or the metro name for big cities), not necessarily the town written in the address.
|
|
75
|
+
- `area` is the locality, village or post-office name. `flat` is the door or house number only.
|
|
76
|
+
- The model does not guess a city that is not written. It fills the state when the city or pincode implies it.
|
|
77
|
+
|
|
78
|
+
## Accuracy and limits
|
|
79
|
+
|
|
80
|
+
The default file (Q4_K_M) scored 94.0% full-record exact match on 200 random synthetic test addresses, 29 of 30 on a
|
|
81
|
+
hand-written Tamil Nadu set and 28 of 36 on a hand-written mixed-India set. The original unquantized model scored
|
|
82
|
+
94.4% on all 800 synthetic test addresses. The hand-written sets were written by the model's author, so they are not
|
|
83
|
+
independent benchmarks. Details: [GGUF model card](https://huggingface.co/abr-6024/indian-address-parser-1.5B-GGUF).
|
|
84
|
+
|
|
85
|
+
- The model was trained on synthetic address text over real India Post geography. **Accuracy on real customer
|
|
86
|
+
addresses has not been measured** and is likely lower. Test it on your own data before relying on it.
|
|
87
|
+
- Bare-number addresses such as `15 Gandhi Salai, Tirupur` can lose the house number.
|
|
88
|
+
- Pincodes are extracted, not validated against the locality. English / Latin script only.
|
|
89
|
+
- `parse` raises `ValueError` if the model does not return a JSON object. Handle that in production code.
|
|
90
|
+
|
|
91
|
+
## Credits and license
|
|
92
|
+
|
|
93
|
+
Code: Apache-2.0. Base model: Qwen2.5-1.5B-Instruct (Apache-2.0). Training geography: All India Pincode Directory,
|
|
94
|
+
Department of Posts, Government of India (data.gov.in, GODL-India).
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
india_address_parser/__init__.py
|
|
5
|
+
india_address_parser/cli.py
|
|
6
|
+
india_address_parser/parser.py
|
|
7
|
+
india_address_parser.egg-info/PKG-INFO
|
|
8
|
+
india_address_parser.egg-info/SOURCES.txt
|
|
9
|
+
india_address_parser.egg-info/dependency_links.txt
|
|
10
|
+
india_address_parser.egg-info/entry_points.txt
|
|
11
|
+
india_address_parser.egg-info/requires.txt
|
|
12
|
+
india_address_parser.egg-info/top_level.txt
|
|
13
|
+
tests/test_parser.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
india_address_parser
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "india-address-parser"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Parse messy Indian addresses into structured JSON with a small local model (GGUF)."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = "Apache-2.0"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
keywords = ["address", "india", "parser", "gguf", "llama.cpp", "nlp"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"Topic :: Text Processing :: Linguistic",
|
|
17
|
+
]
|
|
18
|
+
dependencies = ["llama-cpp-python>=0.3.0", "huggingface_hub>=0.23"]
|
|
19
|
+
|
|
20
|
+
[project.optional-dependencies]
|
|
21
|
+
test = ["pytest"]
|
|
22
|
+
|
|
23
|
+
[project.urls]
|
|
24
|
+
Model = "https://huggingface.co/abr-6024/indian-address-parser-1.5B"
|
|
25
|
+
GGUF = "https://huggingface.co/abr-6024/indian-address-parser-1.5B-GGUF"
|
|
26
|
+
Dataset = "https://huggingface.co/datasets/abr-6024/indian-address-parsing-synthetic"
|
|
27
|
+
|
|
28
|
+
[project.scripts]
|
|
29
|
+
india-address-parser = "india_address_parser.cli:main"
|
|
30
|
+
|
|
31
|
+
[tool.setuptools]
|
|
32
|
+
packages = ["india_address_parser"]
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import sys
|
|
3
|
+
import types
|
|
4
|
+
|
|
5
|
+
import pytest
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class FakeLlama:
|
|
9
|
+
reply = json.dumps({"flat": "14/2", "street": "Gandhi Road", "area": "Pollachi", "city": "Coimbatore",
|
|
10
|
+
"state": "Tamil Nadu", "pincode": "642001", "extra": "dropped", "phone": None})
|
|
11
|
+
last_kwargs = None
|
|
12
|
+
|
|
13
|
+
def __init__(self, **kwargs):
|
|
14
|
+
FakeLlama.last_kwargs = kwargs
|
|
15
|
+
|
|
16
|
+
@classmethod
|
|
17
|
+
def from_pretrained(cls, repo_id, filename, **kwargs):
|
|
18
|
+
inst = cls()
|
|
19
|
+
cls.last_kwargs = {"repo_id": repo_id, "filename": filename, **kwargs}
|
|
20
|
+
return inst
|
|
21
|
+
|
|
22
|
+
def create_chat_completion(self, messages, temperature, max_tokens):
|
|
23
|
+
FakeLlama.messages = messages
|
|
24
|
+
return {"choices": [{"message": {"content": FakeLlama.reply}}]}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@pytest.fixture(autouse=True)
|
|
28
|
+
def fake_llama(monkeypatch):
|
|
29
|
+
mod = types.ModuleType("llama_cpp")
|
|
30
|
+
mod.Llama = FakeLlama
|
|
31
|
+
monkeypatch.setitem(sys.modules, "llama_cpp", mod)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def test_parse_returns_exactly_nine_string_fields():
|
|
35
|
+
from india_address_parser import FIELDS, AddressParser
|
|
36
|
+
out = AddressParser(model_path="x.gguf").parse("D.No 14/2, Gandhi Road, Pollachi, Coimbatore - 642001")
|
|
37
|
+
assert list(out) == FIELDS
|
|
38
|
+
assert out["flat"] == "14/2" and out["city"] == "Coimbatore" and out["phone"] == "" and out["building"] == ""
|
|
39
|
+
assert "extra" not in out
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def test_uses_training_system_prompt():
|
|
43
|
+
from india_address_parser import SYSTEM, AddressParser
|
|
44
|
+
AddressParser(model_path="x.gguf").parse(" anything ")
|
|
45
|
+
assert FakeLlama.messages[0] == {"role": "system", "content": SYSTEM}
|
|
46
|
+
assert FakeLlama.messages[1]["content"] == "anything"
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def test_default_downloads_q4_k_m_from_hub():
|
|
50
|
+
from india_address_parser import AddressParser
|
|
51
|
+
AddressParser()
|
|
52
|
+
assert FakeLlama.last_kwargs["repo_id"] == "abr-6024/indian-address-parser-1.5B-GGUF"
|
|
53
|
+
assert FakeLlama.last_kwargs["filename"] == "*Q4_K_M.gguf"
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def test_invalid_model_output_raises():
|
|
57
|
+
from india_address_parser import AddressParser
|
|
58
|
+
FakeLlama.reply = "not json"
|
|
59
|
+
with pytest.raises(ValueError):
|
|
60
|
+
AddressParser(model_path="x.gguf").parse("addr")
|
|
61
|
+
FakeLlama.reply = "[1, 2]"
|
|
62
|
+
with pytest.raises(ValueError):
|
|
63
|
+
AddressParser(model_path="x.gguf").parse("addr")
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def test_empty_address_raises():
|
|
67
|
+
from india_address_parser import AddressParser
|
|
68
|
+
with pytest.raises(ValueError):
|
|
69
|
+
AddressParser(model_path="x.gguf").parse(" ")
|