Java

Create a client to talk to the API

1
Credentials credentials = new Credentials(
2
System.getenv("LAS_CLIENT_ID"),
3
System.getenv("LAS_CLIENT_SECRET"),
4
System.getenv("LAS_API_KEY"),
5
System.getenv("LAS_AUTH_ENDPOINT"),
6
System.getenv("LAS_API_ENDPOINT")
7
);
8
9
client = new Client(credentials);
Copied!

Upload a document to get a document id

1
public void createDocument() throws IOException, APIException, MissingAccessTokenException {
2
ContentType contentType = ContentType.fromString("image/jpeg");
3
Path path = Paths.get("path/to/document");
4
byte[] content = Files.readAllBytes(path);
5
6
JSONObject document = client.createDocument(content, contentType);
7
Assert.assertTrue(document.has("contentType"));
8
Assert.assertTrue(document.has("documentId"));
9
}
Copied!

Make a prediction on a document

Suppose we wish to run inference on a document using one of the available models.
1
public void createPrediction() throws IOException, APIException, MissingAccessTokenException {
2
String modelId = "las:model:<hex-uuid>";
3
JSONObject prediction = client.createPrediction(documentId, modelId);
4
JSONArray fields = prediction.getJSONArray("predictions");
5
Assert.assertNotNull(fields);
6
}
Copied!
See what models you have available and their model id by using the method listModels()

Set ground truth of document

When uploading data that will be used for training and evaluation, we need to provide a ground truth.
1
File file = new File("myReceipt.pdf");
2
InputStream content = new FileInputStream(file);
3
JSONArray groundTruth = new JSONArray();
4
groundTruth.put(new JSONObject(){{ put("label", "totalAmount"); put("value", "100.00"); }});
5
groundTruth.put(new JSONObject(){{ put("label", "dueDate"); put("value", "2020-02-20"); }});
6
CreateDocumentOptions options = new CreateDocumentOptions().setGroundTruth(groundTruth);
7
JSONObject document = this.client.createDocument(content, ContentType.PDF, options);
Copied!

Update an existing document

If for instance a prediction reveals incorrect values in the ground truth of a document, we can update the existing document with new ground truth values.
1
JSONArray groundTruth = new JSONArray();
2
groundTruth.put(new JSONObject(){{ put("label", "totalAmount"); put("value", "199.00"); }});
3
groundTruth.put(new JSONObject(){{ put("label", "dueDate"); put("value", "2020-03-20"); }});
4
JSONObject document = this.client.createDocument("las:document:<hex-uuid>", groundTruth);
Copied!

Set ground truth of document

Suppose we make a prediction that returns incorrect values and we wish to improve the model for future use. We can do so by sending groundTruth to the model, telling it what the expected values should have been.

Create a batch and associate a few documents with it

Creating a batch is a way to group documents. This is useful for specifying batches of documents to use in improving the model later.
1
public void createBatch() throws IOException, APIException, MissingAccessTokenException {
2
CreadBatchOptions options = new CreateBatchOptions()
3
.setName("TrainingData")
4
.setDescription("I'm gonna create a new batch, give me a batch id!");
5
JSONObject response = client.createBatch(options);
6
Assert.assertNotNull(response.get("batchId"));
7
}
Copied!
Last modified 5mo ago