sparrow-parse 0.4.2__py3-none-any.whl → 0.4.4__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 +1 -2
- sparrow_parse/vllm/mlx_inference.py +74 -41
- {sparrow_parse-0.4.2.dist-info → sparrow_parse-0.4.4.dist-info}/METADATA +1 -1
- {sparrow_parse-0.4.2.dist-info → sparrow_parse-0.4.4.dist-info}/RECORD +8 -8
- {sparrow_parse-0.4.2.dist-info → sparrow_parse-0.4.4.dist-info}/WHEEL +0 -0
- {sparrow_parse-0.4.2.dist-info → sparrow_parse-0.4.4.dist-info}/entry_points.txt +0 -0
- {sparrow_parse-0.4.2.dist-info → sparrow_parse-0.4.4.dist-info}/top_level.txt +0 -0
sparrow_parse/__init__.py
CHANGED
@@ -1 +1 @@
|
|
1
|
-
__version__ = '0.4.
|
1
|
+
__version__ = '0.4.4'
|
@@ -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):
|
@@ -156,7 +155,7 @@ if __name__ == "__main__":
|
|
156
155
|
# # export HF_TOKEN="hf_"
|
157
156
|
# config = {
|
158
157
|
# "method": "mlx", # Could be 'huggingface', 'mlx' or 'local_gpu'
|
159
|
-
# "model_name": "mlx-community/Qwen2-VL-
|
158
|
+
# "model_name": "mlx-community/Qwen2-VL-7B-Instruct-8bit",
|
160
159
|
# # "hf_space": "katanaml/sparrow-qwen2-vl-7b",
|
161
160
|
# # "hf_token": os.getenv('HF_TOKEN'),
|
162
161
|
# # Additional fields for local GPU inference
|
@@ -14,25 +14,48 @@ class MLXInference(ModelInference):
|
|
14
14
|
|
15
15
|
def __init__(self, model_name):
|
16
16
|
"""
|
17
|
-
Initialize the inference class with the given model name
|
17
|
+
Initialize the inference class with the given model name.
|
18
18
|
|
19
19
|
:param model_name: Name of the model to load.
|
20
20
|
"""
|
21
|
-
self.
|
22
|
-
self.
|
21
|
+
self.model_name = model_name
|
22
|
+
self.model = None
|
23
|
+
self.processor = None
|
24
|
+
print(f"MLXInference initialized with model: {model_name}")
|
23
25
|
|
24
|
-
print(f"Loaded model: {model_name}")
|
25
26
|
|
27
|
+
def __del__(self):
|
28
|
+
"""
|
29
|
+
Destructor to clean up resources when the object is deleted.
|
30
|
+
"""
|
31
|
+
self.unload_model()
|
26
32
|
|
27
|
-
|
28
|
-
def
|
33
|
+
|
34
|
+
def unload_model(self):
|
29
35
|
"""
|
30
|
-
|
36
|
+
Unload the model and release resources.
|
37
|
+
"""
|
38
|
+
if self.model:
|
39
|
+
del self.model
|
40
|
+
self.model = None
|
41
|
+
if self.processor:
|
42
|
+
del self.processor
|
43
|
+
self.processor = None
|
44
|
+
print(f"Model {self.model_name} and its resources have been unloaded.")
|
45
|
+
|
31
46
|
|
47
|
+
def _load_model_and_processor(self, model_name):
|
48
|
+
"""
|
49
|
+
Load the model and processor for inference.
|
32
50
|
:param model_name: Name of the model to load.
|
33
51
|
:return: Tuple containing the loaded model and processor.
|
34
52
|
"""
|
35
|
-
|
53
|
+
print(f"Loading model and processor for: {model_name}...")
|
54
|
+
model, processor = load(model_name)
|
55
|
+
self.model = model # Store model instance
|
56
|
+
self.processor = processor # Store processor instance
|
57
|
+
print(f"Model and processor for '{model_name}' loaded successfully.")
|
58
|
+
return model, processor
|
36
59
|
|
37
60
|
|
38
61
|
def process_response(self, output_text):
|
@@ -81,44 +104,54 @@ class MLXInference(ModelInference):
|
|
81
104
|
def inference(self, input_data, mode=None):
|
82
105
|
"""
|
83
106
|
Perform inference on input data using the specified model.
|
84
|
-
|
85
107
|
:param input_data: A list of dictionaries containing image file paths and text inputs.
|
86
108
|
:param mode: Optional mode for inference ("static" for simple JSON output).
|
87
109
|
:return: List of processed model responses.
|
88
110
|
"""
|
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
|
-
|
111
|
+
try:
|
112
|
+
if mode == "static":
|
113
|
+
return [self.get_simple_json()]
|
114
|
+
|
115
|
+
# Load the model and processor
|
116
|
+
model, processor = self._load_model_and_processor(self.model_name)
|
117
|
+
config = model.config
|
118
|
+
|
119
|
+
# Prepare absolute file paths
|
120
|
+
file_paths = self._extract_file_paths(input_data)
|
121
|
+
|
122
|
+
results = []
|
123
|
+
for file_path in file_paths:
|
124
|
+
image, width, height = self.load_image_data(file_path)
|
125
|
+
|
126
|
+
# Prepare messages for the chat model
|
127
|
+
messages = [
|
128
|
+
{"role": "system",
|
129
|
+
"content": "You are an expert at extracting structured text from image documents."},
|
130
|
+
{"role": "user", "content": input_data[0]["text_input"]},
|
131
|
+
]
|
132
|
+
|
133
|
+
# Generate and process response
|
134
|
+
prompt = apply_chat_template(processor, config, messages)
|
135
|
+
response = generate(
|
136
|
+
model,
|
137
|
+
processor,
|
138
|
+
image,
|
139
|
+
prompt,
|
140
|
+
resize_shape=(width, height),
|
141
|
+
max_tokens=4000,
|
142
|
+
temperature=0.0,
|
143
|
+
verbose=False
|
144
|
+
)
|
145
|
+
results.append(self.process_response(response))
|
146
|
+
|
147
|
+
print("Inference completed successfully for: ", file_path)
|
148
|
+
|
149
|
+
return results
|
150
|
+
|
151
|
+
finally:
|
152
|
+
# Always unload the model after inference
|
153
|
+
self.unload_model()
|
120
154
|
|
121
|
-
return results
|
122
155
|
|
123
156
|
@staticmethod
|
124
157
|
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.4
|
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=jP9l7AhBCN2A-6tezbTIihxoMTDna4SLTYhvVxwbdNM,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=PDLgLlKiq3Bv-UOQTzX3AgxNOLcEU2EniGAXLjMC30U,7820
|
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=c6-s493jLXE3DfYnwsybiqgk3GU9GEaWt3CrfqLSWKQ,5872
|
15
|
+
sparrow_parse-0.4.4.dist-info/METADATA,sha256=x_jaR76FUv5-kR9R5YR9So3OZbWj_rG8hjFZ07lZuto,6432
|
16
|
+
sparrow_parse-0.4.4.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
|
17
|
+
sparrow_parse-0.4.4.dist-info/entry_points.txt,sha256=8CrvTVTTcz1YuZ8aRCYNOH15ZOAaYLlcbYX3t28HwJY,54
|
18
|
+
sparrow_parse-0.4.4.dist-info/top_level.txt,sha256=n6b-WtT91zKLyCPZTP7wvne8v_yvIahcsz-4sX8I0rY,14
|
19
|
+
sparrow_parse-0.4.4.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|