GridGain Developers Hub

Get Started With GridGain ML

This section provides a complete, working example of deploying and using a sentiment analysis model.

Step 1: Download a Pre-trained Model

Download a sentiment analysis model from DJL Model Zoo:

import ai.djl.Application;
import ai.djl.repository.zoo.Criteria;
import ai.djl.repository.zoo.ZooModel;
import ai.djl.modality.Classifications;

public class DownloadModel {
    public static void main(String[] args) {
        try {
            // Create a criteria builder for sentiment analysis
            Criteria<String, Classifications> criteria = Criteria.builder()
                .setTypes(String.class, Classifications.class)
                .optModelUrls("djl://ai.djl.pytorch/distilbert")
                .build();

            System.out.println("Loading model...");

            // Load the model from DJL model zoo
            ZooModel<String, Classifications> model = criteria.loadModel();
            Path modelPath = model.getModelPath();
            System.out.println("Model loaded from: " + modelPath);

            // Create the target directory
            Path targetDir = Paths.get("src/main/resources/models/sentiment");
            Files.createDirectories(targetDir);

            // Copy all model files to the target directory
            System.out.println("Copying model files to: " + targetDir);
            copyDirectory(modelPath, targetDir);
            System.out.println("Model downloaded successfully to: " + targetDir);

            // Close the model
            model.close();
        } catch (Exception e) {
            System.err.println("Error downloading model: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private static void copyDirectory(Path source, Path target) throws IOException {
        Files.createDirectories(target);
        Files.list(source).forEach(path -> {
            try {
                Path targetPath = target.resolve(source.relativize(path));
                if (Files.isDirectory(path)) {
                    copyDirectory(path, targetPath);
                } else {
                    Files.copy(path, targetPath, StandardCopyOption.REPLACE_EXISTING);
                    System.out.println("  Copied: " + targetPath.getFileName());
                }
            } catch (IOException e) {
                throw new RuntimeException("Failed to copy: " + path, e);
            }
        });
    }
}

Step 2: Deploy Model to GridGain

Deploy the downloaded model to all nodes:

# Deploy the downloaded model to all nodes
cluster unit deploy sentiment-model \
    --version 1.0.0 \
    --path /path/to/model/directory \
    --nodes ALL

# Verify deployment
cluster unit list

For more information about code deployment, see the Code Deployment article.

Step 3: Run Your First Prediction

Run the prediction using the downloaded model:

package org.apache.ignite.example.ml;

import org.apache.ignite.client.IgniteClient;
import org.gridgain.ml.IgniteMl;
import org.gridgain.ml.model.*;
import ai.djl.modality.Classifications;

public class QuickStartExample {
    public static void main(String[] args) {
        start(MODE.EMBEDDED);

        // Prepare prediction request
        MlSimpleJobParameters<String> jobParams =
            MlSimpleJobParameters.<String>builder()
                .id("sentiment-model")
                .version("1.0.0")
                .type(ModelType.PYTORCH)
                .name("traced_distilbert_sst_english")
                .inputClass(String.class.getName())
                .outputClass(Classifications.class.getName())
                .property("application",
                    "ai.djl.Application$NLP$SENTIMENT_ANALYSIS")
                .translatorFactory("ai.djl.pytorch.zoo.nlp.sentimentanalysis.PtDistilBertTranslatorFactory")
                .input("This product is absolutely fantastic! Best purchase ever!")
                .build();

        // Run prediction
        Classifications result = ml.predict(jobParams);

        // Display results
        System.out.println("Sentiment Analysis Result:");
        System.out.println("Text: 'This product is absolutely fantastic! Best purchase ever!'");
        System.out.println("Prediction: " + result.best().getClassName());
        System.out.println("Confidence: " + (result.best().getProbability() * 100) + "%");
    }
}