Skip to content

Commit 152a353

Browse files
✨ Add support for generated v1 (#156)
Co-authored-by: sebastianMindee <sebastian.oliverasilvera@mindee.co>
1 parent 62bdf37 commit 152a353

17 files changed

Lines changed: 909 additions & 28 deletions

docs/code_samples/default.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import com.mindee.MindeeClient;
22
import com.mindee.input.LocalInputSource;
33
import com.mindee.parsing.common.PredictResponse;
4-
import com.mindee.product.custom.CustomV1;
4+
import com.mindee.product.generated.GeneratedV1;
55
import com.mindee.http.Endpoint;
66
import java.io.File;
77
import java.io.IOException;
@@ -26,9 +26,10 @@ public class SimpleMindeeClient {
2626
);
2727

2828
// Parse the file
29-
PredictResponse<CustomV1> response = mindeeClient.parse(
30-
inputSource,
31-
endpoint
29+
PredictResponse<GeneratedV1> response = mindeeClient.parse(
30+
GeneratedV1.class,
31+
endpoint,
32+
inputSource
3233
);
3334

3435
// Print a summary of the response
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import com.mindee.MindeeClient;
2+
import com.mindee.input.LocalInputSource;
3+
import com.mindee.parsing.common.AsyncPredictResponse;
4+
import com.mindee.product.generated.GeneratedV1;
5+
import com.mindee.http.Endpoint;
6+
import java.io.File;
7+
import java.io.IOException;
8+
9+
public class SimpleMindeeClient {
10+
11+
public static void main(String[] args) throws IOException, InterruptedException {
12+
String apiKey = "my-api-key";
13+
String filePath = "/path/to/the/file.ext";
14+
15+
// Init a new client
16+
MindeeClient mindeeClient = new MindeeClient(apiKey);
17+
18+
// Load a file from disk
19+
LocalInputSource inputSource = new LocalInputSource(new File(filePath));
20+
21+
// Configure the endpoint
22+
Endpoint endpoint = new Endpoint(
23+
"my-endpoint",
24+
"my-account",
25+
"my-version"
26+
);
27+
28+
// Parse the file asynchronously
29+
AsyncPredictResponse<GeneratedV1> response = mindeeClient.enqueueAndParse(
30+
GeneratedV1.class,
31+
endpoint,
32+
inputSource
33+
);
34+
35+
// Print a summary of the response
36+
System.out.println(response.toString());
37+
38+
// Print a summary of the predictions
39+
// System.out.println(response.getDocumentObj().toString());
40+
41+
// Print the document-level predictions
42+
// System.out.println(response.getDocumentObj().getInference().getPrediction().toString());
43+
44+
// Print the page-level predictions
45+
// response.getDocumentObj().getInference().getPages().forEach(
46+
// page -> System.out.println(page.toString())
47+
// );
48+
}
49+
50+
}

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

Lines changed: 276 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.mindee.pdf.PdfOperation;
1818
import com.mindee.pdf.SplitQuery;
1919
import com.mindee.product.custom.CustomV1;
20+
import com.mindee.product.generated.GeneratedV1;
2021
import java.io.IOException;
2122
import java.net.URL;
2223

@@ -312,7 +313,7 @@ private <T extends Inference> AsyncPredictResponse<T> enqueueAndParse(
312313
Thread.sleep(initialDelaySec);
313314

314315
while (retryCount < pollingOptions.getMaxRetries()) {
315-
parseResponse = parseQueued(type, jobId);
316+
parseResponse = this.mindeeApi.documentQueueGet(type, endpoint, jobId);
316317
if (parseResponse.getDocument().isPresent()) {
317318
return parseResponse;
318319
}
@@ -490,6 +491,280 @@ private PredictResponse<CustomV1> parse(
490491
.build());
491492
}
492493

494+
/**
495+
* Send a local file to a Generated prediction async API queue.
496+
*/
497+
public <T extends GeneratedV1> AsyncPredictResponse<T> enqueue(
498+
Class<T> type,
499+
Endpoint endpoint,
500+
LocalInputSource localInputSource
501+
) throws IOException {
502+
return this.enqueue(
503+
type,
504+
endpoint,
505+
localInputSource.getFile(),
506+
localInputSource.getFilename(),
507+
null,
508+
null
509+
);
510+
}
511+
512+
/**
513+
* Send a local file to a Generated prediction async API queue.
514+
*/
515+
public <T extends GeneratedV1> AsyncPredictResponse<T> enqueue(
516+
Class<T> type,
517+
Endpoint endpoint,
518+
LocalInputSource localInputSource,
519+
PredictOptions predictOptions,
520+
PageOptions pageOptions
521+
) throws IOException {
522+
return this.enqueue(
523+
type,
524+
endpoint,
525+
getSplitFile(localInputSource, pageOptions),
526+
localInputSource.getFilename(),
527+
predictOptions,
528+
null
529+
);
530+
}
531+
532+
/**
533+
* Send a remote file to a Generated prediction async API queue.
534+
*/
535+
public <T extends GeneratedV1> AsyncPredictResponse<T> enqueue(
536+
Class<T> type,
537+
Endpoint endpoint,
538+
URL sourceUrl
539+
) throws IOException {
540+
InputSourceUtils.validateUrl(sourceUrl);
541+
return this.enqueue(
542+
type,
543+
endpoint,
544+
null,
545+
null,
546+
null,
547+
sourceUrl
548+
);
549+
}
550+
551+
/**
552+
* Send a remote file to a Generated prediction async API queue.
553+
*/
554+
public <T extends GeneratedV1> AsyncPredictResponse<T> enqueue(
555+
Class<T> type,
556+
Endpoint endpoint,
557+
URL sourceUrl,
558+
PredictOptions predictOptions
559+
) throws IOException {
560+
InputSourceUtils.validateUrl(sourceUrl);
561+
return this.enqueue(
562+
type,
563+
endpoint,
564+
null,
565+
null,
566+
predictOptions,
567+
sourceUrl
568+
);
569+
}
570+
571+
572+
/**
573+
* Send a local file to a Generated prediction API async queue, poll, and parse when complete.
574+
*/
575+
public <T extends GeneratedV1> AsyncPredictResponse<T> enqueueAndParse(
576+
Class<T> type,
577+
Endpoint endpoint,
578+
LocalInputSource localInputSource
579+
) throws IOException, InterruptedException {
580+
return this.enqueueAndParse(
581+
type,
582+
endpoint,
583+
null,
584+
localInputSource.getFile(),
585+
localInputSource.getFilename(),
586+
null,
587+
null);
588+
}
589+
590+
/**
591+
* Send a local file to a Generated prediction API async queue, poll, and parse when complete.
592+
*/
593+
public <T extends GeneratedV1> AsyncPredictResponse<T> enqueueAndParse(
594+
Class<T> type,
595+
Endpoint endpoint,
596+
LocalInputSource localInputSource,
597+
AsyncPollingOptions pollingOptions
598+
) throws IOException, InterruptedException {
599+
return this.enqueueAndParse(
600+
type,
601+
endpoint,
602+
pollingOptions,
603+
localInputSource.getFile(),
604+
localInputSource.getFilename(),
605+
null,
606+
null
607+
);
608+
}
609+
610+
/**
611+
* Send a local file to a Generated prediction API async queue, poll, and parse when complete.
612+
*/
613+
public <T extends GeneratedV1> AsyncPredictResponse<T> enqueueAndParse(
614+
Class<T> type,
615+
Endpoint endpoint,
616+
LocalInputSource localInputSource,
617+
PredictOptions predictOptions,
618+
PageOptions pageOptions,
619+
AsyncPollingOptions pollingOptions
620+
) throws IOException, InterruptedException {
621+
return this.enqueueAndParse(
622+
type,
623+
endpoint,
624+
pollingOptions,
625+
getSplitFile(localInputSource, pageOptions),
626+
localInputSource.getFilename(),
627+
predictOptions,
628+
null
629+
);
630+
}
631+
632+
/**
633+
* Send a remote file to a Generated prediction API async queue, poll, and parse when complete.
634+
*/
635+
public <T extends GeneratedV1> AsyncPredictResponse<T> enqueueAndParse(
636+
Class<T> type,
637+
Endpoint endpoint,
638+
URL sourceUrl
639+
) throws IOException, InterruptedException {
640+
InputSourceUtils.validateUrl(sourceUrl);
641+
return this.enqueueAndParse(
642+
type,
643+
endpoint,
644+
null,
645+
null,
646+
null,
647+
null,
648+
sourceUrl
649+
);
650+
}
651+
652+
/**
653+
* Send a local file to a Generated prediction API and parse the results.
654+
*/
655+
public <T extends GeneratedV1> PredictResponse<T> parse(
656+
Class<T> type,
657+
Endpoint endpoint,
658+
LocalInputSource localInputSource
659+
) throws IOException {
660+
return this.parse(
661+
type,
662+
endpoint,
663+
localInputSource.getFile(),
664+
localInputSource.getFilename(),
665+
null,
666+
null
667+
);
668+
}
669+
670+
/**
671+
* Send a local file to a Generated prediction API and parse the results.
672+
*/
673+
public <T extends GeneratedV1> PredictResponse<T> parse(
674+
Class<T> type,
675+
Endpoint endpoint,
676+
LocalInputSource localInputSource,
677+
PredictOptions predictOptions
678+
) throws IOException {
679+
return this.parse(
680+
type,
681+
endpoint,
682+
localInputSource.getFile(),
683+
localInputSource.getFilename(),
684+
predictOptions,
685+
null
686+
);
687+
}
688+
689+
/**
690+
* Send a local file to a Generated prediction API and parse the results.
691+
*/
692+
public <T extends GeneratedV1> PredictResponse<T> parse(
693+
Class<T> type,
694+
Endpoint endpoint,
695+
LocalInputSource localInputSource,
696+
PageOptions pageOptions
697+
) throws IOException {
698+
return this.parse(
699+
type,
700+
endpoint,
701+
getSplitFile(localInputSource, pageOptions),
702+
localInputSource.getFilename(),
703+
null,
704+
null
705+
);
706+
}
707+
708+
/**
709+
* Send a local file to a Standard prediction API and parse the results.
710+
*/
711+
public <T extends GeneratedV1> PredictResponse<T> parse(
712+
Class<T> type,
713+
Endpoint endpoint,
714+
LocalInputSource localInputSource,
715+
PredictOptions predictOptions,
716+
PageOptions pageOptions
717+
) throws IOException {
718+
return this.parse(
719+
type,
720+
endpoint,
721+
getSplitFile(localInputSource, pageOptions),
722+
localInputSource.getFilename(),
723+
predictOptions,
724+
null
725+
);
726+
}
727+
728+
/**
729+
* Send a remote file to a Generated prediction API and parse the results.
730+
*/
731+
public <T extends GeneratedV1> PredictResponse<T> parse(
732+
Class<T> type,
733+
Endpoint endpoint,
734+
URL documentUrl
735+
) throws IOException {
736+
InputSourceUtils.validateUrl(documentUrl);
737+
return this.parse(type, endpoint, null, null, null, documentUrl);
738+
}
739+
740+
/**
741+
* Send a remote file to a Generated prediction API and parse the results.
742+
*/
743+
public <T extends GeneratedV1> PredictResponse<T> parse(
744+
Class<T> type,
745+
Endpoint endpoint,
746+
URL documentUrl,
747+
PredictOptions predictOptions
748+
) throws IOException {
749+
InputSourceUtils.validateUrl(documentUrl);
750+
return this.parse(type, endpoint, null, null, predictOptions, documentUrl);
751+
}
752+
753+
/**
754+
* Parse a document from a Generated prediction API async queue.
755+
*/
756+
public <T extends GeneratedV1> AsyncPredictResponse<T> parseQueued(
757+
Class<T> type,
758+
Endpoint endpoint,
759+
String jobId
760+
) {
761+
return this.mindeeApi.documentQueueGet(
762+
type,
763+
endpoint,
764+
jobId
765+
);
766+
}
767+
493768
/**
494769
* Load a local prediction.
495770
* Typically used when wanting to load from a webhook callback.

0 commit comments

Comments
 (0)