Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api] improve error message #1348

Merged
merged 1 commit into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public Output processOutput(TranslatorContext ctx, NDList list) throws Exception
public NDList processInput(TranslatorContext ctx, Input input) throws Exception {
BytesSupplier data = input.getData();
try {
if (data == null) {
throw new TranslateException("Input data is empty.");
}
Image image = factory.fromInputStream(new ByteArrayInputStream(data.getAsBytes()));
return translator.processInput(ctx, image);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
import ai.djl.ndarray.BytesSupplier;
import ai.djl.ndarray.NDList;
import ai.djl.translate.Batchifier;
import ai.djl.translate.TranslateException;
import ai.djl.translate.Translator;
import ai.djl.translate.TranslatorContext;
import ai.djl.util.JsonUtils;
import ai.djl.util.PairList;
import com.google.gson.JsonParseException;

/**
* A {@link Translator} that can handle generic question answering {@link Input} and {@link Output}.
Expand Down Expand Up @@ -61,7 +63,15 @@ public NDList processInput(TranslatorContext ctx, Input input) throws Exception
String paragraph = input.getAsString("paragraph");
qa = new QAInput(question, paragraph);
} else {
qa = JsonUtils.GSON.fromJson(input.getData().getAsString(), QAInput.class);
BytesSupplier data = input.getData();
if (data == null) {
throw new TranslateException("Input data is empty.");
}
try {
qa = JsonUtils.GSON.fromJson(data.getAsString(), QAInput.class);
} catch (JsonParseException e) {
throw new TranslateException("Input is not a valid json.", e);
}
}
return translator.processInput(ctx, qa);
}
Expand Down