Skip to content

Ahwar/NER-NLP-with-ONNX-Java

Repository files navigation

Run HuggingFace NER (NLP) Model on Java using ONNX Runtime and DJL

Java application

A Natural Language Processing (NLP) Java application that detects names, organizations, and locations in a text by running Hugging Face's Roberta NER model using ONNX Runtime and the Deep Java Library.

Installation

Open the project folder in a Java IDE (recommended: IntelliJ IDEA Community) with Gradle support and build the project.

Requirements

  1. Java Development Kit (JDK) version 17
  2. Gradle version 8.9

Download Files

These files are required to run the project:

  1. ONNX model
  2. tokenizer.json file

Convert the ONNX Model

To convert the Hugging Face NER model to ONNX, open this Google Colaboratory Notebook, run the code as shown in the image below, and follow all the steps.

run colab code cell

(The code for this purpose is also saved in the Jupyter notebook file convert Huggingface model to ONNX.ipynb. You can run the code using Jupyter Notebook.)

After running one of the above codes, your ONNX model will be saved in the onnx/ folder.

Download tokenizer.json

The tokenizer file tokenizer.json was taken from this Hugging Face repository. Download the tokenizer.json from this link.

Some times, the tokenizer.json file is created in onnx conversion process, In that case you can find it in onnx/ folder.

Move Files

Copy the files created from the above steps into the raw-files directory as shown in the image below.

raw-files path

Building the Project

Build the project using the button shown below.

how to build project

Run the Code

Open the Main.java file and click the play button as shown in the red box in the image below.

how to run project

Run with Gradle

You can build and run the project using the included Gradle wrapper. The Gradle wrapper (gradlew) allows you to build and run the project without installing Gradle system-wide.

  • Build the application:
./gradlew build
  • Run the application:
./gradlew run

The run task will execute the Main class defined in the project.

Install Gradle (optional)

If you want to install Gradle system-wide instead of using the wrapper, here are two common options. Using the Gradle wrapper is recommended for consistent builds.

  • Install via SDKMAN (recommended):
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk install gradle
gradle -v
  • Install on Debian/Ubuntu (may not be latest):
sudo apt update
sudo apt install gradle
gradle -v

After installing Gradle, you can run gradle build and gradle run instead of using ./gradlew.

Star History

Star History Chart

About

A Java NLP application that identifies names, organizations, and locations in text by utilizing Hugging Face's RoBERTa NER model through the ONNX runtime and the Deep Java Library.

Topics

Resources

Stars

Watchers

Forks

Contributors