sparrow-parse 0.3.4__tar.gz → 0.3.6__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.
- {sparrow-parse-0.3.4 → sparrow-parse-0.3.6}/PKG-INFO +22 -99
- {sparrow-parse-0.3.4 → sparrow-parse-0.3.6}/README.md +19 -96
- {sparrow-parse-0.3.4 → sparrow-parse-0.3.6}/setup.py +3 -3
- sparrow-parse-0.3.6/sparrow_parse/__init__.py +1 -0
- sparrow-parse-0.3.6/sparrow_parse/extractors/vllm_extractor.py +87 -0
- {sparrow-parse-0.3.4 → sparrow-parse-0.3.6}/sparrow_parse/helpers/pdf_optimizer.py +11 -6
- {sparrow-parse-0.3.4 → sparrow-parse-0.3.6}/sparrow_parse/vllm/huggingface_inference.py +6 -2
- sparrow-parse-0.3.6/sparrow_parse/vllm/inference_base.py +30 -0
- {sparrow-parse-0.3.4 → sparrow-parse-0.3.6}/sparrow_parse/vllm/local_gpu_inference.py +1 -1
- {sparrow-parse-0.3.4 → sparrow-parse-0.3.6}/sparrow_parse.egg-info/PKG-INFO +22 -99
- {sparrow-parse-0.3.4 → sparrow-parse-0.3.6}/sparrow_parse.egg-info/SOURCES.txt +0 -5
- {sparrow-parse-0.3.4 → sparrow-parse-0.3.6}/sparrow_parse.egg-info/requires.txt +1 -4
- sparrow-parse-0.3.4/sparrow_parse/__init__.py +0 -1
- sparrow-parse-0.3.4/sparrow_parse/extractors/html_extractor.py +0 -251
- sparrow-parse-0.3.4/sparrow_parse/extractors/vllm_extractor.py +0 -46
- sparrow-parse-0.3.4/sparrow_parse/helpers/html_extractor_helper.py +0 -374
- sparrow-parse-0.3.4/sparrow_parse/processors/markdown_processor.py +0 -137
- sparrow-parse-0.3.4/sparrow_parse/processors/unstructured_processor.py +0 -178
- sparrow-parse-0.3.4/sparrow_parse/temp.py +0 -27
- sparrow-parse-0.3.4/sparrow_parse/vllm/inference_base.py +0 -7
- {sparrow-parse-0.3.4 → sparrow-parse-0.3.6}/setup.cfg +0 -0
- {sparrow-parse-0.3.4 → sparrow-parse-0.3.6}/sparrow_parse/__main__.py +0 -0
- {sparrow-parse-0.3.4 → sparrow-parse-0.3.6}/sparrow_parse/extractors/__init__.py +0 -0
- {sparrow-parse-0.3.4 → sparrow-parse-0.3.6}/sparrow_parse/helpers/__init__.py +0 -0
- {sparrow-parse-0.3.4 → sparrow-parse-0.3.6}/sparrow_parse/processors/__init__.py +0 -0
- {sparrow-parse-0.3.4 → sparrow-parse-0.3.6}/sparrow_parse/processors/table_structure_processor.py +0 -0
- {sparrow-parse-0.3.4 → sparrow-parse-0.3.6}/sparrow_parse/vllm/__init__.py +0 -0
- {sparrow-parse-0.3.4 → sparrow-parse-0.3.6}/sparrow_parse/vllm/inference_factory.py +0 -0
- {sparrow-parse-0.3.4 → sparrow-parse-0.3.6}/sparrow_parse.egg-info/dependency_links.txt +0 -0
- {sparrow-parse-0.3.4 → sparrow-parse-0.3.6}/sparrow_parse.egg-info/entry_points.txt +0 -0
- {sparrow-parse-0.3.4 → sparrow-parse-0.3.6}/sparrow_parse.egg-info/top_level.txt +0 -0
@@ -1,14 +1,14 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: sparrow-parse
|
3
|
-
Version: 0.3.
|
4
|
-
Summary: Sparrow Parse is a Python package for parsing and extracting information from documents.
|
3
|
+
Version: 0.3.6
|
4
|
+
Summary: Sparrow Parse is a Python package (part of Sparrow) for parsing and extracting information from documents.
|
5
5
|
Home-page: https://github.com/katanaml/sparrow/tree/main/sparrow-data/parse
|
6
6
|
Author: Andrej Baranovskij
|
7
7
|
Author-email: andrejus.baranovskis@gmail.com
|
8
8
|
License: UNKNOWN
|
9
9
|
Project-URL: Homepage, https://github.com/katanaml/sparrow/tree/main/sparrow-data/parse
|
10
10
|
Project-URL: Repository, https://github.com/katanaml/sparrow
|
11
|
-
Keywords: llm,
|
11
|
+
Keywords: llm,vllm,ocr,vision
|
12
12
|
Platform: UNKNOWN
|
13
13
|
Classifier: Operating System :: OS Independent
|
14
14
|
Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
|
@@ -21,7 +21,7 @@ Description-Content-Type: text/markdown
|
|
21
21
|
|
22
22
|
## Description
|
23
23
|
|
24
|
-
This module implements Sparrow Parse [library](https://pypi.org/project/sparrow-parse/) with helpful methods for data pre-processing, parsing and extracting information.
|
24
|
+
This module implements Sparrow Parse [library](https://pypi.org/project/sparrow-parse/) library with helpful methods for data pre-processing, parsing and extracting information. This library relies on Visual LLM functionality, Table Transformers and is part of Sparrow. Check main [README](https://github.com/katanaml/sparrow)
|
25
25
|
|
26
26
|
## Install
|
27
27
|
|
@@ -29,101 +29,16 @@ This module implements Sparrow Parse [library](https://pypi.org/project/sparrow-
|
|
29
29
|
pip install sparrow-parse
|
30
30
|
```
|
31
31
|
|
32
|
-
## Pre-processing
|
33
|
-
|
34
|
-
### Unstructured
|
35
|
-
|
36
|
-
```
|
37
|
-
from sparrow_parse.extractor.unstructured_processor import UnstructuredProcessor
|
38
|
-
|
39
|
-
processor = UnstructuredProcessor()
|
40
|
-
|
41
|
-
content, table_content = processor.extract_data(
|
42
|
-
file_path, # file to process
|
43
|
-
strategy, # data processing strategy supported by unstructured
|
44
|
-
model_name, # model supported by unstructured
|
45
|
-
options, # table extraction into HTML format
|
46
|
-
local, # True if running from CLI, or False if running from FastAPI
|
47
|
-
debug) # Debug
|
48
|
-
```
|
49
|
-
|
50
|
-
Example:
|
51
|
-
|
52
|
-
*file_path* - `/Users/andrejb/infra/shared/katana-git/sparrow/sparrow-ml/llm/data/invoice_1.pdf`
|
53
|
-
|
54
|
-
*strategy* - `hi_res`
|
55
|
-
|
56
|
-
*model_name* - `yolox`
|
57
|
-
|
58
|
-
*options* - `['tables', 'unstructured']`
|
59
|
-
|
60
|
-
*local* - `True`
|
61
|
-
|
62
|
-
*debug* - `True`
|
63
|
-
|
64
|
-
### Markdown
|
65
|
-
|
66
|
-
```
|
67
|
-
from sparrow_parse.extractor.markdown_processor import MarkdownProcessor
|
68
|
-
|
69
|
-
processor = MarkdownProcessor()
|
70
|
-
|
71
|
-
content, table_content = processor.extract_data(
|
72
|
-
file_path, # file to process
|
73
|
-
options, # table extraction into HTML format
|
74
|
-
local, # True if running from CLI, or False if running from FastAPI
|
75
|
-
debug) # Debug
|
76
|
-
```
|
77
|
-
|
78
|
-
Example:
|
79
|
-
|
80
|
-
*file_path* - `/Users/andrejb/infra/shared/katana-git/sparrow/sparrow-ml/llm/data/invoice_1.pdf`
|
81
|
-
|
82
|
-
*options* - `['tables', 'markdown']`
|
83
|
-
|
84
|
-
*local* - `True`
|
85
|
-
|
86
|
-
*debug* - `True`
|
87
|
-
|
88
32
|
## Parsing and extraction
|
89
33
|
|
90
|
-
###
|
34
|
+
### Sparrow Parse VL (vision-language model) extractor with Hugging Face GPU infra
|
91
35
|
|
92
36
|
```
|
93
|
-
|
94
|
-
|
95
|
-
extractor = HTMLExtractor()
|
96
|
-
|
97
|
-
answer, targets_unprocessed = extractor.read_data(
|
98
|
-
target_columns, # list of table columns data to fetch
|
99
|
-
data, # list of HTML tables
|
100
|
-
column_keywords, # list of valid column names, can be empty. Useful to filter junk content
|
101
|
-
group_by_rows, # JSON result grouping
|
102
|
-
update_targets, # Set to true, if page contains multiple tables with the same columns
|
103
|
-
local, # True if running from CLI, or False if running from FastAPI
|
104
|
-
debug) # Debug
|
105
|
-
|
106
|
-
```
|
107
|
-
|
108
|
-
Example:
|
37
|
+
# run locally: python -m sparrow_parse.extractors.vllm_extractor
|
109
38
|
|
110
|
-
|
39
|
+
from sparrow_parse.vllm.inference_factory import InferenceFactory
|
40
|
+
from sparrow_parse.extractors.vllm_extractor import VLLMExtractor
|
111
41
|
|
112
|
-
*data* - `list of HTML tables`
|
113
|
-
|
114
|
-
*column_keywords* - `None`
|
115
|
-
|
116
|
-
*group_by_rows* - `True`
|
117
|
-
|
118
|
-
*update_targets* - `True`
|
119
|
-
|
120
|
-
*local* - `True`
|
121
|
-
|
122
|
-
*debug* - `True`
|
123
|
-
|
124
|
-
### Sparrow Parse VL (vision-language) extractor
|
125
|
-
|
126
|
-
```
|
127
42
|
extractor = VLLMExtractor()
|
128
43
|
|
129
44
|
# export HF_TOKEN="hf_"
|
@@ -141,17 +56,25 @@ model_inference_instance = factory.get_inference_instance()
|
|
141
56
|
|
142
57
|
input_data = [
|
143
58
|
{
|
144
|
-
"
|
145
|
-
"text_input": "retrieve
|
59
|
+
"file_path": "/data/oracle_10k_2014_q1_small.pdf",
|
60
|
+
"text_input": "retrieve {"table": [{"description": "str", "latest_amount": 0, "previous_amount": 0}]}. return response in JSON format"
|
146
61
|
}
|
147
62
|
]
|
148
63
|
|
149
64
|
# Now you can run inference without knowing which implementation is used
|
150
|
-
|
151
|
-
|
65
|
+
results_array, num_pages = extractor.run_inference(model_inference_instance, input_data, generic_query=False,
|
66
|
+
debug_dir="/data/",
|
67
|
+
debug=True,
|
68
|
+
mode="static")
|
69
|
+
|
70
|
+
for i, result in enumerate(results_array):
|
71
|
+
print(f"Result for page {i + 1}:", result)
|
72
|
+
print(f"Number of pages: {num_pages}")
|
152
73
|
```
|
153
74
|
|
154
|
-
|
75
|
+
Use `mode="static"` if you want to simulate LLM call, without executing LLM backend.
|
76
|
+
|
77
|
+
## PDF pre-processing
|
155
78
|
|
156
79
|
```
|
157
80
|
from sparrow_parse.extractor.pdf_optimizer import PDFOptimizer
|
@@ -166,7 +89,7 @@ num_pages, output_files, temp_dir = pdf_optimizer.split_pdf_to_pages(file_path,
|
|
166
89
|
|
167
90
|
Example:
|
168
91
|
|
169
|
-
*file_path* - `/
|
92
|
+
*file_path* - `/data/invoice_1.pdf`
|
170
93
|
|
171
94
|
*output_directory* - set to not `None`, for debug purposes only
|
172
95
|
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
## Description
|
4
4
|
|
5
|
-
This module implements Sparrow Parse [library](https://pypi.org/project/sparrow-parse/) with helpful methods for data pre-processing, parsing and extracting information.
|
5
|
+
This module implements Sparrow Parse [library](https://pypi.org/project/sparrow-parse/) library with helpful methods for data pre-processing, parsing and extracting information. This library relies on Visual LLM functionality, Table Transformers and is part of Sparrow. Check main [README](https://github.com/katanaml/sparrow)
|
6
6
|
|
7
7
|
## Install
|
8
8
|
|
@@ -10,101 +10,16 @@ This module implements Sparrow Parse [library](https://pypi.org/project/sparrow-
|
|
10
10
|
pip install sparrow-parse
|
11
11
|
```
|
12
12
|
|
13
|
-
## Pre-processing
|
14
|
-
|
15
|
-
### Unstructured
|
16
|
-
|
17
|
-
```
|
18
|
-
from sparrow_parse.extractor.unstructured_processor import UnstructuredProcessor
|
19
|
-
|
20
|
-
processor = UnstructuredProcessor()
|
21
|
-
|
22
|
-
content, table_content = processor.extract_data(
|
23
|
-
file_path, # file to process
|
24
|
-
strategy, # data processing strategy supported by unstructured
|
25
|
-
model_name, # model supported by unstructured
|
26
|
-
options, # table extraction into HTML format
|
27
|
-
local, # True if running from CLI, or False if running from FastAPI
|
28
|
-
debug) # Debug
|
29
|
-
```
|
30
|
-
|
31
|
-
Example:
|
32
|
-
|
33
|
-
*file_path* - `/Users/andrejb/infra/shared/katana-git/sparrow/sparrow-ml/llm/data/invoice_1.pdf`
|
34
|
-
|
35
|
-
*strategy* - `hi_res`
|
36
|
-
|
37
|
-
*model_name* - `yolox`
|
38
|
-
|
39
|
-
*options* - `['tables', 'unstructured']`
|
40
|
-
|
41
|
-
*local* - `True`
|
42
|
-
|
43
|
-
*debug* - `True`
|
44
|
-
|
45
|
-
### Markdown
|
46
|
-
|
47
|
-
```
|
48
|
-
from sparrow_parse.extractor.markdown_processor import MarkdownProcessor
|
49
|
-
|
50
|
-
processor = MarkdownProcessor()
|
51
|
-
|
52
|
-
content, table_content = processor.extract_data(
|
53
|
-
file_path, # file to process
|
54
|
-
options, # table extraction into HTML format
|
55
|
-
local, # True if running from CLI, or False if running from FastAPI
|
56
|
-
debug) # Debug
|
57
|
-
```
|
58
|
-
|
59
|
-
Example:
|
60
|
-
|
61
|
-
*file_path* - `/Users/andrejb/infra/shared/katana-git/sparrow/sparrow-ml/llm/data/invoice_1.pdf`
|
62
|
-
|
63
|
-
*options* - `['tables', 'markdown']`
|
64
|
-
|
65
|
-
*local* - `True`
|
66
|
-
|
67
|
-
*debug* - `True`
|
68
|
-
|
69
13
|
## Parsing and extraction
|
70
14
|
|
71
|
-
###
|
15
|
+
### Sparrow Parse VL (vision-language model) extractor with Hugging Face GPU infra
|
72
16
|
|
73
17
|
```
|
74
|
-
|
75
|
-
|
76
|
-
extractor = HTMLExtractor()
|
77
|
-
|
78
|
-
answer, targets_unprocessed = extractor.read_data(
|
79
|
-
target_columns, # list of table columns data to fetch
|
80
|
-
data, # list of HTML tables
|
81
|
-
column_keywords, # list of valid column names, can be empty. Useful to filter junk content
|
82
|
-
group_by_rows, # JSON result grouping
|
83
|
-
update_targets, # Set to true, if page contains multiple tables with the same columns
|
84
|
-
local, # True if running from CLI, or False if running from FastAPI
|
85
|
-
debug) # Debug
|
86
|
-
|
87
|
-
```
|
88
|
-
|
89
|
-
Example:
|
18
|
+
# run locally: python -m sparrow_parse.extractors.vllm_extractor
|
90
19
|
|
91
|
-
|
20
|
+
from sparrow_parse.vllm.inference_factory import InferenceFactory
|
21
|
+
from sparrow_parse.extractors.vllm_extractor import VLLMExtractor
|
92
22
|
|
93
|
-
*data* - `list of HTML tables`
|
94
|
-
|
95
|
-
*column_keywords* - `None`
|
96
|
-
|
97
|
-
*group_by_rows* - `True`
|
98
|
-
|
99
|
-
*update_targets* - `True`
|
100
|
-
|
101
|
-
*local* - `True`
|
102
|
-
|
103
|
-
*debug* - `True`
|
104
|
-
|
105
|
-
### Sparrow Parse VL (vision-language) extractor
|
106
|
-
|
107
|
-
```
|
108
23
|
extractor = VLLMExtractor()
|
109
24
|
|
110
25
|
# export HF_TOKEN="hf_"
|
@@ -122,17 +37,25 @@ model_inference_instance = factory.get_inference_instance()
|
|
122
37
|
|
123
38
|
input_data = [
|
124
39
|
{
|
125
|
-
"
|
126
|
-
"text_input": "retrieve
|
40
|
+
"file_path": "/data/oracle_10k_2014_q1_small.pdf",
|
41
|
+
"text_input": "retrieve {"table": [{"description": "str", "latest_amount": 0, "previous_amount": 0}]}. return response in JSON format"
|
127
42
|
}
|
128
43
|
]
|
129
44
|
|
130
45
|
# Now you can run inference without knowing which implementation is used
|
131
|
-
|
132
|
-
|
46
|
+
results_array, num_pages = extractor.run_inference(model_inference_instance, input_data, generic_query=False,
|
47
|
+
debug_dir="/data/",
|
48
|
+
debug=True,
|
49
|
+
mode="static")
|
50
|
+
|
51
|
+
for i, result in enumerate(results_array):
|
52
|
+
print(f"Result for page {i + 1}:", result)
|
53
|
+
print(f"Number of pages: {num_pages}")
|
133
54
|
```
|
134
55
|
|
135
|
-
|
56
|
+
Use `mode="static"` if you want to simulate LLM call, without executing LLM backend.
|
57
|
+
|
58
|
+
## PDF pre-processing
|
136
59
|
|
137
60
|
```
|
138
61
|
from sparrow_parse.extractor.pdf_optimizer import PDFOptimizer
|
@@ -147,7 +70,7 @@ num_pages, output_files, temp_dir = pdf_optimizer.split_pdf_to_pages(file_path,
|
|
147
70
|
|
148
71
|
Example:
|
149
72
|
|
150
|
-
*file_path* - `/
|
73
|
+
*file_path* - `/data/invoice_1.pdf`
|
151
74
|
|
152
75
|
*output_directory* - set to not `None`, for debug purposes only
|
153
76
|
|
@@ -8,10 +8,10 @@ with open("requirements.txt", "r", encoding="utf-8") as fh:
|
|
8
8
|
|
9
9
|
setup(
|
10
10
|
name="sparrow-parse",
|
11
|
-
version="0.3.
|
11
|
+
version="0.3.6",
|
12
12
|
author="Andrej Baranovskij",
|
13
13
|
author_email="andrejus.baranovskis@gmail.com",
|
14
|
-
description="Sparrow Parse is a Python package for parsing and extracting information from documents.",
|
14
|
+
description="Sparrow Parse is a Python package (part of Sparrow) for parsing and extracting information from documents.",
|
15
15
|
long_description=long_description,
|
16
16
|
long_description_content_type="text/markdown",
|
17
17
|
url="https://github.com/katanaml/sparrow/tree/main/sparrow-data/parse",
|
@@ -30,7 +30,7 @@ setup(
|
|
30
30
|
'sparrow-parse=sparrow_parse:main',
|
31
31
|
],
|
32
32
|
},
|
33
|
-
keywords="llm,
|
33
|
+
keywords="llm, vllm, ocr, vision",
|
34
34
|
packages=find_packages(),
|
35
35
|
python_requires='>=3.10',
|
36
36
|
install_requires=requirements,
|
@@ -0,0 +1 @@
|
|
1
|
+
__version__ = '0.3.6'
|
@@ -0,0 +1,87 @@
|
|
1
|
+
from sparrow_parse.vllm.inference_factory import InferenceFactory
|
2
|
+
from sparrow_parse.helpers.pdf_optimizer import PDFOptimizer
|
3
|
+
from rich import print
|
4
|
+
import os
|
5
|
+
import shutil
|
6
|
+
|
7
|
+
|
8
|
+
class VLLMExtractor(object):
|
9
|
+
def __init__(self):
|
10
|
+
pass
|
11
|
+
|
12
|
+
def run_inference(self,
|
13
|
+
model_inference_instance,
|
14
|
+
input_data,
|
15
|
+
generic_query=False,
|
16
|
+
debug_dir=None,
|
17
|
+
debug=False,
|
18
|
+
mode=None):
|
19
|
+
if generic_query:
|
20
|
+
input_data[0]["text_input"] = "retrieve document data. return response in JSON format"
|
21
|
+
|
22
|
+
if debug:
|
23
|
+
print("Input Data:", input_data)
|
24
|
+
|
25
|
+
results_array = []
|
26
|
+
|
27
|
+
if self.is_pdf(input_data[0]["file_path"]):
|
28
|
+
pdf_optimizer = PDFOptimizer()
|
29
|
+
num_pages, output_files, temp_dir = pdf_optimizer.split_pdf_to_pages(input_data[0]["file_path"],
|
30
|
+
debug_dir,
|
31
|
+
True)
|
32
|
+
|
33
|
+
# Run inference on each page
|
34
|
+
for page_num, output_file in enumerate(output_files):
|
35
|
+
input_data[0]["file_path"] = output_file
|
36
|
+
if debug:
|
37
|
+
print(f"Running inference on page {page_num + 1}...")
|
38
|
+
|
39
|
+
# Run inference on the page
|
40
|
+
result = model_inference_instance.inference(input_data, mode)
|
41
|
+
results_array.append(result)
|
42
|
+
|
43
|
+
shutil.rmtree(temp_dir, ignore_errors=True)
|
44
|
+
return results_array, num_pages
|
45
|
+
|
46
|
+
result = model_inference_instance.inference(input_data)
|
47
|
+
results_array.append(result)
|
48
|
+
|
49
|
+
return results_array, 1
|
50
|
+
|
51
|
+
def is_pdf(self, file_path):
|
52
|
+
return file_path.lower().endswith('.pdf')
|
53
|
+
|
54
|
+
if __name__ == "__main__":
|
55
|
+
# run locally: python -m sparrow_parse.extractors.vllm_extractor
|
56
|
+
|
57
|
+
extractor = VLLMExtractor()
|
58
|
+
|
59
|
+
# # export HF_TOKEN="hf_"
|
60
|
+
# config = {
|
61
|
+
# "method": "huggingface", # Could be 'huggingface' or 'local_gpu'
|
62
|
+
# "hf_space": "katanaml/sparrow-qwen2-vl-7b",
|
63
|
+
# "hf_token": os.getenv('HF_TOKEN'),
|
64
|
+
# # Additional fields for local GPU inference
|
65
|
+
# # "device": "cuda", "model_path": "model.pth"
|
66
|
+
# }
|
67
|
+
#
|
68
|
+
# # Use the factory to get the correct instance
|
69
|
+
# factory = InferenceFactory(config)
|
70
|
+
# model_inference_instance = factory.get_inference_instance()
|
71
|
+
#
|
72
|
+
# input_data = [
|
73
|
+
# {
|
74
|
+
# "file_path": "/Users/andrejb/infra/shared/katana-git/sparrow/sparrow-ml/llm/data/oracle_10k_2014_q1_small.pdf",
|
75
|
+
# "text_input": "retrieve {\"table\": [{\"description\": \"str\", \"latest_amount\": 0, \"previous_amount\": 0}]}. return response in JSON format"
|
76
|
+
# }
|
77
|
+
# ]
|
78
|
+
#
|
79
|
+
# # Now you can run inference without knowing which implementation is used
|
80
|
+
# results_array, num_pages = extractor.run_inference(model_inference_instance, input_data, generic_query=False,
|
81
|
+
# debug_dir="/Users/andrejb/infra/shared/katana-git/sparrow/sparrow-ml/llm/data/",
|
82
|
+
# debug=True,
|
83
|
+
# mode="static")
|
84
|
+
#
|
85
|
+
# for i, result in enumerate(results_array):
|
86
|
+
# print(f"Result for page {i + 1}:", result)
|
87
|
+
# print(f"Number of pages: {num_pages}")
|
@@ -40,17 +40,18 @@ class PDFOptimizer(object):
|
|
40
40
|
return number_of_pages, output_files, temp_dir
|
41
41
|
else:
|
42
42
|
# Convert the PDF to images
|
43
|
-
images = convert_from_path(file_path, dpi=
|
43
|
+
images = convert_from_path(file_path, dpi=300)
|
44
|
+
base_name = os.path.splitext(os.path.basename(file_path))[0]
|
44
45
|
|
45
46
|
# Save the images to the temporary directory
|
46
47
|
for i, image in enumerate(images):
|
47
|
-
output_filename = os.path.join(temp_dir, f'
|
48
|
+
output_filename = os.path.join(temp_dir, f'{base_name}_page_{i + 1}.jpg')
|
48
49
|
image.save(output_filename, 'JPEG')
|
49
50
|
output_files.append(output_filename)
|
50
51
|
|
51
52
|
if output_dir:
|
52
53
|
# Save each image to the debug folder
|
53
|
-
debug_output_filename = os.path.join(output_dir, f'
|
54
|
+
debug_output_filename = os.path.join(output_dir, f'{base_name}_page_{i + 1}.jpg')
|
54
55
|
image.save(debug_output_filename, 'JPEG')
|
55
56
|
|
56
57
|
# Return the number of pages, the list of file paths, and the temporary directory
|
@@ -60,13 +61,17 @@ class PDFOptimizer(object):
|
|
60
61
|
if __name__ == "__main__":
|
61
62
|
pdf_optimizer = PDFOptimizer()
|
62
63
|
|
63
|
-
# output_directory = "/Users/andrejb/
|
64
|
+
# output_directory = "/Users/andrejb/infra/shared/katana-git/sparrow/sparrow-ml/llm/data/"
|
64
65
|
# # Ensure the output directory exists
|
65
66
|
# os.makedirs(output_directory, exist_ok=True)
|
66
67
|
#
|
67
68
|
# # Split the optimized PDF into separate pages
|
68
|
-
# num_pages, output_files, temp_dir = pdf_optimizer.split_pdf_to_pages("/Users/andrejb/
|
69
|
+
# num_pages, output_files, temp_dir = pdf_optimizer.split_pdf_to_pages("/Users/andrejb/infra/shared/katana-git/sparrow/sparrow-ml/llm/data/oracle_10k_2014_q1_small.pdf",
|
69
70
|
# output_directory,
|
70
|
-
#
|
71
|
+
# True)
|
72
|
+
#
|
73
|
+
# print(f"Number of pages: {num_pages}")
|
74
|
+
# print(f"Output files: {output_files}")
|
75
|
+
# print(f"Temporary directory: {temp_dir}")
|
71
76
|
#
|
72
77
|
# shutil.rmtree(temp_dir, ignore_errors=True)
|
@@ -24,11 +24,15 @@ class HuggingFaceInference(ModelInference):
|
|
24
24
|
return output_text
|
25
25
|
|
26
26
|
|
27
|
-
def inference(self, input_data):
|
27
|
+
def inference(self, input_data, mode=None):
|
28
|
+
if mode == "static":
|
29
|
+
simple_json = self.get_simple_json()
|
30
|
+
return simple_json
|
31
|
+
|
28
32
|
client = Client(self.hf_space, hf_token=self.hf_token)
|
29
33
|
|
30
34
|
result = client.predict(
|
31
|
-
image=handle_file(input_data[0]["
|
35
|
+
image=handle_file(input_data[0]["file_path"]),
|
32
36
|
text_input=input_data[0]["text_input"],
|
33
37
|
api_name="/run_inference"
|
34
38
|
)
|
@@ -0,0 +1,30 @@
|
|
1
|
+
from abc import ABC, abstractmethod
|
2
|
+
import json
|
3
|
+
|
4
|
+
|
5
|
+
class ModelInference(ABC):
|
6
|
+
@abstractmethod
|
7
|
+
def inference(self, input_data, mode=None):
|
8
|
+
"""This method should be implemented by subclasses."""
|
9
|
+
pass
|
10
|
+
|
11
|
+
def get_simple_json(self):
|
12
|
+
# Define a simple data structure
|
13
|
+
data = {
|
14
|
+
"table": [
|
15
|
+
{
|
16
|
+
"description": "Revenues",
|
17
|
+
"latest_amount": 12453,
|
18
|
+
"previous_amount": 11445
|
19
|
+
},
|
20
|
+
{
|
21
|
+
"description": "Operating expenses",
|
22
|
+
"latest_amount": 9157,
|
23
|
+
"previous_amount": 8822
|
24
|
+
}
|
25
|
+
]
|
26
|
+
}
|
27
|
+
|
28
|
+
# Convert the dictionary to a JSON string
|
29
|
+
json_data = json.dumps(data, indent=4)
|
30
|
+
return json_data
|
@@ -8,7 +8,7 @@ class LocalGPUInference(ModelInference):
|
|
8
8
|
self.device = device
|
9
9
|
self.model.to(self.device)
|
10
10
|
|
11
|
-
def inference(self, input_data):
|
11
|
+
def inference(self, input_data, mode=None):
|
12
12
|
self.model.eval() # Set the model to evaluation mode
|
13
13
|
with torch.no_grad(): # No need to calculate gradients
|
14
14
|
input_tensor = torch.tensor(input_data).to(self.device)
|