Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 41 |
-
|
| 42 |
"""
|
| 43 |
if isinstance(result, list):
|
|
|
|
| 44 |
return [clean_ppstructure_result(item) for item in result]
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
cleaned_dict = {}
|
| 47 |
-
for key, value in
|
| 48 |
-
#
|
| 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()
|
|
|
|
|
|
|
|
|
|
| 58 |
elif isinstance(result, np.floating):
|
| 59 |
-
return float(result)
|
| 60 |
elif isinstance(result, np.integer):
|
| 61 |
-
return int(result)
|
| 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 |
|