|
| 1 | +"""Repackage en_core_web_trf to work with spacy-transformers 1.4+ and spacy 3.8+. |
| 2 | +
|
| 3 | +Loads the installed en_core_web_trf model, converts serialized weights from |
| 4 | +torch pickle to safetensors format, and writes a new spaCy model package |
| 5 | +named en_core_web_hftrf. |
| 6 | +""" |
| 7 | +import json |
| 8 | +import shutil |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +import spacy |
| 12 | +from spacy.util import get_package_path |
| 13 | + |
| 14 | +SRC_VERSION = "3.6.1" |
| 15 | +NEW_VERSION = "3.8.1" |
| 16 | +PKG_NAME = "en_core_web_hftrf" |
| 17 | +META_NAME = "core_web_hftrf" |
| 18 | +SPACY_COMPAT = ">=3.8.0,<3.9.0" |
| 19 | +SPACY_TRF_COMPAT = ">=1.4.0,<1.5.0" |
| 20 | + |
| 21 | +src_path = get_package_path("en_core_web_trf") |
| 22 | +model_dir = src_path / f"en_core_web_trf-{SRC_VERSION}" |
| 23 | + |
| 24 | +# Layout: out_root/setup.cfg, out_root/en_core_web_hftrf/__init__.py, etc. |
| 25 | +out_root = Path("dist") / f"{PKG_NAME}-{NEW_VERSION}" |
| 26 | +pkg_dir = out_root / PKG_NAME |
| 27 | +data_dir = pkg_dir / f"{PKG_NAME}-{NEW_VERSION}" |
| 28 | + |
| 29 | +if out_root.exists(): |
| 30 | + shutil.rmtree(out_root) |
| 31 | + |
| 32 | +# Copy the full model directory into the package |
| 33 | +shutil.copytree(model_dir, data_dir) |
| 34 | + |
| 35 | +# --- Convert transformer weights to safetensors --- |
| 36 | +print("Loading model to convert transformer weights...") |
| 37 | +nlp = spacy.load(model_dir) |
| 38 | +trf = nlp.get_pipe("transformer") |
| 39 | +model_bytes = trf.model.to_bytes() |
| 40 | +print(f" Serialized transformer model: {len(model_bytes)} bytes") |
| 41 | +(data_dir / "transformer" / "model").write_bytes(model_bytes) |
| 42 | + |
| 43 | +# --- Update meta.json --- |
| 44 | +meta = json.loads((data_dir / "meta.json").read_text()) |
| 45 | +meta["name"] = META_NAME |
| 46 | +meta["version"] = NEW_VERSION |
| 47 | +meta["spacy_version"] = SPACY_COMPAT |
| 48 | +meta["requirements"] = [f"spacy-transformers{SPACY_TRF_COMPAT}"] |
| 49 | +(data_dir / "meta.json").write_text(json.dumps(meta, indent=2)) |
| 50 | + |
| 51 | +# --- Write package __init__.py --- |
| 52 | +init_py = f'''"""{PKG_NAME} model, repackaged for spacy-transformers {SPACY_TRF_COMPAT}.""" |
| 53 | +from pathlib import Path |
| 54 | +from spacy.util import load_model_from_init_py, get_model_meta |
| 55 | +
|
| 56 | +__version__ = get_model_meta(Path(__file__).parent / "{PKG_NAME}-{NEW_VERSION}")["version"] |
| 57 | +
|
| 58 | +def load(**overrides): |
| 59 | + return load_model_from_init_py(__file__, **overrides) |
| 60 | +''' |
| 61 | +(pkg_dir / "__init__.py").write_text(init_py) |
| 62 | + |
| 63 | +# spacy.load looks for meta.json next to __init__.py |
| 64 | +shutil.copy(data_dir / "meta.json", pkg_dir / "meta.json") |
| 65 | + |
| 66 | +# --- Write build files at project root --- |
| 67 | +setup_cfg = f"""[metadata] |
| 68 | +name = {PKG_NAME} |
| 69 | +version = {NEW_VERSION} |
| 70 | +description = English transformer pipeline (roberta-base), repackaged for spacy-transformers 1.4+ |
| 71 | +license = MIT |
| 72 | +
|
| 73 | +[options] |
| 74 | +zip_safe = false |
| 75 | +include_package_data = true |
| 76 | +packages = {PKG_NAME} |
| 77 | +python_requires = >=3.11 |
| 78 | +install_requires = |
| 79 | + spacy{SPACY_COMPAT} |
| 80 | + spacy-transformers{SPACY_TRF_COMPAT} |
| 81 | +""" |
| 82 | +(out_root / "setup.cfg").write_text(setup_cfg) |
| 83 | + |
| 84 | +pyproject_toml = """[build-system] |
| 85 | +requires = ["setuptools"] |
| 86 | +build-backend = "setuptools.build_meta" |
| 87 | +""" |
| 88 | +(out_root / "pyproject.toml").write_text(pyproject_toml) |
| 89 | + |
| 90 | +manifest = f"""recursive-include {PKG_NAME} * |
| 91 | +""" |
| 92 | +(out_root / "MANIFEST.in").write_text(manifest) |
| 93 | + |
| 94 | +print(f"\nRepackaged model written to: {out_root}") |
| 95 | +print(f"To build a wheel: cd {out_root} && python -m build --wheel") |
| 96 | +print(f"To load: spacy.load('{PKG_NAME}')") |
0 commit comments