shinseung428
commited on
Commit
•
cb40a1c
1
Parent(s):
d585e51
update scripts and script README
Browse files- scripts/README.md +2 -6
- scripts/infer_aws.py +4 -0
- scripts/infer_google.py +3 -3
- scripts/infer_llamaparse.py +3 -0
- scripts/infer_microsoft.py +6 -3
- scripts/infer_unstructured.py +9 -7
- scripts/infer_upstage.py +3 -0
scripts/README.md
CHANGED
@@ -99,13 +99,9 @@ $ python infer_microsoft.py \
|
|
99 |
|
100 |
To handle various document formats with Unstructured, follow the steps below:
|
101 |
```
|
102 |
-
$ pip install "unstructured
|
103 |
-
$ pip install poppler-utils
|
104 |
-
|
105 |
-
$ apt install tesseract-ocr libtesseract-dev
|
106 |
-
$ apt install tesseract-ocr-[lang] # use an appropriate language code
|
107 |
```
|
108 |
-
Detailed installation instructions can be found [here](https://unstructured
|
109 |
|
110 |
**Note:** To run the Unstructured inference code, you must set the `UNSTRUCTURED_API_KEY` and `UNSTRUCTURED_URL` variables.
|
111 |
|
|
|
99 |
|
100 |
To handle various document formats with Unstructured, follow the steps below:
|
101 |
```
|
102 |
+
$ pip install "unstructured-client"
|
|
|
|
|
|
|
|
|
103 |
```
|
104 |
+
Detailed installation instructions can be found [here](https://docs.unstructured.io/api-reference/api-services/sdk-python).
|
105 |
|
106 |
**Note:** To run the Unstructured inference code, you must set the `UNSTRUCTURED_API_KEY` and `UNSTRUCTURED_URL` variables.
|
107 |
|
scripts/infer_aws.py
CHANGED
@@ -36,6 +36,10 @@ class AWSInference:
|
|
36 |
AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY") or ""
|
37 |
AWS_REGION = os.getenv("AWS_REGION") or ""
|
38 |
AWS_S3_BUCKET_NAME = os.getenv("AWS_S3_BUCKET_NAME") or ""
|
|
|
|
|
|
|
|
|
39 |
self.client = boto3.client(
|
40 |
"textract",
|
41 |
region_name=AWS_REGION,
|
|
|
36 |
AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY") or ""
|
37 |
AWS_REGION = os.getenv("AWS_REGION") or ""
|
38 |
AWS_S3_BUCKET_NAME = os.getenv("AWS_S3_BUCKET_NAME") or ""
|
39 |
+
|
40 |
+
if not all([AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION, AWS_S3_BUCKET_NAME]):
|
41 |
+
raise ValueError("Please set the environment variables for AWS")
|
42 |
+
|
43 |
self.client = boto3.client(
|
44 |
"textract",
|
45 |
region_name=AWS_REGION,
|
scripts/infer_google.py
CHANGED
@@ -35,13 +35,13 @@ class GoogleInference:
|
|
35 |
input_formats (list, optional): the supported file formats.
|
36 |
"""
|
37 |
self.project_id = os.getenv("GOOGLE_PROJECT_ID") or ""
|
38 |
-
|
39 |
self.processor_id = os.getenv("GOOGLE_PROCESSOR_ID") or ""
|
40 |
-
|
41 |
self.location = os.getenv("GOOGLE_LOCATION") or ""
|
42 |
-
|
43 |
self.endpoint = os.getenv("GOOGLE_ENDPOINT") or ""
|
44 |
|
|
|
|
|
|
|
45 |
self.processor_version = "rc"
|
46 |
|
47 |
validate_json_save_path(save_path)
|
|
|
35 |
input_formats (list, optional): the supported file formats.
|
36 |
"""
|
37 |
self.project_id = os.getenv("GOOGLE_PROJECT_ID") or ""
|
|
|
38 |
self.processor_id = os.getenv("GOOGLE_PROCESSOR_ID") or ""
|
|
|
39 |
self.location = os.getenv("GOOGLE_LOCATION") or ""
|
|
|
40 |
self.endpoint = os.getenv("GOOGLE_ENDPOINT") or ""
|
41 |
|
42 |
+
if not all([self.project_id, self.processor_id, self.location, self.endpoint]):
|
43 |
+
raise ValueError("Please set the environment variables for Google Cloud")
|
44 |
+
|
45 |
self.processor_version = "rc"
|
46 |
|
47 |
validate_json_save_path(save_path)
|
scripts/infer_llamaparse.py
CHANGED
@@ -34,6 +34,9 @@ class LlamaParseInference:
|
|
34 |
self.post_url = os.getenv("LLAMAPARSE_POST_URL") or ""
|
35 |
self.get_url = os.getenv("LLAMAPARSE_GET_URL") or ""
|
36 |
|
|
|
|
|
|
|
37 |
self.headers = {
|
38 |
"Accept": "application/json",
|
39 |
"Authorization": f"Bearer {self.api_key}",
|
|
|
34 |
self.post_url = os.getenv("LLAMAPARSE_POST_URL") or ""
|
35 |
self.get_url = os.getenv("LLAMAPARSE_GET_URL") or ""
|
36 |
|
37 |
+
if not all([self.api_key, self.post_url, self.get_url]):
|
38 |
+
raise ValueError("Please set the environment variables for LlamaParse")
|
39 |
+
|
40 |
self.headers = {
|
41 |
"Accept": "application/json",
|
42 |
"Authorization": f"Bearer {self.api_key}",
|
scripts/infer_microsoft.py
CHANGED
@@ -32,11 +32,14 @@ class MicrosoftInference:
|
|
32 |
save_path (str): the json path to save the results
|
33 |
input_formats (list, optional): the supported file formats.
|
34 |
"""
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
37 |
|
38 |
self.document_analysis_client = DocumentAnalysisClient(
|
39 |
-
endpoint=
|
40 |
)
|
41 |
|
42 |
validate_json_save_path(save_path)
|
|
|
32 |
save_path (str): the json path to save the results
|
33 |
input_formats (list, optional): the supported file formats.
|
34 |
"""
|
35 |
+
MICROSOFT_API_KEY = os.getenv("MICROSOFT_API_KEY") or ""
|
36 |
+
MICROSOFT_ENDPOINT = os.getenv("MICROSOFT_ENDPOINT") or ""
|
37 |
+
|
38 |
+
if not all([MICROSOFT_API_KEY, MICROSOFT_ENDPOINT]):
|
39 |
+
raise ValueError("Please set the environment variables for Microsoft")
|
40 |
|
41 |
self.document_analysis_client = DocumentAnalysisClient(
|
42 |
+
endpoint=MICROSOFT_ENDPOINT, credential=AzureKeyCredential(MICROSOFT_API_KEY)
|
43 |
)
|
44 |
|
45 |
validate_json_save_path(save_path)
|
scripts/infer_unstructured.py
CHANGED
@@ -6,7 +6,6 @@ from pathlib import Path
|
|
6 |
|
7 |
import unstructured_client
|
8 |
from unstructured_client.models import operations, shared
|
9 |
-
from unstructured.staging.base import elements_from_dicts
|
10 |
|
11 |
from utils import read_file_paths, validate_json_save_path, load_json_file
|
12 |
|
@@ -45,6 +44,9 @@ class UnstructuredInference:
|
|
45 |
self.api_key = os.getenv("UNSTRUCTURED_API_KEY") or ""
|
46 |
self.url = os.getenv("UNSTRUCTURED_URL") or ""
|
47 |
|
|
|
|
|
|
|
48 |
self.languages = ["eng", "kor"]
|
49 |
self.get_coordinates = True
|
50 |
self.infer_table_structure = True
|
@@ -70,15 +72,15 @@ class UnstructuredInference:
|
|
70 |
|
71 |
id_counter = 0
|
72 |
for elem in output_data:
|
73 |
-
transcription = elem
|
74 |
-
category = CATEGORY_MAP.get(elem
|
75 |
-
if elem
|
76 |
continue
|
77 |
|
78 |
-
xy_coord = [{"x": x, "y": y} for x, y in elem
|
79 |
|
80 |
if category == "table":
|
81 |
-
transcription = elem
|
82 |
|
83 |
data_dict = {
|
84 |
"coordinates": xy_coord,
|
@@ -135,7 +137,7 @@ class UnstructuredInference:
|
|
135 |
|
136 |
try:
|
137 |
res = self.client.general.partition(request=req)
|
138 |
-
elements =
|
139 |
except Exception as e:
|
140 |
print(e)
|
141 |
print("Error processing document..")
|
|
|
6 |
|
7 |
import unstructured_client
|
8 |
from unstructured_client.models import operations, shared
|
|
|
9 |
|
10 |
from utils import read_file_paths, validate_json_save_path, load_json_file
|
11 |
|
|
|
44 |
self.api_key = os.getenv("UNSTRUCTURED_API_KEY") or ""
|
45 |
self.url = os.getenv("UNSTRUCTURED_URL") or ""
|
46 |
|
47 |
+
if not self.api_key or not self.url:
|
48 |
+
raise ValueError("Please set the environment variables for Unstructured")
|
49 |
+
|
50 |
self.languages = ["eng", "kor"]
|
51 |
self.get_coordinates = True
|
52 |
self.infer_table_structure = True
|
|
|
72 |
|
73 |
id_counter = 0
|
74 |
for elem in output_data:
|
75 |
+
transcription = elem["text"]
|
76 |
+
category = CATEGORY_MAP.get(elem["type"], "paragraph")
|
77 |
+
if elem["metadata"]["coordinates"] is None:
|
78 |
continue
|
79 |
|
80 |
+
xy_coord = [{"x": x, "y": y} for x, y in elem["metadata"]["coordinates"]["points"]]
|
81 |
|
82 |
if category == "table":
|
83 |
+
transcription = elem["metadata"]["text_as_html"]
|
84 |
|
85 |
data_dict = {
|
86 |
"coordinates": xy_coord,
|
|
|
137 |
|
138 |
try:
|
139 |
res = self.client.general.partition(request=req)
|
140 |
+
elements = res.elements
|
141 |
except Exception as e:
|
142 |
print(e)
|
143 |
print("Error processing document..")
|
scripts/infer_upstage.py
CHANGED
@@ -27,6 +27,9 @@ class UpstageInference:
|
|
27 |
self.endpoint = os.getenv("UPSTAGE_ENDPOINT", "")
|
28 |
self.api_key = os.getenv("UPSTAGE_API_KEY", "")
|
29 |
|
|
|
|
|
|
|
30 |
validate_json_save_path(save_path)
|
31 |
self.save_path = save_path
|
32 |
self.processed_data = load_json_file(save_path)
|
|
|
27 |
self.endpoint = os.getenv("UPSTAGE_ENDPOINT", "")
|
28 |
self.api_key = os.getenv("UPSTAGE_API_KEY", "")
|
29 |
|
30 |
+
if not all([self.endpoint, self.api_key]):
|
31 |
+
raise ValueError("Please set the environment variables for Upstage")
|
32 |
+
|
33 |
validate_json_save_path(save_path)
|
34 |
self.save_path = save_path
|
35 |
self.processed_data = load_json_file(save_path)
|