Skip to content

OPENNLP-1518: Roberta-based Models - Add support for utilization via Onxx#998

Open
rzo1 wants to merge 1 commit intomainfrom
OPENNLP-1518
Open

OPENNLP-1518: Roberta-based Models - Add support for utilization via Onxx#998
rzo1 wants to merge 1 commit intomainfrom
OPENNLP-1518

Conversation

@rzo1
Copy link
Copy Markdown
Contributor

@rzo1 rzo1 commented Mar 26, 2026

For all changes:

  • Is there a JIRA ticket associated with this PR? Is it referenced
    in the commit message?

  • Does your PR title start with OPENNLP-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.

  • Has your PR been rebased against the latest commit within the target branch (typically main)?

  • Is your initial contribution a single, squashed commit?

For code changes:

  • Have you ensured that the full suite of tests is executed via mvn clean install at the root opennlp folder?
  • Have you written or updated unit tests to verify your changes?
  • If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under ASF 2.0?
  • If applicable, have you updated the LICENSE file, including the main LICENSE file in opennlp folder?
  • If applicable, have you updated the NOTICE file, including the main NOTICE file found in opennlp folder?

For documentation related changes:

  • Have you ensured that format looks appropriate for the output in which it is rendered?

Note:

Converted the model mentioned in the issue to ONXX first. Did run a test via

public class RobertaGoEmotionsTest {

  private static final String MODEL_DIR = "roberta-base-go_emotions";

  public static void main(String[] args) throws Exception {

    final File modelDir = new File(MODEL_DIR);
    if (!modelDir.exists()) {
      System.err.println("Model directory not found: " + modelDir.getAbsolutePath());
      System.err.println("Clone it with: git clone https://huggingface.co/SamLowe/roberta-base-go_emotions");
      return;
    }

    final File onnxModel = new File(modelDir, "model.onnx");
    if (!onnxModel.exists()) {
      System.err.println("ONNX model file not found: " + onnxModel.getAbsolutePath());
      System.err.println("Download it from: https://huggingface.co/SamLowe/roberta-base-go_emotions/resolve/main/onnx/model.onnx");
      return;
    }

    final File vocabFile = new File(modelDir, "vocab.json");
    final File configFile = new File(modelDir, "config.json");

    // RoBERTa does not use token_type_ids
    final InferenceOptions inferenceOptions = new InferenceOptions();
    inferenceOptions.setIncludeTokenTypeIds(false);

    try (DocumentCategorizerDL categorizer = new DocumentCategorizerDL(
        onnxModel, vocabFile, configFile,
        new AverageClassificationScoringStrategy(),
        inferenceOptions)) {

      final String[] testSentences = {
          "I am so happy today!",
          "This is terrible and I hate it.",
          "Thank you so much for your help!",
          "I'm not sure what to think about this.",
          "That's hilarious, I can't stop laughing!"
      };

      for (final String sentence : testSentences) {
        System.out.println("Input: \"" + sentence + "\"");

        final SortedMap<Double, Set<String>> scores = categorizer.sortedScoreMap(new String[] {sentence});

        System.out.println("  Top emotions:");
        final List<Double> sortedScores = new ArrayList<>(scores.keySet());
        Collections.reverse(sortedScores);
        int count = 0;
        for (final Double score : sortedScores) {
          if (count++ >= 3) break;
          System.out.printf("    %-20s %.4f%n", scores.get(score), score);
        }
        System.out.println();
      }
    }
  }
}

This will fail with the unpatched version but will work with the patch:

Input: "I am so happy today!"
  Top emotions:
    [joy]                0,8805
    [excitement]         0,0736
    [neutral]            0,0105

Input: "This is terrible and I hate it."
  Top emotions:
    [anger]              0,6766
    [annoyance]          0,1317
    [disapproval]        0,1008

Input: "Thank you so much for your help!"
  Top emotions:
    [gratitude]          0,9992
    [approval]           0,0001
    [admiration]         0,0001

Input: "I'm not sure what to think about this."
  Top emotions:
    [confusion]          0,9703
    [neutral]            0,0082
    [disapproval]        0,0025

Input: "That's hilarious, I can't stop laughing!"
  Top emotions:
    [amusement]          0,7920
    [joy]                0,1405
    [neutral]            0,0279

Note: Since we do not use a JSON library (like jackson) here, I used a regex approach with a quirky check to parse a json based vocab file. The original issue failed due to missing Roberta special tokens.

@rzo1 rzo1 requested review from jzonthemtn and mawiesne March 26, 2026 19:52
@rzo1 rzo1 force-pushed the OPENNLP-1518 branch 2 times, most recently from 58e454b to 2db2c60 Compare March 26, 2026 20:05
@rzo1
Copy link
Copy Markdown
Contributor Author

rzo1 commented Mar 27, 2026

Let's wait with the merge until the current vote is completed.

@mawiesne mawiesne changed the title OPENNLP-1518 - Roberta-based Models - Add support for utilization via Onxx OPENNLP-1518: Roberta-based Models - Add support for utilization via Onxx Mar 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants