sparrow-parse 0.4.5__py3-none-any.whl → 0.4.7__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/vllm/mlx_inference.py +42 -76
- {sparrow_parse-0.4.5.dist-info → sparrow_parse-0.4.7.dist-info}/METADATA +1 -1
- {sparrow_parse-0.4.5.dist-info → sparrow_parse-0.4.7.dist-info}/RECORD +7 -7
- {sparrow_parse-0.4.5.dist-info → sparrow_parse-0.4.7.dist-info}/WHEEL +0 -0
- {sparrow_parse-0.4.5.dist-info → sparrow_parse-0.4.7.dist-info}/entry_points.txt +0 -0
- {sparrow_parse-0.4.5.dist-info → sparrow_parse-0.4.7.dist-info}/top_level.txt +0 -0
sparrow_parse/__init__.py
CHANGED
@@ -1 +1 @@
|
|
1
|
-
__version__ = '0.4.
|
1
|
+
__version__ = '0.4.7'
|
@@ -4,7 +4,6 @@ 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
|
8
7
|
|
9
8
|
|
10
9
|
class MLXInference(ModelInference):
|
@@ -20,46 +19,19 @@ class MLXInference(ModelInference):
|
|
20
19
|
:param model_name: Name of the model to load.
|
21
20
|
"""
|
22
21
|
self.model_name = model_name
|
23
|
-
|
24
|
-
self.processor = None
|
25
|
-
print(f"MLXInference initialized with model: {model_name}")
|
22
|
+
print(f"MLXInference initialized for model: {model_name}")
|
26
23
|
|
27
24
|
|
28
|
-
|
29
|
-
|
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.")
|
50
|
-
|
51
|
-
|
52
|
-
def _load_model_and_processor(self, model_name):
|
25
|
+
@staticmethod
|
26
|
+
def _load_model_and_processor(model_name):
|
53
27
|
"""
|
54
28
|
Load the model and processor for inference.
|
29
|
+
|
55
30
|
:param model_name: Name of the model to load.
|
56
31
|
:return: Tuple containing the loaded model and processor.
|
57
32
|
"""
|
58
|
-
print(f"Loading model and processor for: {model_name}...")
|
59
33
|
model, processor = load(model_name)
|
60
|
-
|
61
|
-
self.processor = processor # Store processor instance
|
62
|
-
print(f"Model and processor for '{model_name}' loaded successfully.")
|
34
|
+
print(f"Loaded model: {model_name}")
|
63
35
|
return model, processor
|
64
36
|
|
65
37
|
|
@@ -109,54 +81,48 @@ class MLXInference(ModelInference):
|
|
109
81
|
def inference(self, input_data, mode=None):
|
110
82
|
"""
|
111
83
|
Perform inference on input data using the specified model.
|
84
|
+
|
112
85
|
:param input_data: A list of dictionaries containing image file paths and text inputs.
|
113
86
|
:param mode: Optional mode for inference ("static" for simple JSON output).
|
114
87
|
:return: List of processed model responses.
|
115
88
|
"""
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
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()
|
89
|
+
if mode == "static":
|
90
|
+
return [self.get_simple_json()]
|
91
|
+
|
92
|
+
# Load the model and processor
|
93
|
+
model, processor = self._load_model_and_processor(self.model_name)
|
94
|
+
config = model.config
|
95
|
+
|
96
|
+
# Prepare absolute file paths
|
97
|
+
file_paths = self._extract_file_paths(input_data)
|
98
|
+
|
99
|
+
results = []
|
100
|
+
for file_path in file_paths:
|
101
|
+
image, width, height = self.load_image_data(file_path)
|
102
|
+
|
103
|
+
# Prepare messages for the chat model
|
104
|
+
messages = [
|
105
|
+
{"role": "system", "content": "You are an expert at extracting structured text from image documents."},
|
106
|
+
{"role": "user", "content": input_data[0]["text_input"]},
|
107
|
+
]
|
108
|
+
|
109
|
+
# Generate and process response
|
110
|
+
prompt = apply_chat_template(processor, config, messages) # Assuming defined
|
111
|
+
response = generate(
|
112
|
+
model,
|
113
|
+
processor,
|
114
|
+
image,
|
115
|
+
prompt,
|
116
|
+
resize_shape=(width, height),
|
117
|
+
max_tokens=4000,
|
118
|
+
temperature=0.0,
|
119
|
+
verbose=False
|
120
|
+
)
|
121
|
+
results.append(self.process_response(response))
|
122
|
+
|
123
|
+
print("Inference completed successfully for: ", file_path)
|
159
124
|
|
125
|
+
return results
|
160
126
|
|
161
127
|
@staticmethod
|
162
128
|
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.7
|
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,4 +1,4 @@
|
|
1
|
-
sparrow_parse/__init__.py,sha256=
|
1
|
+
sparrow_parse/__init__.py,sha256=swwUIK4WtUiYyZYTfyyc_9CkUUoMvY9Mrqd1fsqa9_E,21
|
2
2
|
sparrow_parse/__main__.py,sha256=Xs1bpJV0n08KWOoQE34FBYn6EBXZA9HIYJKrE4ZdG78,153
|
3
3
|
sparrow_parse/extractors/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
4
4
|
sparrow_parse/extractors/vllm_extractor.py,sha256=G4kQh0GoZ4V4TdyeDwZWFkOG15MQCQMvSf2UbFQDWeI,7746
|
@@ -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=9PgcPf5uvAmbRmuUD1BOs_WZqzx82hFdoWMUcTLdJ50,4876
|
15
|
+
sparrow_parse-0.4.7.dist-info/METADATA,sha256=KX74XdEuoK7fGg0haxPlo6Oyx3HLA-kDyfACDJEouJY,6432
|
16
|
+
sparrow_parse-0.4.7.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
|
17
|
+
sparrow_parse-0.4.7.dist-info/entry_points.txt,sha256=8CrvTVTTcz1YuZ8aRCYNOH15ZOAaYLlcbYX3t28HwJY,54
|
18
|
+
sparrow_parse-0.4.7.dist-info/top_level.txt,sha256=n6b-WtT91zKLyCPZTP7wvne8v_yvIahcsz-4sX8I0rY,14
|
19
|
+
sparrow_parse-0.4.7.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|