Skip to content

Commit 2198e8f

Browse files
LemurPwnedIngvarstepMVYaroshenkoZbigniewTomanek
authored
Feat/add python bindings (#1)
* first commit * fix major issues * fix span mask * refactor the code * js->c++ * refactor: update structure and cmake * fix: submodules * refactor: update example structure and fix warnings, inlcude and make onnxruntime option fatal * refactor: remove unnecessary flags in example * refactor: update model interface * feat: add gpu support * docs: describe gpu usage procedure * docs: make variables explicit * docs: make variables explicit * feat: add cuda check * Update README.md * Update README.md * doc: update README.md * feat: refactor and add support for token-level models * docs: fix description * docs: fix typos in README and add comments * fix: bug in decoder * flatNER to> true by default, update readme * Add license file * Use PCRE2 to tokenize UTF-8 text * fix: Fix pattern and update tests * initial bindings * fixing lfs * update workflow * build updates * build updates * build updates * build updates * cargo updates * update cmake reqs * update cmake reqs * add pathing to the old CMAKES * missing files added * missing files added * missing files added * attempt to update submodules during build * attempt to update submodules during build * attempt to update submodules during build * attempt to update submodules during build * attempt to update submodules during build * attempt to update submodules during build * many linus bump * many linus bump * many linus bump * many linus bump * many linus bump * wasm tests * wasm tests --------- Co-authored-by: Ihor Stepanov <igor.stepanov2000@gmail.com> Co-authored-by: Ihor Stepanov <64355919+Ingvarstep@users.noreply.github.com> Co-authored-by: MVYaroshenko <mykytayaroshenko@gmail.com> Co-authored-by: Zbigniew Tomanek <me@zbeegnew.dev> Co-authored-by: MVYaroshenko <108987221+MVYaroshenko@users.noreply.github.com>
1 parent 8f20189 commit 2198e8f

14 files changed

Lines changed: 866 additions & 188 deletions

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.onnx !text !filter !merge !diff

.github/workflows/build-wheels.yml

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
name: Build wheels with cibuildwheel
2+
3+
on:
4+
pull_request:
5+
branches: [ main ]
6+
paths:
7+
- '**.cpp'
8+
- '**.hpp'
9+
- '**.py'
10+
- 'pyproject.toml'
11+
- 'CMakeLists.txt'
12+
- 'bindings/**'
13+
- '.github/workflows/**'
14+
15+
pull_request_target:
16+
types: [closed]
17+
branches: [ main ]
18+
paths:
19+
- '**.cpp'
20+
- '**.hpp'
21+
- '**.py'
22+
- 'pyproject.toml'
23+
- 'CMakeLists.txt'
24+
- 'bindings/**'
25+
- '.github/workflows/**'
26+
27+
workflow_dispatch:
28+
inputs:
29+
dry-run:
30+
description: 'Dry run (skip publish to PyPI)'
31+
type: boolean
32+
default: true
33+
34+
jobs:
35+
build_wheels:
36+
name: Build wheels on ${{ matrix.os }}
37+
runs-on: ${{ matrix.os }}
38+
strategy:
39+
fail-fast: false
40+
matrix:
41+
os: [ubuntu-latest, macos-latest]
42+
env:
43+
TWINE_USERNAME: __token__
44+
IS_DRY_RUN: "true"
45+
CIBW_BUILD: "cp310-* cp311-* cp312-*"
46+
CIBW_SKIP: "pp* *-win32 *-manylinux_i686"
47+
CIBW_PROJECT_REQUIRES_PYTHON: ">=3.10"
48+
CIBW_BUILD_REQUIRES: "setuptools>=61.0 setuptools-scm>=8.0 pybind11>=2.6.1 wheel"
49+
CIBW_TEST_COMMAND: "cd {project} && python test_bindings/test_inference.py"
50+
CIBW_TEST_SKIP: "*-musllinux*"
51+
CIBW_MANYLINUX_X86_64_IMAGE: manylinux_2_34
52+
CIBW_MANYLINUX_AARCH64_IMAGE: manylinux_2_34
53+
CIBW_ENVIRONMENT: 'CMAKE_ARGS=-DDOWNLOAD_ONNXRUNTIME=ON -DCMAKE_POLICY_VERSION_MINIMUM=3.5 -DGIT_SUBMODULE=OFF PATH=$HOME/.cargo/bin:$PATH RUSTFLAGS="-A dangerous_implicit_autorefs"'
54+
CIBW_ENVIRONMENT_MACOS: 'CMAKE_ARGS=-DDOWNLOAD_ONNXRUNTIME=ON -DCMAKE_POLICY_VERSION_MINIMUM=3.5 -DGIT_SUBMODULE=OFF MACOSX_DEPLOYMENT_TARGET=11.0 PATH=$HOME/.cargo/bin:$PATH RUSTFLAGS="-A dangerous_implicit_autorefs"'
55+
CIBW_BEFORE_BUILD: 'python -m pip install --upgrade "cmake>=3.26" && python {project}/scripts/patch_tokenizers_cmake.py'
56+
CIBW_BEFORE_ALL_LINUX: "if ! command -v curl >/dev/null 2>&1; then yum install -y curl; fi; curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --profile minimal -y"
57+
steps:
58+
- uses: actions/checkout@v4
59+
with:
60+
submodules: 'true'
61+
62+
- name: Determine dry run mode
63+
shell: bash
64+
env:
65+
EVENT_NAME: ${{ github.event_name }}
66+
PR_MERGED: ${{ github.event.pull_request.merged || false }}
67+
DISPATCH_DRY_RUN: ${{ inputs['dry-run'] || 'true' }}
68+
run: |
69+
set -eo pipefail
70+
is_dry_run=true
71+
if [[ "$EVENT_NAME" == "pull_request_target" && "$PR_MERGED" == "true" ]]; then
72+
is_dry_run=false
73+
elif [[ "$EVENT_NAME" == "workflow_dispatch" && "$DISPATCH_DRY_RUN" == "false" ]]; then
74+
is_dry_run=false
75+
fi
76+
echo "Dry run mode: $is_dry_run"
77+
echo "IS_DRY_RUN=$is_dry_run" >> "$GITHUB_ENV"
78+
79+
- name: Download test model assets
80+
shell: bash
81+
run: |
82+
set -euo pipefail
83+
MODEL_NAME="gliner_small-v2.1"
84+
MODEL_DIR="models/${MODEL_NAME}"
85+
mkdir -p "${MODEL_DIR}"
86+
curl -L --fail --retry 5 --retry-delay 5 \
87+
"https://huggingface.co/onnx-community/${MODEL_NAME}/resolve/main/tokenizer.json" \
88+
-o "${MODEL_DIR}/tokenizer.json"
89+
curl -L --fail --retry 5 --retry-delay 5 \
90+
"https://huggingface.co/onnx-community/${MODEL_NAME}/resolve/main/onnx/model.onnx" \
91+
-o "${MODEL_DIR}/model.onnx"
92+
93+
- name: Set up Python
94+
uses: actions/setup-python@v5
95+
with:
96+
python-version: "3.11"
97+
98+
- name: Install Rust toolchain
99+
uses: dtolnay/rust-toolchain@stable
100+
101+
- name: Install cibuildwheel
102+
run: python -m pip install --upgrade pip cibuildwheel
103+
104+
- name: Build wheels
105+
run: python -m cibuildwheel --output-dir wheelhouse
106+
107+
- name: Install twine
108+
run: python -m pip install twine
109+
110+
- name: Check wheels
111+
run: python -m twine check wheelhouse/*
112+
113+
- name: Upload wheels to Test PyPI (dry run)
114+
if: env.IS_DRY_RUN == 'true'
115+
env:
116+
TWINE_PASSWORD: ${{ secrets.TEST_PYPI_API_TOKEN }}
117+
run: python -m twine upload --repository testpypi wheelhouse/* --verbose
118+
continue-on-error: true
119+
120+
- name: Upload wheels to PyPI (production)
121+
if: env.IS_DRY_RUN == 'false'
122+
env:
123+
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
124+
run: python -m twine upload wheelhouse/*
125+
126+
- name: Upload wheels as artifacts
127+
uses: actions/upload-artifact@v4
128+
with:
129+
name: wheels-${{ matrix.os }}-${{ strategy.job-index }}
130+
path: wheelhouse/*.whl
131+
retention-days: 30
132+
133+
build_sdist:
134+
name: Build source distribution
135+
runs-on: ubuntu-latest
136+
env:
137+
TWINE_USERNAME: __token__
138+
IS_DRY_RUN: "true"
139+
steps:
140+
- uses: actions/checkout@v4
141+
with:
142+
submodules: 'true'
143+
144+
- name: Determine dry run mode
145+
shell: bash
146+
env:
147+
EVENT_NAME: ${{ github.event_name }}
148+
PR_MERGED: ${{ github.event.pull_request.merged || false }}
149+
DISPATCH_DRY_RUN: ${{ inputs['dry-run'] || 'true' }}
150+
run: |
151+
set -eo pipefail
152+
is_dry_run=true
153+
if [[ "$EVENT_NAME" == "pull_request_target" && "$PR_MERGED" == "true" ]]; then
154+
is_dry_run=false
155+
elif [[ "$EVENT_NAME" == "workflow_dispatch" && "$DISPATCH_DRY_RUN" == "false" ]]; then
156+
is_dry_run=false
157+
fi
158+
echo "Dry run mode: $is_dry_run"
159+
echo "IS_DRY_RUN=$is_dry_run" >> "$GITHUB_ENV"
160+
161+
- name: Set up Python
162+
uses: actions/setup-python@v5
163+
with:
164+
python-version: "3.11"
165+
166+
- name: Build sdist
167+
run: |
168+
python -m pip install --upgrade pip build
169+
python -m build --sdist
170+
171+
- name: Install twine
172+
run: python -m pip install twine
173+
174+
- name: Check sdist
175+
run: python -m twine check dist/*
176+
177+
- name: Upload sdist to Test PyPI (dry run)
178+
if: env.IS_DRY_RUN == 'true'
179+
env:
180+
TWINE_PASSWORD: ${{ secrets.TEST_PYPI_API_TOKEN }}
181+
run: python -m twine upload --repository testpypi dist/* --verbose
182+
continue-on-error: true
183+
184+
- name: Upload sdist to PyPI (production)
185+
if: env.IS_DRY_RUN == 'false'
186+
env:
187+
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
188+
run: python -m twine upload dist/*
189+
190+
- name: Upload sdist as artifact
191+
uses: actions/upload-artifact@v4
192+
with:
193+
name: sdist
194+
path: dist/*.tar.gz
195+
retention-days: 30
196+
197+
release-build:
198+
name: Create GitHub Release
199+
needs: [build_wheels, build_sdist]
200+
if: (github.event_name == 'pull_request_target' && github.event.pull_request.merged == true)
201+
runs-on: ubuntu-latest
202+
steps:
203+
- uses: actions/checkout@v4
204+
205+
- name: Get version
206+
id: get_version
207+
run: |
208+
python -m pip install setuptools-scm
209+
echo "version=$(python -c "import setuptools_scm; print(setuptools_scm.get_version())")" >> $GITHUB_OUTPUT
210+
211+
- name: Download all artifacts
212+
uses: actions/download-artifact@v4
213+
with:
214+
path: artifacts
215+
216+
- name: Create release
217+
env:
218+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
219+
run: |
220+
gh release create "v${{ steps.get_version.outputs.version }}" \
221+
--repo="$GITHUB_REPOSITORY" \
222+
--title="${GITHUB_REPOSITORY#*/} ${{ steps.get_version.outputs.version }}" \
223+
--generate-notes \
224+
artifacts/*/*

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ Makefile
33
cmake_install.cmake
44
CMakeFiles/
55
.vscode/
6-
onnxruntime*/
6+
onnxruntime*/
7+
models/
8+
*.onnx

0 commit comments

Comments
 (0)