sparrow-parse 1.0.6__py3-none-any.whl → 1.0.8__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__ = '1.0.6'
1
+ __version__ = '1.0.8'
@@ -75,7 +75,6 @@ class MLXInference(ModelInference):
75
75
  print(f"Failed to parse JSON: {e}")
76
76
  return output_text
77
77
 
78
-
79
78
  def load_image_data(self, image_filepath, max_width=1250, max_height=1750):
80
79
  """
81
80
  Load and resize image while maintaining its aspect ratio.
@@ -155,7 +154,8 @@ class MLXInference(ModelInference):
155
154
  def _process_images(self, model, processor, config, file_paths, input_data, apply_annotation):
156
155
  """
157
156
  Process images and generate responses for each.
158
-
157
+ If apply_annotation=True, don't resize to maintain accurate coordinates.
158
+
159
159
  :param model: The loaded model
160
160
  :param processor: The loaded processor
161
161
  :param config: Model configuration
@@ -166,33 +166,59 @@ class MLXInference(ModelInference):
166
166
  """
167
167
  results = []
168
168
  for file_path in file_paths:
169
- image, width, height = self.load_image_data(file_path)
170
-
169
+ # Load image differently based on annotation requirement
170
+ if apply_annotation:
171
+ # For annotation, just load the image without resizing
172
+ image = load_image(file_path)
173
+ # We'll skip the resize_shape parameter when generating
174
+ else:
175
+ # For non-annotation cases, load with potential resizing
176
+ image, width, height = self.load_image_data(file_path)
177
+ # We'll use resize_shape when generating
178
+
171
179
  # Prepare messages based on model type
172
180
  messages = self._prepare_messages(input_data, apply_annotation)
173
-
181
+
174
182
  # Generate and process response
175
183
  prompt = apply_chat_template(processor, config, messages)
176
- response, _ = generate(
177
- model,
178
- processor,
179
- prompt,
180
- image,
181
- resize_shape=(width, height),
182
- max_tokens=4000,
183
- temperature=0.0,
184
- verbose=False
185
- )
186
- results.append(self.process_response(response))
184
+
185
+ if apply_annotation:
186
+ # When annotation is required, don't use resize_shape
187
+ # This preserves original coordinate system
188
+ response, _ = generate(
189
+ model,
190
+ processor,
191
+ prompt,
192
+ image,
193
+ max_tokens=4000,
194
+ temperature=0.0,
195
+ verbose=False
196
+ )
197
+ else:
198
+ # For non-annotation cases, use resize_shape for memory efficiency
199
+ response, _ = generate(
200
+ model,
201
+ processor,
202
+ prompt,
203
+ image,
204
+ resize_shape=(width, height),
205
+ max_tokens=4000,
206
+ temperature=0.0,
207
+ verbose=False
208
+ )
209
+
210
+ processed_response = self.process_response(response)
211
+ results.append(processed_response)
187
212
  print(f"Inference completed successfully for: {file_path}")
188
-
213
+
189
214
  return results
190
215
 
191
216
 
192
217
  def transform_query_with_bbox(self, text_input):
193
218
  """
194
219
  Transform JSON schema in text_input to include value, bbox, and confidence.
195
- Works with both array and object JSON structures.
220
+ Works with formats like: "retrieve field1, field2. return response in JSON format,
221
+ by strictly following this JSON schema: [{...}]."
196
222
 
197
223
  Args:
198
224
  text_input (str): The input text containing a JSON schema
@@ -200,38 +226,33 @@ class MLXInference(ModelInference):
200
226
  Returns:
201
227
  str: Text with transformed JSON including value, bbox, and confidence
202
228
  """
203
- # Split text into parts - find the JSON portion between "retrieve" and "return response"
204
- retrieve_pattern = r'retrieve\s+'
205
- return_pattern = r'\.\s+return\s+response'
206
229
 
207
- retrieve_match = re.search(retrieve_pattern, text_input)
208
- return_match = re.search(return_pattern, text_input)
230
+ schema_pattern = r'JSON schema:\s*(\[.*?\]|\{.*?\})'
231
+ schema_match = re.search(schema_pattern, text_input, re.DOTALL)
209
232
 
210
- if not retrieve_match or not return_match:
233
+ if not schema_match:
211
234
  return text_input # Return original if pattern not found
212
235
 
213
- json_start = retrieve_match.end()
214
- json_end = return_match.start()
215
-
216
- prefix = text_input[:json_start]
217
- json_str = text_input[json_start:json_end].strip()
218
- suffix = text_input[json_end:]
236
+ # Extract the schema part and its position
237
+ schema_str = schema_match.group(1).strip()
238
+ schema_start = schema_match.start(1)
239
+ schema_end = schema_match.end(1)
219
240
 
220
241
  # Parse and transform the JSON
221
242
  try:
222
243
  # Handle single quotes if needed
223
- json_str = json_str.replace("'", '"')
244
+ schema_str = schema_str.replace("'", '"')
224
245
 
225
- json_obj = json.loads(json_str)
246
+ json_obj = json.loads(schema_str)
226
247
  transformed_json = self.transform_query_structure(json_obj)
227
248
  transformed_json_str = json.dumps(transformed_json)
228
249
 
229
- # Rebuild the text
230
- result = prefix + transformed_json_str + suffix
250
+ # Rebuild the text by replacing just the schema portion
251
+ result = text_input[:schema_start] + transformed_json_str + text_input[schema_end:]
231
252
 
232
253
  return result
233
254
  except json.JSONDecodeError as e:
234
- print(f"Error parsing JSON: {e}")
255
+ print(f"Error parsing JSON schema: {e}")
235
256
  return text_input # Return original if parsing fails
236
257
 
237
258
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sparrow-parse
3
- Version: 1.0.6
3
+ Version: 1.0.8
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=zrUEHc9dmvLJ5ka5maZk9TTHoZ21dwKsENXeOSwXM3o,21
1
+ sparrow_parse/__init__.py,sha256=iCEPnhz-knfGRAO4Ep2uQaYf4xwhPIjjcgAcNjga8kc,21
2
2
  sparrow_parse/__main__.py,sha256=Xs1bpJV0n08KWOoQE34FBYn6EBXZA9HIYJKrE4ZdG78,153
3
3
  sparrow_parse/text_extraction.py,sha256=uhYVNK5Q2FZnw1Poa3JWjtN-aEL7cyKpvaltdn0m2II,8948
4
4
  sparrow_parse/extractors/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -13,9 +13,9 @@ sparrow_parse/vllm/huggingface_inference.py,sha256=RqYmP-wh_cm_BZ271HbejnZe30S5E
13
13
  sparrow_parse/vllm/inference_base.py,sha256=AmWF1OUjJLxSEK_WCbcRpXHX3cKk8nPJJHha_X-9Gs4,844
14
14
  sparrow_parse/vllm/inference_factory.py,sha256=FTM65O-dW2WZchHOrNN7_Q3-FlVoAc65iSptuuUuClM,1166
15
15
  sparrow_parse/vllm/local_gpu_inference.py,sha256=SIyprv12fYawwfxgQ7ZOTM5WmMfQqhO_9vbereRpZdk,652
16
- sparrow_parse/vllm/mlx_inference.py,sha256=wNysikBBU5tTg3u2902EkhJOoliccHydL4IXHOW6j3I,11824
17
- sparrow_parse-1.0.6.dist-info/METADATA,sha256=RKzmkA3uaUQ9g5kJivfYp7S20_gbqeiZ_uJA7_fbmQQ,7229
18
- sparrow_parse-1.0.6.dist-info/WHEEL,sha256=yQN5g4mg4AybRjkgi-9yy4iQEFibGQmlz78Pik5Or-A,92
19
- sparrow_parse-1.0.6.dist-info/entry_points.txt,sha256=HV5nnQVtr2m-kn6hzY_ynp0zugNCcGovbmnfmQgOyhw,53
20
- sparrow_parse-1.0.6.dist-info/top_level.txt,sha256=n6b-WtT91zKLyCPZTP7wvne8v_yvIahcsz-4sX8I0rY,14
21
- sparrow_parse-1.0.6.dist-info/RECORD,,
16
+ sparrow_parse/vllm/mlx_inference.py,sha256=j4DWq6e_9iQSt7CmWuA7OD7RoXkCrxzCNq4UffBuaoQ,12882
17
+ sparrow_parse-1.0.8.dist-info/METADATA,sha256=clalm_6WpyInHCLH10dyMGX4dgJrPHIXwSU9ltSFZKM,7229
18
+ sparrow_parse-1.0.8.dist-info/WHEEL,sha256=yQN5g4mg4AybRjkgi-9yy4iQEFibGQmlz78Pik5Or-A,92
19
+ sparrow_parse-1.0.8.dist-info/entry_points.txt,sha256=HV5nnQVtr2m-kn6hzY_ynp0zugNCcGovbmnfmQgOyhw,53
20
+ sparrow_parse-1.0.8.dist-info/top_level.txt,sha256=n6b-WtT91zKLyCPZTP7wvne8v_yvIahcsz-4sX8I0rY,14
21
+ sparrow_parse-1.0.8.dist-info/RECORD,,