gliner-node-wrapper
Version:
Node.js wrapper around Python-based GlinER NER library
106 lines (70 loc) โข 2.17 kB
Markdown
# GlinER Node.js Wrapper
A secure and production-ready Node.js wrapper for the [GlinER](https://huggingface.co/urchade/gliner_base) Named Entity Recognition (NER) Python library.
This package allows you to call GlinER from Node.js to extract entities using labels of your choice.
## ๐ฆ Installation
### Prerequisites
- Python 3.7+
- Node.js 14+
### Step 1: Install via npm
```bash
npm install gliner-node-wrapper
```
### Step 2: Python Dependencies (Auto-installed)
This package automatically installs Python dependencies (`gliner`, `torch`, `transformers`) during installation using a `postinstall` hook.
## ๐ Usage
### Basic Example
```js
const { extractEntities } = require('gliner-node-wrapper');
const text = "Barack Obama served as president of the United States.";
const labels = ["person", "organization", "location"];
extractEntities(text, labels)
.then((entities) => console.log("Entities:", entities))
.catch((err) => console.error("Error:", err.message));
```
### Output
```json
[
{ "text": "Barack Obama", "label": "person" },
{ "text": "United States", "label": "location" }
]
```
## ๐ How It Works
- A Node.js script (`index.js`) spawns a secure Python subprocess.
- Input text and labels are passed via stdin to a Python script.
- The Python script runs GlinER and returns extracted entities as JSON.
## ๐ Project Structure
```
gliner-node-wrapper/
โโโ index.js # Main Node wrapper
โโโ gliner_wrapper.py # Python subprocess handler
โโโ install-python.js # Auto-installs Python deps
โโโ requirements.txt # Python dependencies
โโโ example.js # Demo usage
โโโ package.json
โโโ README.md
```
## ๐ Security Features
- Input validation on both JS and Python sides
- Timeout to kill unresponsive subprocesses
- No shell evaluation (uses `spawn` safely)
- Full error reporting
## ๐งช Development
Clone the repo:
```bash
git clone https://github.com/yourname/gliner-node-wrapper.git
cd gliner-node-wrapper
npm install
```
Run the test:
```bash
npm test
```
## ๐ License
MIT ยฉ 2025 Your Name