ahd75 commited on
Commit
aa1faaf
·
verified ·
1 Parent(s): 7253ef8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -10
app.py CHANGED
@@ -37,30 +37,46 @@ def decode_image(image_base64: str):
37
 
38
  def clean_ppstructure_result(result):
39
  """
40
- Recursively converts numpy arrays in the PPStructure result to standard lists.
41
- Also optionally removes the 'img' field (raw numpy image crop) to keep standard JSON payloads light.
42
  """
43
  if isinstance(result, list):
 
44
  return [clean_ppstructure_result(item) for item in result]
45
- elif isinstance(result, dict):
 
 
 
 
 
 
 
 
 
 
46
  cleaned_dict = {}
47
- for key, value in result.items():
48
- # PPStructure often includes the raw image crop as a numpy array in the 'img' key.
49
- # Returning this in standard JSON is usually a bad idea (too heavy).
50
- # Comment out the next two lines if you actually NEED raw pixel data in your JSON.
51
  if key == 'img':
52
  continue
53
 
54
  cleaned_dict[key] = clean_ppstructure_result(value)
55
  return cleaned_dict
 
 
56
  elif isinstance(result, np.ndarray):
57
- return result.tolist() # crucial: converts numpy array to standard list
 
 
 
58
  elif isinstance(result, np.floating):
59
- return float(result) # crucial: converts ALL numpy floats (32, 64, etc) to standard python floats
60
  elif isinstance(result, np.integer):
61
- return int(result) # crucial: converts ALL numpy ints (int8, int16, uint8, etc) to standard python ints
62
  elif isinstance(result, np.bool_):
63
  return bool(result)
 
 
64
  else:
65
  return result
66
 
 
37
 
38
  def clean_ppstructure_result(result):
39
  """
40
+ Recursively converts numpy arrays and custom objects in the PPStructure result
41
+ to standard Python lists and dictionaries that are JSON-safe.
42
  """
43
  if isinstance(result, list):
44
+ # Case 1: List - Recurse through all elements
45
  return [clean_ppstructure_result(item) for item in result]
46
+
47
+ # Case 2: Dictionary or Custom Object
48
+ # Custom objects (with a __dict__) need to be converted to dicts before processing.
49
+ if isinstance(result, dict) or hasattr(result, '__dict__'):
50
+ if hasattr(result, '__dict__'):
51
+ # It's a custom class instance (e.g., from PPStructure), get its attributes as a dict
52
+ items = vars(result).items()
53
+ else:
54
+ # It's a standard dictionary
55
+ items = result.items()
56
+
57
  cleaned_dict = {}
58
+ for key, value in items:
59
+ # Exclude raw image data, as it's too large and unnecessary for JSON
 
 
60
  if key == 'img':
61
  continue
62
 
63
  cleaned_dict[key] = clean_ppstructure_result(value)
64
  return cleaned_dict
65
+
66
+ # Case 3: Numpy array - Convert to standard Python list
67
  elif isinstance(result, np.ndarray):
68
+ return result.tolist()
69
+
70
+ # Case 4: Numpy scalar types - Convert to standard Python primitives
71
+ # These base classes cover all int and float variants (int16, float32, etc.)
72
  elif isinstance(result, np.floating):
73
+ return float(result)
74
  elif isinstance(result, np.integer):
75
+ return int(result)
76
  elif isinstance(result, np.bool_):
77
  return bool(result)
78
+
79
+ # Case 5: Standard Python primitive or unhandled type - return as is
80
  else:
81
  return result
82