wrapture
Version:
Wrapture lets you go from a Python-trained model to deployable JavaScript with a single command. It generates TypeScript bindings and a Web/Node-compatible wrapper, using WebGPU/WASM-ready ONNX runtimes.
186 lines (131 loc) β’ 5.52 kB
Markdown
# Wrapture
<p align="center" width="100%">
<img width="256px" src="./public/wrapture-logo-small.png" alt="wrapture logo" />
</p>
> One-click exporter from PyTorch models to Web-ready ONNX with JS/TS wrappers.
[](http://commitizen.github.io/cz-cli/)
[](http://makeapullrequest.com)
[](http://semver.org/spec/v2.0.0.html)






[](https://codecov.io/gh/phun-ky/wrapture)
[](https://github.com/phun-ky/wrapture/actions/workflows/check.yml)
## About
Wrapture lets you go from a Python-trained model to deployable JavaScript with a
single command. It generates TypeScript bindings and a Web/Node-compatible
wrapper, using WebGPU/WASM-ready ONNX runtimes.
> [!NOTE]
> This is an experiment trying to fulfil a need between python and js.
> YMMV
## Table of Contents<!-- omit from toc -->
- [Wrapture](#-wrapture)
- [About](#about)
- [π Features](#-features)
- [Prerequisites](#prerequisites)
- [Python 3.10+ required](#python-310-required)
- [Install required Python packages](#install-required-python-packages)
- [Installation](#installation)
- [Generating a Model](#generating-a-model)
- [Usage](#usage)
- [Output Structure](#output-structure)
- [Example: Using the Generated Model](#example-using-the-generated-model)
- [API](#api)
- [Contributing](#contributing)
- [License](#license)
- [Changelog](#changelog)
- [Sponsor me](#sponsor-me)
## π Features
- β
Convert PyTorch models to ONNX
- β
Optional ONNX simplification and quantization
- β
Generate `loadModel()` + `predict()` JavaScript wrappers
- β
Auto-generate `.d.ts` TypeScript bindings
---
## Prerequisites
### Python 3.10+ required
Install Python if you donβt have it: π <https://www.python.org/downloads/>
---
### Install required Python packages
```shell-session
python3 -m pip install torch onnx onnxsim onnxruntime
```
Check your installation:
```shell-session
python3 -c "import torch; print(torch.__version__)"
python3 -c "import onnx; print(onnx.__version__)"
```
You should see output like:
`2.x.x` etc..
## Installation
```shell-session
npm i -g wrapture
```
## Generating a Model
A helper script is provided to create a basic test model.
```shell-session
python3 python/scripts/basic_model.py
```
This generates:
```shell-session
test/fixtures/basic_model.pt
```
## Usage
```shell-session
wrapture --input test/fixtures/basic_model.pt --output ./wrapped
```
Youβll see a spinner as the model is converted, and then a JS/TS wrapper is
written to the ./wrapped/ directory.
## Output Structure
Example contents of a `--output ./` folder:
```shell-session
/
βββ wrapped.ts # The loadModel() + predict() logic
βββ wrapped.d.ts # Fully typed API
βββ model.onnx # Exported ONNX model
```
## Example: Using the Generated Model
```ts
import { loadModel } from './wrapped.js';
const model = await loadModel();
const input = { data: new Float32Array(1 _3_ 224 \* 224), dims: [1, 3, 224, 224]
};
const result = await model.predict(input); console.log(result); // { // logits:
Float32Array, // probabilities: number[], // predictedClass: number // }
```
---
## API
Full API documentation is available
[here](https://github.com/phun-ky/wrapture/blob/main/api/README.md).
---
## Contributing
Want to contribute? Please read the
[CONTRIBUTING.md](https://github.com/phun-ky/wrapture/blob/main/CONTRIBUTING.md)
and
[CODE_OF_CONDUCT.md](https://github.com/phun-ky/wrapture/blob/main/CODE_OF_CONDUCT.md)
## License
This project is licensed under the MIT License - see the
[LICENSE](https://github.com/phun-ky/wrapture/blob/main/LICENSE) file for
details.
## Changelog
See the
[CHANGELOG.md](https://github.com/phun-ky/wrapture/blob/main/CHANGELOG.md) for
details on the latest updates.
## Sponsor me
I'm an Open Source evangelist, creating stuff that does not exist yet to help
get rid of secondary activities and to enhance systems already in place, be it
documentation or web sites.
The sponsorship is an unique opportunity to alleviate more hours for me to
maintain my projects, create new ones and contribute to the large community
we're all part of :)
[Support me on GitHub Sponsors](https://github.com/sponsors/phun-ky).
p.s. **Ukraine is still under brutal Russian invasion. A lot of Ukrainian people
are hurt, without shelter and need help**. You can help in various ways, for
instance, directly helping refugees, spreading awareness, putting pressure on
your local government or companies. You can also support Ukraine by donating
e.g. to [Red Cross](https://www.icrc.org/en/donate/ukraine),
[Ukraine humanitarian organisation](https://savelife.in.ua/en/donate-en/#donate-army-card-weekly)
or
[donate Ambulances for Ukraine](https://www.gofundme.com/f/help-to-save-the-lives-of-civilians-in-a-war-zone).