Last active
May 25, 2024 10:19
-
-
Save dacr/f657bb159441a0f8447571f61f8a958e to your computer and use it in GitHub Desktop.
sentiment analysis using DJL / published by https://github.com/dacr/code-examples-manager #65caa624-232d-483d-83d2-0d4ea0067540/4d079d819834510a709ae96a96f636d28e6b9df7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// summary : sentiment analysis using DJL | |
// keywords : djl, machine-learning, tutorial, sentiment, ai, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 65caa624-232d-483d-83d2-0d4ea0067540 | |
// created-on : 2024-01-28T16:16:23+01:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.2" | |
//> using dep "org.slf4j:slf4j-api:2.0.13" | |
//> using dep "org.slf4j:slf4j-simple:2.0.13" | |
//> using dep "net.java.dev.jna:jna:5.14.0" | |
//> using dep "ai.djl:api:0.28.0" | |
//> using dep "ai.djl:basicdataset:0.28.0" | |
//> using dep "ai.djl:model-zoo:0.28.0" | |
//> using dep "ai.djl.huggingface:tokenizers:0.28.0" | |
//> using dep "ai.djl.mxnet:mxnet-engine:0.28.0" | |
//> using dep "ai.djl.mxnet:mxnet-model-zoo:0.28.0" | |
//> using dep "ai.djl.pytorch:pytorch-engine:0.28.0" | |
//> using dep "ai.djl.pytorch:pytorch-model-zoo:0.28.0" | |
//> using dep "ai.djl.tensorflow:tensorflow-engine:0.28.0" | |
//> using dep "ai.djl.tensorflow:tensorflow-model-zoo:0.28.0" | |
////> using dep "ai.djl.paddlepaddle:paddlepaddle-engine:0.28.0" | |
////> using dep "ai.djl.paddlepaddle:paddlepaddle-model-zoo:0.28.0" | |
//> using dep "ai.djl.onnxruntime:onnxruntime-engine:0.28.0" | |
// --------------------- | |
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "error") | |
import ai.djl.Application | |
import ai.djl.engine.Engine | |
import ai.djl.modality.Classifications | |
import ai.djl.repository.zoo.Criteria | |
import ai.djl.training.util.ProgressBar | |
import ai.djl.huggingface.translator.{TextClassificationTranslatorFactory, TextEmbeddingTranslatorFactory} | |
import scala.io.AnsiColor.{BLUE, BOLD, CYAN, GREEN, MAGENTA, RED, RESET, UNDERLINED, YELLOW} | |
val criteria = | |
Criteria.builder | |
.setTypes(classOf[String], classOf[Classifications]) | |
.optModelUrls("djl://ai.djl.huggingface.pytorch/distilbert-base-uncased-finetuned-sst-2-english") | |
.optEngine("PyTorch") | |
.optTranslatorFactory(new TextClassificationTranslatorFactory) | |
.optProgress(new ProgressBar) | |
.build | |
val model = criteria.loadModel() | |
val predictor = model.newPredictor() | |
def analyze(text: String): Unit = { | |
println(s"${GREEN}====================$text====================${RESET}") | |
val result = predictor.predict(text) | |
println(result) | |
} | |
analyze("Hello world") | |
analyze("I like you") | |
analyze("This an awful shirt") | |
analyze("You are a fucking bastard") | |
analyze("This is not very good for you but it can help") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment