sparrow-parse 0.4.4__py3-none-any.whl → 0.4.6__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 CHANGED
@@ -1 +1 @@
1
- __version__ = '0.4.4'
1
+ __version__ = '0.4.6'
@@ -152,34 +152,34 @@ if __name__ == "__main__":
152
152
 
153
153
  extractor = VLLMExtractor()
154
154
 
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}")
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}")
@@ -23,3 +23,9 @@ class InferenceFactory:
23
23
  # model = torch.load('model.pth')
24
24
  # return model
25
25
  raise NotImplementedError("Model loading logic not implemented")
26
+
27
+
28
+ def unload_inference_instance(self, instance):
29
+ if instance and hasattr(instance, "unload_model"):
30
+ instance.unload_model()
31
+ print(f"Inference instance of type {type(instance).__name__} has been unloaded.")
@@ -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):
@@ -24,24 +25,22 @@ class MLXInference(ModelInference):
24
25
  print(f"MLXInference initialized with model: {model_name}")
25
26
 
26
27
 
27
- def __del__(self):
28
- """
29
- Destructor to clean up resources when the object is deleted.
30
- """
31
- self.unload_model()
32
-
33
-
34
28
  def unload_model(self):
35
29
  """
36
30
  Unload the model and release resources.
37
31
  """
38
32
  if self.model:
33
+ print(f"Unloading model: {self.model_name}")
39
34
  del self.model
40
35
  self.model = None
41
36
  if self.processor:
37
+ print(f"Unloading processor for model: {self.model_name}")
42
38
  del self.processor
43
39
  self.processor = None
44
- print(f"Model {self.model_name} and its resources have been unloaded.")
40
+
41
+ # Force garbage collection to release memory
42
+ gc.collect()
43
+ print(f"Model {self.model_name} and its resources have been unloaded, memory cleared.")
45
44
 
46
45
 
47
46
  def _load_model_and_processor(self, model_name):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sparrow-parse
3
- Version: 0.4.4
3
+ Version: 0.4.6
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=jP9l7AhBCN2A-6tezbTIihxoMTDna4SLTYhvVxwbdNM,21
1
+ sparrow_parse/__init__.py,sha256=IIRkY9z46sq_aFXyT8WySWX9GdnZHB1GyedV8VfB_lA,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=PDLgLlKiq3Bv-UOQTzX3AgxNOLcEU2EniGAXLjMC30U,7820
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
@@ -9,11 +9,11 @@ sparrow_parse/processors/table_structure_processor.py,sha256=PQHHFdQUuTin3Mm2USu
9
9
  sparrow_parse/vllm/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
10
  sparrow_parse/vllm/huggingface_inference.py,sha256=EJnG6PesGKMc_0qGPN8ufE6pSnhAgFu0XjCbaLCNVyM,1980
11
11
  sparrow_parse/vllm/inference_base.py,sha256=4mwGoAY63MB4cHZpV0czTkJWEzimmiTzqqzKmLNzgjw,820
12
- sparrow_parse/vllm/inference_factory.py,sha256=FTM65O-dW2WZchHOrNN7_Q3-FlVoAc65iSptuuUuClM,1166
12
+ sparrow_parse/vllm/inference_factory.py,sha256=J5_sPIL6YlkttaGeTc6_6t4004klQxESeeLLhXHvyZg,1407
13
13
  sparrow_parse/vllm/local_gpu_inference.py,sha256=aHoJTejb5xrXjWDIGu5RBQWEyRCOBCB04sMvO2Wyvg8,628
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,,
14
+ sparrow_parse/vllm/mlx_inference.py,sha256=NNBJ_eQZTzJ6X05wUfJhGLJjm1ANMB5BxpW7GMR4S3g,5955
15
+ sparrow_parse-0.4.6.dist-info/METADATA,sha256=YiIuculsNFZVH2YKtSGTRpOMPpc6B2DhH0sbW99onBM,6432
16
+ sparrow_parse-0.4.6.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
17
+ sparrow_parse-0.4.6.dist-info/entry_points.txt,sha256=8CrvTVTTcz1YuZ8aRCYNOH15ZOAaYLlcbYX3t28HwJY,54
18
+ sparrow_parse-0.4.6.dist-info/top_level.txt,sha256=n6b-WtT91zKLyCPZTP7wvne8v_yvIahcsz-4sX8I0rY,14
19
+ sparrow_parse-0.4.6.dist-info/RECORD,,