unstract-client 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- unstract_client-0.1.0/LICENSE +21 -0
- unstract_client-0.1.0/PKG-INFO +84 -0
- unstract_client-0.1.0/README.md +63 -0
- unstract_client-0.1.0/pyproject.toml +94 -0
- unstract_client-0.1.0/src/unstract/api_deployments/__init__.py +7 -0
- unstract_client-0.1.0/src/unstract/api_deployments/client.py +222 -0
- unstract_client-0.1.0/src/unstract/api_deployments/utils.py +22 -0
- unstract_client-0.1.0/tests/__init__.py +0 -0
- unstract_client-0.1.0/tests/client_test.py +40 -0
- unstract_client-0.1.0/tests/sample.env +2 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Zipstack
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: unstract-client
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python client for the Unstract LLM-powered structured data extraction platform
|
|
5
|
+
Keywords: unstract cli apps sdk
|
|
6
|
+
Author-Email: Zipstack Inc <devsupport@zipstack.com>
|
|
7
|
+
License: MIT
|
|
8
|
+
Classifier: Development Status :: 4 - Beta
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: License :: OSI Approved :: GNU Affero General Public License v3
|
|
11
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
12
|
+
Classifier: Operating System :: MacOS :: MacOS X
|
|
13
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
14
|
+
Classifier: Programming Language :: Python
|
|
15
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
16
|
+
Project-URL: Homepage, https://unstract.com
|
|
17
|
+
Project-URL: Source, https://github.com/Zipstack/unstract-python-client
|
|
18
|
+
Requires-Python: >=3.11
|
|
19
|
+
Requires-Dist: requests>=2.32.3
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
|
|
22
|
+
# unstract-python-client
|
|
23
|
+
Python client for the Unstract LLM-powered structured data extraction platform
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
You can install the Unstract Python Client using pip:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pip install unstract-client
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Usage
|
|
35
|
+
|
|
36
|
+
First, import the `APIDeploymentsClient` from the `client` module:
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
from unstract.api_deployments.client import APIDeploymentsClient
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Then, create an instance of the `APIDeploymentsClient`:
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
client = APIDeploymentsClient(api_url="url", api_key="your_api_key")
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Now, you can use the client to interact with the Unstract API deployments API:
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
try:
|
|
52
|
+
adc = APIDeploymentsClient(
|
|
53
|
+
api_url=os.getenv("UNSTRACT_API_URL"),
|
|
54
|
+
api_key=os.getenv("UNSTRACT_API_DEPLOYMENT_KEY"),
|
|
55
|
+
api_timeout=10,
|
|
56
|
+
logging_level="DEBUG",
|
|
57
|
+
)
|
|
58
|
+
# Replace files with pdfs
|
|
59
|
+
response = adc.structure_file(
|
|
60
|
+
["<files>"]
|
|
61
|
+
)
|
|
62
|
+
print(response)
|
|
63
|
+
if response["pending"]:
|
|
64
|
+
while True:
|
|
65
|
+
p_response = adc.check_execution_status(
|
|
66
|
+
response["status_check_api_endpoint"]
|
|
67
|
+
)
|
|
68
|
+
print(p_response)
|
|
69
|
+
if not p_response["pending"]:
|
|
70
|
+
break
|
|
71
|
+
print("Sleeping and checking again in 5 seconds..")
|
|
72
|
+
time.sleep(5)
|
|
73
|
+
except APIDeploymentsClientException as e:
|
|
74
|
+
print(e)
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
## Questions and Feedback
|
|
79
|
+
|
|
80
|
+
On Slack, [join great conversations](https://join-slack.unstract.com/) around LLMs, their ecosystem and leveraging them to automate the previously unautomatable!
|
|
81
|
+
|
|
82
|
+
[Unstract Cloud](https://unstract.com/): Signup and Try!
|
|
83
|
+
|
|
84
|
+
[Unstract developer documentation](https://docs.unstract.com/): Learn more about Unstract and its API.
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# unstract-python-client
|
|
2
|
+
Python client for the Unstract LLM-powered structured data extraction platform
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
You can install the Unstract Python Client using pip:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install unstract-client
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
First, import the `APIDeploymentsClient` from the `client` module:
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
from unstract.api_deployments.client import APIDeploymentsClient
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Then, create an instance of the `APIDeploymentsClient`:
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
client = APIDeploymentsClient(api_url="url", api_key="your_api_key")
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Now, you can use the client to interact with the Unstract API deployments API:
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
try:
|
|
31
|
+
adc = APIDeploymentsClient(
|
|
32
|
+
api_url=os.getenv("UNSTRACT_API_URL"),
|
|
33
|
+
api_key=os.getenv("UNSTRACT_API_DEPLOYMENT_KEY"),
|
|
34
|
+
api_timeout=10,
|
|
35
|
+
logging_level="DEBUG",
|
|
36
|
+
)
|
|
37
|
+
# Replace files with pdfs
|
|
38
|
+
response = adc.structure_file(
|
|
39
|
+
["<files>"]
|
|
40
|
+
)
|
|
41
|
+
print(response)
|
|
42
|
+
if response["pending"]:
|
|
43
|
+
while True:
|
|
44
|
+
p_response = adc.check_execution_status(
|
|
45
|
+
response["status_check_api_endpoint"]
|
|
46
|
+
)
|
|
47
|
+
print(p_response)
|
|
48
|
+
if not p_response["pending"]:
|
|
49
|
+
break
|
|
50
|
+
print("Sleeping and checking again in 5 seconds..")
|
|
51
|
+
time.sleep(5)
|
|
52
|
+
except APIDeploymentsClientException as e:
|
|
53
|
+
print(e)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
## Questions and Feedback
|
|
58
|
+
|
|
59
|
+
On Slack, [join great conversations](https://join-slack.unstract.com/) around LLMs, their ecosystem and leveraging them to automate the previously unautomatable!
|
|
60
|
+
|
|
61
|
+
[Unstract Cloud](https://unstract.com/): Signup and Try!
|
|
62
|
+
|
|
63
|
+
[Unstract developer documentation](https://docs.unstract.com/): Learn more about Unstract and its API.
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "unstract-client"
|
|
3
|
+
dynamic = []
|
|
4
|
+
description = "Python client for the Unstract LLM-powered structured data extraction platform"
|
|
5
|
+
authors = [
|
|
6
|
+
{ name = "Zipstack Inc", email = "devsupport@zipstack.com" },
|
|
7
|
+
]
|
|
8
|
+
dependencies = [
|
|
9
|
+
"requests>=2.32.3",
|
|
10
|
+
]
|
|
11
|
+
requires-python = ">=3.11"
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
keywords = [
|
|
14
|
+
"unstract cli apps sdk",
|
|
15
|
+
]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 4 - Beta",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"License :: OSI Approved :: GNU Affero General Public License v3",
|
|
20
|
+
"Operating System :: POSIX :: Linux",
|
|
21
|
+
"Operating System :: MacOS :: MacOS X",
|
|
22
|
+
"Operating System :: Microsoft :: Windows",
|
|
23
|
+
"Programming Language :: Python",
|
|
24
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
25
|
+
]
|
|
26
|
+
version = "0.1.0"
|
|
27
|
+
|
|
28
|
+
[project.urls]
|
|
29
|
+
Homepage = "https://unstract.com"
|
|
30
|
+
Source = "https://github.com/Zipstack/unstract-python-client"
|
|
31
|
+
|
|
32
|
+
[project.license]
|
|
33
|
+
text = "MIT"
|
|
34
|
+
|
|
35
|
+
[tool.pdm.dev-dependencies]
|
|
36
|
+
test = [
|
|
37
|
+
"pytest>=8.2.2",
|
|
38
|
+
"pytest-mock>=3.14.0",
|
|
39
|
+
"pytest-dotenv>=0.5.2",
|
|
40
|
+
"pytest-cov>=5.0.0",
|
|
41
|
+
"pytest-md-report>=0.6.2",
|
|
42
|
+
]
|
|
43
|
+
lint = [
|
|
44
|
+
"autopep8~=2.0.2",
|
|
45
|
+
"black~=23.3.0",
|
|
46
|
+
"docutils~=0.20.1",
|
|
47
|
+
"flake8~=6.0.0",
|
|
48
|
+
"flake8-pyproject~=1.2.2",
|
|
49
|
+
"isort~=5.12.0",
|
|
50
|
+
"pre-commit~=3.3.1",
|
|
51
|
+
"yamllint>=1.35.1",
|
|
52
|
+
"mypy~=1.10.0",
|
|
53
|
+
]
|
|
54
|
+
|
|
55
|
+
[tool.pdm.version]
|
|
56
|
+
source = "file"
|
|
57
|
+
path = "src/unstract/api_deployments/__init__.py"
|
|
58
|
+
|
|
59
|
+
[tool.pdm.build]
|
|
60
|
+
includes = [
|
|
61
|
+
"src",
|
|
62
|
+
]
|
|
63
|
+
package-dir = "src"
|
|
64
|
+
|
|
65
|
+
[tool.pdm.scripts.test]
|
|
66
|
+
cmd = "pytest -s -v"
|
|
67
|
+
env_file = "tests/.env"
|
|
68
|
+
help = "Runs pytests for Unstract client"
|
|
69
|
+
|
|
70
|
+
[tool.isort]
|
|
71
|
+
line_length = 120
|
|
72
|
+
multi_line_output = 3
|
|
73
|
+
include_trailing_comma = true
|
|
74
|
+
force_grid_wrap = 0
|
|
75
|
+
use_parentheses = true
|
|
76
|
+
ensure_newline_before_comments = true
|
|
77
|
+
profile = "black"
|
|
78
|
+
|
|
79
|
+
[tool.flake8]
|
|
80
|
+
max-line-length = 120
|
|
81
|
+
|
|
82
|
+
[tool.pytest.ini_options]
|
|
83
|
+
env_files = [
|
|
84
|
+
"tests/.env",
|
|
85
|
+
]
|
|
86
|
+
addopts = "-s"
|
|
87
|
+
log_level = "INFO"
|
|
88
|
+
log_cli = true
|
|
89
|
+
|
|
90
|
+
[build-system]
|
|
91
|
+
requires = [
|
|
92
|
+
"pdm-backend",
|
|
93
|
+
]
|
|
94
|
+
build-backend = "pdm.backend"
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This module provides an API client to invoke APIs deployed on the Unstract platform.
|
|
3
|
+
|
|
4
|
+
Classes:
|
|
5
|
+
APIDeploymentsClient: A class to invoke APIs deployed on the Unstract platform.
|
|
6
|
+
APIDeploymentsClientException: A class to handle exceptions raised by the APIDeploymentsClient class.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import logging
|
|
10
|
+
import ntpath
|
|
11
|
+
import os
|
|
12
|
+
from urllib.parse import parse_qs, urlparse
|
|
13
|
+
|
|
14
|
+
import requests
|
|
15
|
+
|
|
16
|
+
from unstract.api_deployments.utils import UnstractUtils
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class APIDeploymentsClientException(Exception):
|
|
20
|
+
"""
|
|
21
|
+
A class to handle exceptions raised by the APIClient class.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
def __init__(self, message):
|
|
25
|
+
def __init__(self, value):
|
|
26
|
+
self.value = value
|
|
27
|
+
|
|
28
|
+
def __str__(self):
|
|
29
|
+
return repr(self.value)
|
|
30
|
+
|
|
31
|
+
def error_message(self):
|
|
32
|
+
return self.value
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class APIDeploymentsClient:
|
|
36
|
+
"""
|
|
37
|
+
A class to invoke APIs deployed on the Unstract platform.
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
formatter = logging.Formatter(
|
|
41
|
+
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
|
42
|
+
)
|
|
43
|
+
logger = logging.getLogger(__name__)
|
|
44
|
+
log_stream_handler = logging.StreamHandler()
|
|
45
|
+
log_stream_handler.setFormatter(formatter)
|
|
46
|
+
logger.addHandler(log_stream_handler)
|
|
47
|
+
|
|
48
|
+
api_key = ""
|
|
49
|
+
api_timeout = 300
|
|
50
|
+
|
|
51
|
+
def __init__(
|
|
52
|
+
self,
|
|
53
|
+
api_url: str,
|
|
54
|
+
api_key: str,
|
|
55
|
+
api_timeout: int = 300,
|
|
56
|
+
logging_level: str = "INFO",
|
|
57
|
+
):
|
|
58
|
+
"""
|
|
59
|
+
Initializes the APIClient class.
|
|
60
|
+
|
|
61
|
+
Args:
|
|
62
|
+
api_key (str): The API key to authenticate the API request.
|
|
63
|
+
api_timeout (int): The timeout to wait for the API response.
|
|
64
|
+
logging_level (str): The logging level to log messages.
|
|
65
|
+
"""
|
|
66
|
+
if logging_level == "":
|
|
67
|
+
logging_level = os.getenv("UNSTRACT_API_CLIENT_LOGGING_LEVEL", "INFO")
|
|
68
|
+
if logging_level == "DEBUG":
|
|
69
|
+
self.logger.setLevel(logging.DEBUG)
|
|
70
|
+
elif logging_level == "INFO":
|
|
71
|
+
self.logger.setLevel(logging.INFO)
|
|
72
|
+
elif logging_level == "WARNING":
|
|
73
|
+
self.logger.setLevel(logging.WARNING)
|
|
74
|
+
elif logging_level == "ERROR":
|
|
75
|
+
self.logger.setLevel(logging.ERROR)
|
|
76
|
+
|
|
77
|
+
# self.logger.setLevel(logging_level)
|
|
78
|
+
self.logger.debug("Logging level set to: " + logging_level)
|
|
79
|
+
|
|
80
|
+
if api_key == "":
|
|
81
|
+
self.api_key = os.getenv("UNSTRACT_API_DEPLOYMENT_KEY", "")
|
|
82
|
+
else:
|
|
83
|
+
self.api_key = api_key
|
|
84
|
+
self.logger.debug("API key set to: " + UnstractUtils.redact_key(self.api_key))
|
|
85
|
+
|
|
86
|
+
self.api_timeout = api_timeout
|
|
87
|
+
self.api_url = api_url
|
|
88
|
+
self.__save_base_url(api_url)
|
|
89
|
+
|
|
90
|
+
def __save_base_url(self, full_url: str):
|
|
91
|
+
"""
|
|
92
|
+
Extracts the base URL from the full URL and saves it.
|
|
93
|
+
|
|
94
|
+
Args:
|
|
95
|
+
full_url (str): The full URL of the API.
|
|
96
|
+
"""
|
|
97
|
+
parsed_url = urlparse(full_url)
|
|
98
|
+
self.base_url = parsed_url.scheme + "://" + parsed_url.netloc
|
|
99
|
+
self.logger.debug("Base URL: " + self.base_url)
|
|
100
|
+
|
|
101
|
+
def structure_file(self, file_paths: list[str]) -> dict:
|
|
102
|
+
"""
|
|
103
|
+
Invokes the API deployed on the Unstract platform.
|
|
104
|
+
|
|
105
|
+
Args:
|
|
106
|
+
file_paths (list[str]): The file path to the file to be uploaded.
|
|
107
|
+
|
|
108
|
+
Returns:
|
|
109
|
+
dict: The response from the API.
|
|
110
|
+
"""
|
|
111
|
+
self.logger.debug("Invoking API: " + self.api_url)
|
|
112
|
+
self.logger.debug("File paths: " + str(file_paths))
|
|
113
|
+
|
|
114
|
+
headers = {
|
|
115
|
+
"Authorization": "Bearer " + self.api_key,
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
data = {"timeout": self.api_timeout}
|
|
119
|
+
|
|
120
|
+
files = []
|
|
121
|
+
|
|
122
|
+
try:
|
|
123
|
+
for file_path in file_paths:
|
|
124
|
+
record = (
|
|
125
|
+
"files",
|
|
126
|
+
(
|
|
127
|
+
ntpath.basename(file_path),
|
|
128
|
+
open(file_path, "rb"),
|
|
129
|
+
"application/octet-stream",
|
|
130
|
+
),
|
|
131
|
+
)
|
|
132
|
+
files.append(record)
|
|
133
|
+
except FileNotFoundError as e:
|
|
134
|
+
raise APIDeploymentsClientException("File not found: " + str(e))
|
|
135
|
+
|
|
136
|
+
response = requests.post(
|
|
137
|
+
self.api_url,
|
|
138
|
+
headers=headers,
|
|
139
|
+
data=data,
|
|
140
|
+
files=files,
|
|
141
|
+
)
|
|
142
|
+
self.logger.debug(response.status_code)
|
|
143
|
+
self.logger.debug(response.text)
|
|
144
|
+
# The returned object is wrapped in a "message" key. Let's simplify the response.
|
|
145
|
+
obj_to_return = {}
|
|
146
|
+
|
|
147
|
+
if response.status_code == 401:
|
|
148
|
+
obj_to_return = {
|
|
149
|
+
"pending": False,
|
|
150
|
+
"execution_status": "",
|
|
151
|
+
"error": response.json()["errors"][0]["detail"],
|
|
152
|
+
"extraction_result": "",
|
|
153
|
+
"status_code": response.status_code,
|
|
154
|
+
}
|
|
155
|
+
return obj_to_return
|
|
156
|
+
|
|
157
|
+
# If the execution status is pending, extract the execution ID from the response
|
|
158
|
+
# and return it in the response. Later, users can use the execution ID to check the status of the execution.
|
|
159
|
+
# The returned object is wrapped in a "message" key. Let's simplify the response.
|
|
160
|
+
obj_to_return = {
|
|
161
|
+
"pending": False,
|
|
162
|
+
"execution_status": response.json()["message"]["execution_status"],
|
|
163
|
+
"error": response.json()["message"]["error"],
|
|
164
|
+
"extraction_result": response.json()["message"]["result"],
|
|
165
|
+
"status_code": response.status_code,
|
|
166
|
+
}
|
|
167
|
+
if (
|
|
168
|
+
200 <= response.status_code < 300
|
|
169
|
+
and obj_to_return["execution_status"] == "PENDING"
|
|
170
|
+
):
|
|
171
|
+
|
|
172
|
+
obj_to_return["status_check_api_endpoint"] = response.json()["message"][
|
|
173
|
+
"status_api"
|
|
174
|
+
]
|
|
175
|
+
obj_to_return["pending"] = True
|
|
176
|
+
|
|
177
|
+
return obj_to_return
|
|
178
|
+
|
|
179
|
+
def check_execution_status(self, status_check_api_endpoint: str) -> dict:
|
|
180
|
+
"""
|
|
181
|
+
Checks the status of the execution.
|
|
182
|
+
|
|
183
|
+
Args:
|
|
184
|
+
status_check_api_endpoint (str): The API endpoint to check the status of the execution.
|
|
185
|
+
|
|
186
|
+
Returns:
|
|
187
|
+
dict: The response from the API.
|
|
188
|
+
"""
|
|
189
|
+
|
|
190
|
+
headers = {
|
|
191
|
+
"Authorization": "Bearer " + self.api_key,
|
|
192
|
+
}
|
|
193
|
+
status_call_url = self.base_url + status_check_api_endpoint
|
|
194
|
+
self.logger.debug("Checking execution status via endpoint: " + status_call_url)
|
|
195
|
+
response = requests.get(
|
|
196
|
+
status_call_url,
|
|
197
|
+
headers=headers,
|
|
198
|
+
)
|
|
199
|
+
self.logger.debug(response.status_code)
|
|
200
|
+
self.logger.debug(response.text)
|
|
201
|
+
obj_to_return = {
|
|
202
|
+
"pending": False,
|
|
203
|
+
"execution_status": response.json()["status"],
|
|
204
|
+
"status_code": response.status_code,
|
|
205
|
+
"message": response.json()["message"],
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
# If the execution status is pending, extract the execution ID from the response
|
|
209
|
+
# and return it in the response. Later, users can use the execution ID to check the status of the execution.
|
|
210
|
+
if (
|
|
211
|
+
200 <= response.status_code < 500
|
|
212
|
+
and obj_to_return["execution_status"] == "PENDING"
|
|
213
|
+
):
|
|
214
|
+
obj_to_return["pending"] = True
|
|
215
|
+
|
|
216
|
+
if (
|
|
217
|
+
200 <= response.status_code < 300
|
|
218
|
+
and obj_to_return["execution_status"] == "SUCCESS"
|
|
219
|
+
):
|
|
220
|
+
obj_to_return["extraction_result"] = response.json()["message"]
|
|
221
|
+
|
|
222
|
+
return obj_to_return
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
class UnstractUtils:
|
|
2
|
+
@staticmethod
|
|
3
|
+
def redact_key(api_key: str, reveal_length=4) -> str:
|
|
4
|
+
"""Hides sensitive information partially. Useful for logging keys.
|
|
5
|
+
|
|
6
|
+
Args:
|
|
7
|
+
api_key (str): API key to redact
|
|
8
|
+
reveal_length (int): Number of characters to reveal from the start of the key
|
|
9
|
+
|
|
10
|
+
Returns:
|
|
11
|
+
str: Redacted API key
|
|
12
|
+
"""
|
|
13
|
+
if not isinstance(api_key, str):
|
|
14
|
+
raise ValueError("API key must be a string")
|
|
15
|
+
|
|
16
|
+
if reveal_length < 0:
|
|
17
|
+
raise ValueError("Reveal length must be a non-negative integer")
|
|
18
|
+
|
|
19
|
+
redacted_length = max(len(api_key) - reveal_length, 0)
|
|
20
|
+
revealed_part = api_key[:reveal_length]
|
|
21
|
+
redacted_part = "x" * redacted_length
|
|
22
|
+
return revealed_part + redacted_part
|
|
File without changes
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import time
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
from unstract.api_deployments.client import (
|
|
5
|
+
APIDeploymentsClient,
|
|
6
|
+
APIDeploymentsClientException,
|
|
7
|
+
)
|
|
8
|
+
import dotenv
|
|
9
|
+
|
|
10
|
+
dotenv.load_dotenv()
|
|
11
|
+
|
|
12
|
+
def main():
|
|
13
|
+
try:
|
|
14
|
+
adc = APIDeploymentsClient(
|
|
15
|
+
api_url=os.getenv("API_URL"),
|
|
16
|
+
api_key=os.getenv("UNSTRACT_API_DEPLOYMENT_KEY"),
|
|
17
|
+
api_timeout=10,
|
|
18
|
+
logging_level="DEBUG",
|
|
19
|
+
)
|
|
20
|
+
# Replace files with pdfs
|
|
21
|
+
response = adc.structure_file(
|
|
22
|
+
["<files>"]
|
|
23
|
+
)
|
|
24
|
+
print(response)
|
|
25
|
+
if response["pending"]:
|
|
26
|
+
while True:
|
|
27
|
+
p_response = adc.check_execution_status(
|
|
28
|
+
response["status_check_api_endpoint"]
|
|
29
|
+
)
|
|
30
|
+
print(p_response)
|
|
31
|
+
if not p_response["pending"]:
|
|
32
|
+
break
|
|
33
|
+
print("Sleeping and checking again in 5 seconds..")
|
|
34
|
+
time.sleep(5)
|
|
35
|
+
except APIDeploymentsClientException as e:
|
|
36
|
+
print(e)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
if __name__ == "__main__":
|
|
40
|
+
main()
|