Skip to content

Commit 6a3ec0a

Browse files
rework inference & page options
1 parent 6518b7a commit 6a3ec0a

File tree

5 files changed

+50
-67
lines changed

5 files changed

+50
-67
lines changed

src/main/java/com/mindee/CommonClient.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/main/java/com/mindee/InferenceParameters.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ public final class InferenceParameters {
2929
* IDs of webhooks to propagate the API response to (may be empty).
3030
*/
3131
private final List<String> webhookIds;
32-
/**
33-
* Page options to apply to the document.
34-
*/
35-
private final PageOptions pageOptions;
3632
/*
3733
* Asynchronous polling options.
3834
*/
@@ -57,7 +53,6 @@ public static final class Builder {
5753
private boolean rag = false;
5854
private String alias;
5955
private List<String> webhookIds = Collections.emptyList();
60-
private PageOptions pageOptions = null;
6156
private AsyncPollingOptions pollingOptions = AsyncPollingOptions.builder().build();
6257

6358
private Builder(String modelId) {
@@ -82,10 +77,6 @@ public Builder webhookIds(List<String> webhookIds) {
8277
return this;
8378
}
8479

85-
public Builder pageOptions(PageOptions pageOptions) {
86-
this.pageOptions = pageOptions;
87-
return this;
88-
}
8980

9081
public Builder pollingOptions(AsyncPollingOptions pollingOptions) {
9182
this.pollingOptions = pollingOptions;
@@ -99,7 +90,6 @@ public InferenceParameters build() {
9990
rag,
10091
alias,
10192
webhookIds,
102-
pageOptions,
10393
pollingOptions
10494
);
10595
}

src/main/java/com/mindee/MindeeClient.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.mindee.parsing.common.WorkflowResponse;
1717
import com.mindee.pdf.PdfBoxApi;
1818
import com.mindee.pdf.PdfOperation;
19+
import com.mindee.pdf.SplitQuery;
1920
import com.mindee.product.custom.CustomV1;
2021
import com.mindee.product.generated.GeneratedV1;
2122
import java.io.IOException;
@@ -24,8 +25,9 @@
2425
/**
2526
* Main entrypoint for Mindee operations.
2627
*/
27-
public class MindeeClient extends CommonClient {
28+
public class MindeeClient {
2829

30+
protected PdfOperation pdfOperation;
2931
private final MindeeApi mindeeApi;
3032

3133
/**
@@ -118,6 +120,28 @@ public <T extends Inference> AsyncPredictResponse<T> enqueue(
118120
null);
119121
}
120122

123+
/**
124+
* Retrieves the file after applying page operations to it.
125+
* @param localInputSource Local input source to apply operations to.
126+
* @param pageOptions Options to apply.
127+
* @return A byte array of the file after applying page operations.
128+
* @throws IOException Throws if the file can't be accessed.
129+
*/
130+
protected byte[] getSplitFile(
131+
LocalInputSource localInputSource,
132+
PageOptions pageOptions
133+
) throws IOException {
134+
byte[] splitFile;
135+
if (pageOptions == null || !localInputSource.isPdf()) {
136+
splitFile = localInputSource.getFile();
137+
} else {
138+
splitFile = pdfOperation.split(
139+
new SplitQuery(localInputSource.getFile(), pageOptions)
140+
).getFile();
141+
}
142+
return splitFile;
143+
}
144+
121145
/**
122146
* Send a local file to an async queue.
123147
* @param <T> Type of inference.

src/main/java/com/mindee/MindeeClientV2.java

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,27 @@
99
import com.mindee.parsing.v2.ErrorResponse;
1010
import com.mindee.parsing.v2.InferenceResponse;
1111
import com.mindee.parsing.v2.JobResponse;
12-
import com.mindee.pdf.PdfBoxApi;
13-
import com.mindee.pdf.PdfOperation;
1412
import java.io.IOException;
1513

1614
/**
1715
* Entry point for the Mindee **V2** API features.
1816
*/
19-
public class MindeeClientV2 extends CommonClient {
17+
public class MindeeClientV2 {
2018
private final MindeeApiV2 mindeeApi;
2119

2220
/** Uses an API-key read from the environment variables. */
2321
public MindeeClientV2() {
24-
this(new PdfBoxApi(), createDefaultApiV2(""));
22+
this(createDefaultApiV2(""));
2523
}
2624

2725
/** Uses the supplied API-key. */
2826
public MindeeClientV2(String apiKey) {
29-
this(new PdfBoxApi(), createDefaultApiV2(apiKey));
27+
this(createDefaultApiV2(apiKey));
3028
}
3129

32-
/** Directly inject an already configured {@link MindeeApiV2}. */
33-
public MindeeClientV2(MindeeApiV2 mindeeApi) {
34-
this(new PdfBoxApi(), mindeeApi);
35-
}
3630

3731
/** Inject both a PDF implementation and a HTTP implementation. */
38-
public MindeeClientV2(PdfOperation pdfOperation, MindeeApiV2 mindeeApi) {
39-
this.pdfOperation = pdfOperation;
32+
public MindeeClientV2(MindeeApiV2 mindeeApi) {
4033
this.mindeeApi = mindeeApi;
4134
}
4235

@@ -46,16 +39,9 @@ public MindeeClientV2(PdfOperation pdfOperation, MindeeApiV2 mindeeApi) {
4639
public JobResponse enqueue(
4740
LocalInputSource inputSource,
4841
InferenceParameters params) throws IOException {
49-
LocalInputSource finalInput;
50-
if (params.getPageOptions() != null) {
51-
finalInput = new LocalInputSource(getSplitFile(inputSource, params.getPageOptions()), inputSource.getFilename());
52-
} else {
53-
finalInput = inputSource;
54-
}
55-
return mindeeApi.reqPostInferenceEnqueue(finalInput, params);
42+
return mindeeApi.reqPostInferenceEnqueue(inputSource, params);
5643
}
5744

58-
5945
/**
6046
* Poll queue for a previously enqueued document.
6147
*/
@@ -103,7 +89,7 @@ public InferenceResponse enqueueAndParse(
10389
if (resp.getJob().getStatus().equals("Failed")) {
10490
break;
10591
}
106-
else if(resp.getJob().getStatus().equals("Processed")) {
92+
else if (resp.getJob().getStatus().equals("Processed")) {
10793
return parseQueued(resp.getJob().getId());
10894
}
10995
attempts++;

src/main/java/com/mindee/input/LocalInputSource.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.mindee.input;
22

33
import com.mindee.image.ImageCompressor;
4+
import com.mindee.pdf.PdfBoxApi;
45
import com.mindee.pdf.PdfCompressor;
6+
import com.mindee.pdf.PdfOperation;
7+
import com.mindee.pdf.SplitQuery;
58
import java.io.File;
69
import java.io.IOException;
710
import java.io.InputStream;
@@ -45,6 +48,22 @@ public LocalInputSource(String fileAsBase64, String filename) {
4548
this.filename = filename;
4649
}
4750

51+
52+
/**
53+
* Applies PDF-specific operations on the current file based on the specified {@code PageOptions}.
54+
*
55+
* @param pageOptions The options specifying which pages to modify or retain in the PDF file.
56+
* @throws IOException If an I/O error occurs during the PDF operation.
57+
*/
58+
public void applyOperations (PageOptions pageOptions) throws IOException {
59+
if (pageOptions != null && this.isPdf()) {
60+
PdfOperation pdfOperation = new PdfBoxApi();
61+
this.file = pdfOperation.split(
62+
new SplitQuery(this.file, pageOptions)
63+
).getFile();
64+
}
65+
}
66+
4867
public boolean isPdf() {
4968
return InputSourceUtils.isPdf(this.file);
5069
}

0 commit comments

Comments
 (0)