sparrow-parse 0.4.3__py3-none-any.whl → 0.4.5__py3-none-any.whl
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/__init__.py +1 -1
- sparrow_parse/extractors/vllm_extractor.py +31 -32
- sparrow_parse/vllm/mlx_inference.py +77 -41
- {sparrow_parse-0.4.3.dist-info → sparrow_parse-0.4.5.dist-info}/METADATA +1 -1
- {sparrow_parse-0.4.3.dist-info → sparrow_parse-0.4.5.dist-info}/RECORD +8 -8
- {sparrow_parse-0.4.3.dist-info → sparrow_parse-0.4.5.dist-info}/WHEEL +0 -0
- {sparrow_parse-0.4.3.dist-info → sparrow_parse-0.4.5.dist-info}/entry_points.txt +0 -0
- {sparrow_parse-0.4.3.dist-info → sparrow_parse-0.4.5.dist-info}/top_level.txt +0 -0
sparrow_parse/__init__.py
CHANGED
@@ -1 +1 @@
|
|
1
|
-
__version__ = '0.4.
|
1
|
+
__version__ = '0.4.5'
|
@@ -7,7 +7,6 @@ from rich import print
|
|
7
7
|
import os
|
8
8
|
import tempfile
|
9
9
|
import shutil
|
10
|
-
from typing import Any, Dict, List, Union
|
11
10
|
|
12
11
|
|
13
12
|
class VLLMExtractor(object):
|
@@ -153,34 +152,34 @@ if __name__ == "__main__":
|
|
153
152
|
|
154
153
|
extractor = VLLMExtractor()
|
155
154
|
|
156
|
-
#
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
#
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
#
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
155
|
+
# export HF_TOKEN="hf_"
|
156
|
+
config = {
|
157
|
+
"method": "mlx", # Could be 'huggingface', 'mlx' or 'local_gpu'
|
158
|
+
"model_name": "mlx-community/Qwen2-VL-7B-Instruct-8bit",
|
159
|
+
# "hf_space": "katanaml/sparrow-qwen2-vl-7b",
|
160
|
+
# "hf_token": os.getenv('HF_TOKEN'),
|
161
|
+
# Additional fields for local GPU inference
|
162
|
+
# "device": "cuda", "model_path": "model.pth"
|
163
|
+
}
|
164
|
+
|
165
|
+
# Use the factory to get the correct instance
|
166
|
+
factory = InferenceFactory(config)
|
167
|
+
model_inference_instance = factory.get_inference_instance()
|
168
|
+
|
169
|
+
input_data = [
|
170
|
+
{
|
171
|
+
"file_path": "/Users/andrejb/Work/katana-git/sparrow/sparrow-ml/llm/data/invoice_1.jpg",
|
172
|
+
"text_input": "retrieve document data. return response in JSON format"
|
173
|
+
}
|
174
|
+
]
|
175
|
+
|
176
|
+
# Now you can run inference without knowing which implementation is used
|
177
|
+
results_array, num_pages = extractor.run_inference(model_inference_instance, input_data, tables_only=True,
|
178
|
+
generic_query=False,
|
179
|
+
debug_dir="/Users/andrejb/Work/katana-git/sparrow/sparrow-ml/llm/data/",
|
180
|
+
debug=True,
|
181
|
+
mode=None)
|
182
|
+
|
183
|
+
for i, result in enumerate(results_array):
|
184
|
+
print(f"Result for page {i + 1}:", result)
|
185
|
+
print(f"Number of pages: {num_pages}")
|
@@ -4,6 +4,7 @@ from mlx_vlm.utils import load_image
|
|
4
4
|
from sparrow_parse.vllm.inference_base import ModelInference
|
5
5
|
import os
|
6
6
|
import json
|
7
|
+
import gc
|
7
8
|
|
8
9
|
|
9
10
|
class MLXInference(ModelInference):
|
@@ -19,18 +20,47 @@ class MLXInference(ModelInference):
|
|
19
20
|
:param model_name: Name of the model to load.
|
20
21
|
"""
|
21
22
|
self.model_name = model_name
|
23
|
+
self.model = None
|
24
|
+
self.processor = None
|
22
25
|
print(f"MLXInference initialized with model: {model_name}")
|
23
26
|
|
24
27
|
|
25
|
-
|
26
|
-
def _load_model_and_processor(model_name):
|
28
|
+
def __del__(self):
|
27
29
|
"""
|
28
|
-
|
30
|
+
Destructor to clean up resources when the object is deleted.
|
31
|
+
"""
|
32
|
+
self.unload_model()
|
33
|
+
|
34
|
+
def unload_model(self):
|
35
|
+
"""
|
36
|
+
Unload the model and release resources.
|
37
|
+
"""
|
38
|
+
if self.model:
|
39
|
+
print(f"Unloading model: {self.model_name}")
|
40
|
+
del self.model
|
41
|
+
self.model = None
|
42
|
+
if self.processor:
|
43
|
+
print(f"Unloading processor for model: {self.model_name}")
|
44
|
+
del self.processor
|
45
|
+
self.processor = None
|
46
|
+
|
47
|
+
# Force garbage collection to release memory
|
48
|
+
gc.collect()
|
49
|
+
print(f"Model {self.model_name} and its resources have been unloaded, memory cleared.")
|
29
50
|
|
51
|
+
|
52
|
+
def _load_model_and_processor(self, model_name):
|
53
|
+
"""
|
54
|
+
Load the model and processor for inference.
|
30
55
|
:param model_name: Name of the model to load.
|
31
56
|
:return: Tuple containing the loaded model and processor.
|
32
57
|
"""
|
33
|
-
|
58
|
+
print(f"Loading model and processor for: {model_name}...")
|
59
|
+
model, processor = load(model_name)
|
60
|
+
self.model = model # Store model instance
|
61
|
+
self.processor = processor # Store processor instance
|
62
|
+
print(f"Model and processor for '{model_name}' loaded successfully.")
|
63
|
+
return model, processor
|
34
64
|
|
35
65
|
|
36
66
|
def process_response(self, output_text):
|
@@ -79,48 +109,54 @@ class MLXInference(ModelInference):
|
|
79
109
|
def inference(self, input_data, mode=None):
|
80
110
|
"""
|
81
111
|
Perform inference on input data using the specified model.
|
82
|
-
|
83
112
|
:param input_data: A list of dictionaries containing image file paths and text inputs.
|
84
113
|
:param mode: Optional mode for inference ("static" for simple JSON output).
|
85
114
|
:return: List of processed model responses.
|
86
115
|
"""
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
116
|
+
try:
|
117
|
+
if mode == "static":
|
118
|
+
return [self.get_simple_json()]
|
119
|
+
|
120
|
+
# Load the model and processor
|
121
|
+
model, processor = self._load_model_and_processor(self.model_name)
|
122
|
+
config = model.config
|
123
|
+
|
124
|
+
# Prepare absolute file paths
|
125
|
+
file_paths = self._extract_file_paths(input_data)
|
126
|
+
|
127
|
+
results = []
|
128
|
+
for file_path in file_paths:
|
129
|
+
image, width, height = self.load_image_data(file_path)
|
130
|
+
|
131
|
+
# Prepare messages for the chat model
|
132
|
+
messages = [
|
133
|
+
{"role": "system",
|
134
|
+
"content": "You are an expert at extracting structured text from image documents."},
|
135
|
+
{"role": "user", "content": input_data[0]["text_input"]},
|
136
|
+
]
|
137
|
+
|
138
|
+
# Generate and process response
|
139
|
+
prompt = apply_chat_template(processor, config, messages)
|
140
|
+
response = generate(
|
141
|
+
model,
|
142
|
+
processor,
|
143
|
+
image,
|
144
|
+
prompt,
|
145
|
+
resize_shape=(width, height),
|
146
|
+
max_tokens=4000,
|
147
|
+
temperature=0.0,
|
148
|
+
verbose=False
|
149
|
+
)
|
150
|
+
results.append(self.process_response(response))
|
151
|
+
|
152
|
+
print("Inference completed successfully for: ", file_path)
|
153
|
+
|
154
|
+
return results
|
155
|
+
|
156
|
+
finally:
|
157
|
+
# Always unload the model after inference
|
158
|
+
self.unload_model()
|
122
159
|
|
123
|
-
return results
|
124
160
|
|
125
161
|
@staticmethod
|
126
162
|
def _extract_file_paths(input_data):
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: sparrow-parse
|
3
|
-
Version: 0.4.
|
3
|
+
Version: 0.4.5
|
4
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
|
@@ -1,7 +1,7 @@
|
|
1
|
-
sparrow_parse/__init__.py,sha256=
|
1
|
+
sparrow_parse/__init__.py,sha256=aYJpblaKx7C4NugypZ2lZHBzsGsZoG5sUbcsPwgoi9s,21
|
2
2
|
sparrow_parse/__main__.py,sha256=Xs1bpJV0n08KWOoQE34FBYn6EBXZA9HIYJKrE4ZdG78,153
|
3
3
|
sparrow_parse/extractors/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
4
|
-
sparrow_parse/extractors/vllm_extractor.py,sha256=
|
4
|
+
sparrow_parse/extractors/vllm_extractor.py,sha256=G4kQh0GoZ4V4TdyeDwZWFkOG15MQCQMvSf2UbFQDWeI,7746
|
5
5
|
sparrow_parse/helpers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
6
6
|
sparrow_parse/helpers/pdf_optimizer.py,sha256=GIqQYWtixFeZGCRFXL0lQfQByapCDuQzzRHAkzcPwLE,3302
|
7
7
|
sparrow_parse/processors/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
@@ -11,9 +11,9 @@ sparrow_parse/vllm/huggingface_inference.py,sha256=EJnG6PesGKMc_0qGPN8ufE6pSnhAg
|
|
11
11
|
sparrow_parse/vllm/inference_base.py,sha256=4mwGoAY63MB4cHZpV0czTkJWEzimmiTzqqzKmLNzgjw,820
|
12
12
|
sparrow_parse/vllm/inference_factory.py,sha256=FTM65O-dW2WZchHOrNN7_Q3-FlVoAc65iSptuuUuClM,1166
|
13
13
|
sparrow_parse/vllm/local_gpu_inference.py,sha256=aHoJTejb5xrXjWDIGu5RBQWEyRCOBCB04sMvO2Wyvg8,628
|
14
|
-
sparrow_parse/vllm/mlx_inference.py,sha256=
|
15
|
-
sparrow_parse-0.4.
|
16
|
-
sparrow_parse-0.4.
|
17
|
-
sparrow_parse-0.4.
|
18
|
-
sparrow_parse-0.4.
|
19
|
-
sparrow_parse-0.4.
|
14
|
+
sparrow_parse/vllm/mlx_inference.py,sha256=0LP6tDeaID7Rtarcs_ffmK6I77e2xv5tjs4xyt0qxLo,6100
|
15
|
+
sparrow_parse-0.4.5.dist-info/METADATA,sha256=HbGmqb5Kq7BKVNUi5wE_w69LZMAYQXI34cpRN6pdWAs,6432
|
16
|
+
sparrow_parse-0.4.5.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
|
17
|
+
sparrow_parse-0.4.5.dist-info/entry_points.txt,sha256=8CrvTVTTcz1YuZ8aRCYNOH15ZOAaYLlcbYX3t28HwJY,54
|
18
|
+
sparrow_parse-0.4.5.dist-info/top_level.txt,sha256=n6b-WtT91zKLyCPZTP7wvne8v_yvIahcsz-4sX8I0rY,14
|
19
|
+
sparrow_parse-0.4.5.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|