Skip to content

Commit b2fb094

Browse files
✨ implement support for V2 API (#328)
1 parent f57ba8d commit b2fb094

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1438
-123
lines changed

.github/workflows/_test-code-samples.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
max-parallel: 2
1313
matrix:
1414
python-version:
15-
- "3.7"
15+
- "3.8"
1616
- "3.12"
1717
runs-on: "ubuntu-22.04"
1818
steps:
@@ -40,7 +40,7 @@ jobs:
4040
4141
- name: Tests code samples
4242
run: |
43-
./tests/test_code_samples.sh ${{ secrets.MINDEE_ACCOUNT_SE_TESTS }} ${{ secrets.MINDEE_ENDPOINT_SE_TESTS }} ${{ secrets.MINDEE_API_KEY_SE_TESTS }}
43+
./tests/test_code_samples.sh ${{ secrets.MINDEE_ACCOUNT_SE_TESTS }} ${{ secrets.MINDEE_ENDPOINT_SE_TESTS }} ${{ secrets.MINDEE_API_KEY_SE_TESTS }} ${{ secrets.MINDEE_V2_SE_TESTS_API_KEY }} ${{ secrets.MINDEE_V2_SE_TESTS_FINDOC_MODEL_ID }}
4444
4545
- name: Notify Slack Action on Failure
4646
uses: ravsamhq/notify-slack-action@2.3.0

.github/workflows/_test-integrations.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- "ubuntu-22.04"
1919
- "windows-2022"
2020
python-version:
21-
- "3.7"
21+
- "3.8"
2222
- "3.12"
2323
runs-on: ${{ matrix.os }}
2424
steps:
@@ -47,6 +47,8 @@ jobs:
4747
env:
4848
MINDEE_API_KEY: ${{ secrets.MINDEE_API_KEY_SE_TESTS }}
4949
WORKFLOW_ID: ${{ secrets.WORKFLOW_ID_SE_TESTS }}
50+
MINDEE_V2_API_KEY: ${{ secrets.MINDEE_V2_SE_TESTS_API_KEY }}
51+
MINDEE_V2_FINDOC_MODEL_ID: ${{ secrets.MINDEE_V2_SE_TESTS_FINDOC_MODEL_ID }}
5052
run: |
5153
pytest -m integration
5254

.github/workflows/_test-regressions.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- "ubuntu-22.04"
1919
- "windows-2022"
2020
python-version:
21-
- "3.7"
21+
- "3.8"
2222
- "3.12"
2323
runs-on: ${{ matrix.os }}
2424
steps:

.github/workflows/_test-units.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ jobs:
1616
- "ubuntu-22.04"
1717
- "windows-2022"
1818
python-version:
19-
- "3.7"
2019
- "3.8"
2120
- "3.9"
2221
- "3.10"

.github/workflows/pull-request.yml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,26 @@ name: Pull Request
33
on:
44
pull_request:
55

6+
permissions:
7+
contents: read
8+
pull-requests: read
9+
610
jobs:
711
static-analysis:
8-
uses: mindee/mindee-api-python/.github/workflows/_static-analysis.yml@main
12+
uses: ./.github/workflows/_static-analysis.yml
913
test-units:
10-
uses: mindee/mindee-api-python/.github/workflows/_test-units.yml@main
14+
uses: ./.github/workflows/_test-units.yml
1115
needs: static-analysis
1216
secrets: inherit
1317
test-regressions:
14-
uses: mindee/mindee-api-python/.github/workflows/_test-regressions.yml@main
18+
uses: ./.github/workflows/_test-regressions.yml
1519
needs: test-units
1620
secrets: inherit
1721
test-integrations:
18-
uses: mindee/mindee-api-python/.github/workflows/_test-integrations.yml@main
22+
uses: ./.github/workflows/_test-integrations.yml
1923
needs: test-units
2024
secrets: inherit
2125
test-code-samples:
22-
uses: mindee/mindee-api-python/.github/workflows/_test-code-samples.yml@main
26+
uses: ./.github/workflows/_test-code-samples.yml
2327
needs: test-units
2428
secrets: inherit
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#
2+
# Install the Python client library by running:
3+
# pip install mindee
4+
#
5+
6+
from mindee import ClientV2, InferencePredictOptions
7+
from mindee.parsing.v2 import InferenceResponse, PollingResponse
8+
from tests.product import PRODUCT_DATA_DIR
9+
10+
input_path = "/path/to/the/file.ext"
11+
api_key = "MY_API_KEY"
12+
model_id = "MY_MODEL_ID"
13+
14+
# Init a new client
15+
mindee_client = ClientV2(api_key)
16+
17+
# Load a file from disk
18+
input_doc = mindee_client.source_from_path(input_path)
19+
options = InferencePredictOptions(model_id=model_id)
20+
21+
# Parse the file.
22+
response: InferenceResponse = mindee_client.enqueue_and_parse(input_doc, options)
23+
24+
# Print a brief summary of the parsed data
25+
print(response.inference)

mindee/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from mindee import product
22
from mindee.client import Client
3+
from mindee.client_v2 import ClientV2
4+
from mindee.input.inference_predict_options import InferencePredictOptions
35
from mindee.input.local_response import LocalResponse
46
from mindee.input.page_options import PageOptions
57
from mindee.parsing.common.api_response import ApiResponse

mindee/client.py

Lines changed: 3 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
from pathlib import Path
21
from time import sleep
3-
from typing import BinaryIO, Dict, Optional, Type, Union
2+
from typing import Dict, Optional, Type, Union
43

4+
from mindee.client_mixin import ClientMixin
55
from mindee.error.mindee_error import MindeeClientError, MindeeError
66
from mindee.error.mindee_http_error import handle_error
77
from mindee.input import WorkflowOptions
88
from mindee.input.local_response import LocalResponse
99
from mindee.input.page_options import PageOptions
1010
from mindee.input.predict_options import AsyncPredictOptions, PredictOptions
11-
from mindee.input.sources.base_64_input import Base64Input
12-
from mindee.input.sources.bytes_input import BytesInput
13-
from mindee.input.sources.file_input import FileInput
1411
from mindee.input.sources.local_input_source import LocalInputSource
15-
from mindee.input.sources.path_input import PathInput
1612
from mindee.input.sources.url_input_source import UrlInputSource
1713
from mindee.logger import logger
1814
from mindee.mindee_http.endpoint import CustomEndpoint, Endpoint
@@ -55,7 +51,7 @@ def _clean_account_name(account_name: str) -> str:
5551
return account_name
5652

5753

58-
class Client:
54+
class Client(ClientMixin):
5955
"""
6056
Mindee API Client.
6157
@@ -275,23 +271,6 @@ def execute_workflow(
275271
logger.debug("Sending document to workflow: %s", workflow_id)
276272
return self._send_to_workflow(GeneratedV1, input_source, workflow_id, options)
277273

278-
def _validate_async_params(
279-
self, initial_delay_sec: float, delay_sec: float, max_retries: int
280-
) -> None:
281-
min_delay = 1
282-
min_initial_delay = 1
283-
min_retries = 1
284-
if delay_sec < min_delay:
285-
raise MindeeClientError(
286-
f"Cannot set auto-parsing delay to less than {min_delay} second(s)."
287-
)
288-
if initial_delay_sec < min_initial_delay:
289-
raise MindeeClientError(
290-
f"Cannot set initial parsing delay to less than {min_initial_delay} second(s)."
291-
)
292-
if max_retries < min_retries:
293-
raise MindeeClientError(f"Cannot set retries to less than {min_retries}.")
294-
295274
def enqueue_and_parse( # pylint: disable=too-many-locals
296275
self,
297276
product_class: Type[Inference],
@@ -584,70 +563,6 @@ def create_endpoint(
584563
version = "1"
585564
return self._build_endpoint(endpoint_name, account_name, version)
586565

587-
@staticmethod
588-
def source_from_path(
589-
input_path: Union[Path, str], fix_pdf: bool = False
590-
) -> PathInput:
591-
"""
592-
Load a document from an absolute path, as a string.
593-
594-
:param input_path: Path of file to open
595-
:param fix_pdf: Whether to attempt fixing PDF files before sending.
596-
Setting this to `True` can modify the data sent to Mindee.
597-
"""
598-
input_doc = PathInput(input_path)
599-
if fix_pdf:
600-
input_doc.fix_pdf()
601-
return input_doc
602-
603-
@staticmethod
604-
def source_from_file(input_file: BinaryIO, fix_pdf: bool = False) -> FileInput:
605-
"""
606-
Load a document from a normal Python file object/handle.
607-
608-
:param input_file: Input file handle
609-
:param fix_pdf: Whether to attempt fixing PDF files before sending.
610-
Setting this to `True` can modify the data sent to Mindee.
611-
"""
612-
input_doc = FileInput(input_file)
613-
if fix_pdf:
614-
input_doc.fix_pdf()
615-
return input_doc
616-
617-
@staticmethod
618-
def source_from_b64string(
619-
input_string: str, filename: str, fix_pdf: bool = False
620-
) -> Base64Input:
621-
"""
622-
Load a document from a base64 encoded string.
623-
624-
:param input_string: Input to parse as base64 string
625-
:param filename: The name of the file (without the path)
626-
:param fix_pdf: Whether to attempt fixing PDF files before sending.
627-
Setting this to `True` can modify the data sent to Mindee.
628-
"""
629-
input_doc = Base64Input(input_string, filename)
630-
if fix_pdf:
631-
input_doc.fix_pdf()
632-
return input_doc
633-
634-
@staticmethod
635-
def source_from_bytes(
636-
input_bytes: bytes, filename: str, fix_pdf: bool = False
637-
) -> BytesInput:
638-
"""
639-
Load a document from raw bytes.
640-
641-
:param input_bytes: Raw byte input
642-
:param filename: The name of the file (without the path)
643-
:param fix_pdf: Whether to attempt fixing PDF files before sending.
644-
Setting this to `True` can modify the data sent to Mindee.
645-
"""
646-
input_doc = BytesInput(input_bytes, filename)
647-
if fix_pdf:
648-
input_doc.fix_pdf()
649-
return input_doc
650-
651566
@staticmethod
652567
def source_from_url(
653568
url: str,

mindee/client_mixin.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
from pathlib import Path
2+
from typing import BinaryIO, Union
3+
4+
from mindee.error import MindeeClientError
5+
from mindee.input import Base64Input, BytesInput, FileInput, PathInput
6+
7+
8+
class ClientMixin:
9+
"""Mixin for client Client V1 & V2 common static methods."""
10+
11+
@staticmethod
12+
def source_from_path(
13+
input_path: Union[Path, str], fix_pdf: bool = False
14+
) -> PathInput:
15+
"""
16+
Load a document from an absolute path, as a string.
17+
18+
:param input_path: Path of file to open
19+
:param fix_pdf: Whether to attempt fixing PDF files before sending.
20+
Setting this to `True` can modify the data sent to Mindee.
21+
"""
22+
input_doc = PathInput(input_path)
23+
if fix_pdf:
24+
input_doc.fix_pdf()
25+
return input_doc
26+
27+
@staticmethod
28+
def source_from_file(input_file: BinaryIO, fix_pdf: bool = False) -> FileInput:
29+
"""
30+
Load a document from a normal Python file object/handle.
31+
32+
:param input_file: Input file handle
33+
:param fix_pdf: Whether to attempt fixing PDF files before sending.
34+
Setting this to `True` can modify the data sent to Mindee.
35+
"""
36+
input_doc = FileInput(input_file)
37+
if fix_pdf:
38+
input_doc.fix_pdf()
39+
return input_doc
40+
41+
@staticmethod
42+
def source_from_b64string(
43+
input_string: str, filename: str, fix_pdf: bool = False
44+
) -> Base64Input:
45+
"""
46+
Load a document from a base64 encoded string.
47+
48+
:param input_string: Input to parse as base64 string
49+
:param filename: The name of the file (without the path)
50+
:param fix_pdf: Whether to attempt fixing PDF files before sending.
51+
Setting this to `True` can modify the data sent to Mindee.
52+
"""
53+
input_doc = Base64Input(input_string, filename)
54+
if fix_pdf:
55+
input_doc.fix_pdf()
56+
return input_doc
57+
58+
@staticmethod
59+
def source_from_bytes(
60+
input_bytes: bytes, filename: str, fix_pdf: bool = False
61+
) -> BytesInput:
62+
"""
63+
Load a document from raw bytes.
64+
65+
:param input_bytes: Raw byte input
66+
:param filename: The name of the file (without the path)
67+
:param fix_pdf: Whether to attempt fixing PDF files before sending.
68+
Setting this to `True` can modify the data sent to Mindee.
69+
"""
70+
input_doc = BytesInput(input_bytes, filename)
71+
if fix_pdf:
72+
input_doc.fix_pdf()
73+
return input_doc
74+
75+
@staticmethod
76+
def _validate_async_params(
77+
initial_delay_sec: float, delay_sec: float, max_retries: int
78+
) -> None:
79+
min_delay = 1
80+
min_initial_delay = 1
81+
min_retries = 1
82+
if delay_sec < min_delay:
83+
raise MindeeClientError(
84+
f"Cannot set auto-parsing delay to less than {min_delay} second(s)."
85+
)
86+
if initial_delay_sec < min_initial_delay:
87+
raise MindeeClientError(
88+
f"Cannot set initial parsing delay to less than {min_initial_delay} second(s)."
89+
)
90+
if max_retries < min_retries:
91+
raise MindeeClientError(f"Cannot set retries to less than {min_retries}.")

0 commit comments

Comments
 (0)