Skip to content

✨ add support for Mindee Client V2 #251

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
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
18 changes: 13 additions & 5 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,29 @@ name: Pull Request
on:
pull_request:

permissions:
contents: read
pull-requests: read

jobs:
static_analysis:
uses: mindee/mindee-api-java/.github/workflows/_static-analysis.yml@main
uses: ./.github/workflows/_static-analysis.yml
build:
uses: mindee/mindee-api-java/.github/workflows/_build.yml@main
uses: ./.github/workflows/_build.yml
needs: static_analysis
secrets: inherit
codeql:
uses: mindee/mindee-api-java/.github/workflows/_codeql.yml@main
uses: ./.github/workflows/_codeql.yml
needs: build
permissions:
contents: read
actions: read
security-events: write
test_integrations:
uses: mindee/mindee-api-java/.github/workflows/_test-integrations.yml@main
uses: ./.github/workflows/_test-integrations.yml
needs: build
secrets: inherit
test_code_samples:
uses: mindee/mindee-api-java/.github/workflows/_test-code-samples.yml@main
uses: ./.github/workflows/_test-code-samples.yml
needs: build
secrets: inherit
45 changes: 45 additions & 0 deletions docs/code_samples/default_v2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import com.mindee.MindeeClientV2;
import com.mindee.InferenceOptions;
import com.mindee.input.LocalInputSource;
import com.mindee.parsing.v2.InferenceResponse;
import java.io.File;
import java.io.IOException;

public class SimpleMindeeClient {

public static void main(String[] args) throws IOException, InterruptedException {
String apiKey = "MY_API_KEY";
String filePath = "/path/to/the/file.ext";

// Init a new client
MindeeClientV2 mindeeClient = new MindeeClientV2(apiKey);

// Load a file from disk
LocalInputSource inputSource = new LocalInputSource(new File(filePath));

// Prepare the enqueueing options
// Note: modelId is mandatory.
InferenceOptions options = InferenceOptions.builder("MY_MODEL_ID").build();

// Parse the file
InferenceResponse response = mindeeClient.enqueueAndParse(
inputSource,
options
);

// Print a summary of the response
System.out.println(response.getInference().toString());

// Print a summary of the predictions
// System.out.println(response.getDocument().toString());

// Print the document-level predictions
// System.out.println(response.getDocument().getInference().getPrediction().toString());

// Print the page-level predictions
// response.getDocument().getInference().getPages().forEach(
// page -> System.out.println(page.toString())
// );
}

}
36 changes: 36 additions & 0 deletions src/main/java/com/mindee/CommonClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.mindee;

import com.mindee.input.LocalInputSource;
import com.mindee.input.PageOptions;
import com.mindee.pdf.PdfOperation;
import com.mindee.pdf.SplitQuery;
import java.io.IOException;

/**
* Common client for all Mindee API clients.
*/
public abstract class CommonClient {
protected PdfOperation pdfOperation;

/**
* Retrieves the file after applying page operations to it.
* @param localInputSource Local input source to apply operations to.
* @param pageOptions Options to apply.
* @return A byte array of the file after applying page operations.
* @throws IOException Throws if the file can't be accessed.
*/
protected byte[] getSplitFile(
LocalInputSource localInputSource,
PageOptions pageOptions
) throws IOException {
byte[] splitFile;
if (pageOptions == null || !localInputSource.isPdf()) {
splitFile = localInputSource.getFile();
} else {
splitFile = pdfOperation.split(
new SplitQuery(localInputSource.getFile(), pageOptions)
).getFile();
}
return splitFile;
}
}
122 changes: 122 additions & 0 deletions src/main/java/com/mindee/InferenceOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package com.mindee;

import com.mindee.input.PageOptions;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import lombok.Data;
import lombok.Getter;

/**
* Options to pass when calling methods using the API V2.
*/
@Getter
@Data
public final class InferenceOptions {
/**
* ID of the model (required).
*/
private final String modelId;
/**
* Whether to include full text data for async APIs.
* Performing a full OCR on the server increases response time and payload size.
*/
private final boolean fullText;
/**
* Enables Retrieval-Augmented Generation (optional, default: {@code false}).
*/
private final boolean rag;
/**
* Optional alias for the file.
*/
private final String alias;
/**
* IDs of webhooks to propagate the API response to (may be empty).
*/
private final List<String> webhookIds;
/**
* Page options to apply to the document.
*/
private final PageOptions pageOptions;
/*
* Asynchronous polling options.
*/
private final AsyncPollingOptions pollingOptions;

/**
* Create a new builder.
*
* @param modelId the mandatory model identifier
* @return a fresh {@link Builder}
*/
public static Builder builder(String modelId) {
return new Builder(modelId);
}

/**
* Fluent builder for {@link InferenceOptions}.
*/
public static final class Builder {

private final String modelId;
private boolean fullText = false;
private boolean rag = false;
private String alias;
private List<String> webhookIds = Collections.emptyList();
private PageOptions pageOptions = null;
private AsyncPollingOptions pollingOptions = AsyncPollingOptions.builder().build();

private Builder(String modelId) {
this.modelId = Objects.requireNonNull(modelId, "modelId must not be null");
}

/**
* Toggle full-text OCR extraction.
*/
public Builder fullText(boolean fullText) {
this.fullText = fullText;
return this;
}

/** Enable / disable Retrieval-Augmented Generation. */
public Builder rag(boolean rag) {
this.rag = rag;
return this;
}

/** Set an alias for the uploaded document. */
public Builder alias(String alias) {
this.alias = alias;
return this;
}

/** Provide IDs of webhooks to forward the API response to. */
public Builder webhookIds(List<String> webhookIds) {
this.webhookIds = webhookIds;
return this;
}

public Builder pageOptions(PageOptions pageOptions) {
this.pageOptions = pageOptions;
return this;
}

public Builder pollingOptions(AsyncPollingOptions pollingOptions) {
this.pollingOptions = pollingOptions;
return this;
}

/** Build an immutable {@link InferenceOptions} instance. */
public InferenceOptions build() {
return new InferenceOptions(
modelId,
fullText,
rag,
alias,
webhookIds,
pageOptions,
pollingOptions
);
}
}
}
19 changes: 1 addition & 18 deletions src/main/java/com/mindee/MindeeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.mindee.parsing.common.WorkflowResponse;
import com.mindee.pdf.PdfBoxApi;
import com.mindee.pdf.PdfOperation;
import com.mindee.pdf.SplitQuery;
import com.mindee.product.custom.CustomV1;
import com.mindee.product.generated.GeneratedV1;
import java.io.IOException;
Expand All @@ -25,10 +24,9 @@
/**
* Main entrypoint for Mindee operations.
*/
public class MindeeClient {
public class MindeeClient extends CommonClient {

private final MindeeApi mindeeApi;
private final PdfOperation pdfOperation;

/**
* Create a default MindeeClient.
Expand Down Expand Up @@ -1124,19 +1122,4 @@ public <T extends Inference> AsyncPredictResponse<T> loadPrediction(
return objectMapper.readValue(localResponse.getFile(), parametricType);
}

private byte[] getSplitFile(
LocalInputSource localInputSource,
PageOptions pageOptions
) throws IOException {
byte[] splitFile;
if (pageOptions == null || !localInputSource.isPdf()) {
splitFile = localInputSource.getFile();
} else {
splitFile = pdfOperation.split(
new SplitQuery(localInputSource.getFile(), pageOptions)
).getFile();
}
return splitFile;
}

}
Loading
Loading