Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -52,50 +52,39 @@ def to_jsonable(obj):
|
|
| 52 |
},
|
| 53 |
)
|
| 54 |
|
| 55 |
-
def clean_ppstructure_result(result):
|
| 56 |
-
"""
|
| 57 |
-
Recursively converts numpy arrays and custom objects in the PPStructure result
|
| 58 |
-
to standard Python lists and dictionaries that are JSON-safe.
|
| 59 |
-
"""
|
| 60 |
if isinstance(result, list):
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
if isinstance(result, dict) or hasattr(result, '__dict__'):
|
| 67 |
-
if hasattr(result, '__dict__'):
|
| 68 |
-
# It's a custom class instance (e.g., from PPStructure), get its attributes as a dict
|
| 69 |
-
items = vars(result).items()
|
| 70 |
-
else:
|
| 71 |
-
# It's a standard dictionary
|
| 72 |
-
items = result.items()
|
| 73 |
-
|
| 74 |
-
cleaned_dict = {}
|
| 75 |
for key, value in items:
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
return result.tolist()
|
| 86 |
-
|
| 87 |
-
# Case 4: Numpy scalar types - Convert to standard Python primitives
|
| 88 |
-
# These base classes cover all int and float variants (int16, float32, etc.)
|
| 89 |
-
elif isinstance(result, np.floating):
|
| 90 |
return float(result)
|
| 91 |
-
|
| 92 |
return int(result)
|
| 93 |
-
|
| 94 |
return bool(result)
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
else:
|
| 98 |
-
return result
|
| 99 |
|
| 100 |
# --- API Endpoints ---
|
| 101 |
|
|
@@ -143,8 +132,8 @@ def run_structure_analysis(ocr_input: OcrInput):
|
|
| 143 |
|
| 144 |
print("** Structure Analysis Complete. Converting to JSON-safe **")
|
| 145 |
json_safe_result = clean_ppstructure_result(raw_result)
|
| 146 |
-
|
| 147 |
-
return {"result": to_jsonable(raw_result)}
|
| 148 |
|
| 149 |
except HTTPException:
|
| 150 |
raise
|
|
|
|
| 52 |
},
|
| 53 |
)
|
| 54 |
|
| 55 |
+
def clean_ppstructure_result(result, exclude_keys=("img",), include_img=False, img_mode="shape"):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
if isinstance(result, list):
|
| 57 |
+
return [clean_ppstructure_result(item, exclude_keys, include_img, img_mode) for item in result]
|
| 58 |
+
|
| 59 |
+
if isinstance(result, dict) or hasattr(result, "__dict__"):
|
| 60 |
+
items = vars(result).items() if hasattr(result, "__dict__") else result.items()
|
| 61 |
+
cleaned = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
for key, value in items:
|
| 63 |
+
if key in exclude_keys:
|
| 64 |
+
if include_img and key == "img":
|
| 65 |
+
if img_mode == "shape" and isinstance(value, np.ndarray):
|
| 66 |
+
cleaned["img_shape"] = value.shape
|
| 67 |
+
elif img_mode == "base64" and isinstance(value, np.ndarray):
|
| 68 |
+
ok, buf = cv2.imencode(".png", value)
|
| 69 |
+
if ok:
|
| 70 |
+
cleaned["img_base64"] = base64.b64encode(buf).decode("ascii")
|
| 71 |
+
continue
|
| 72 |
+
cleaned[key] = clean_ppstructure_result(value, exclude_keys, include_img, img_mode)
|
| 73 |
+
return cleaned
|
| 74 |
+
|
| 75 |
+
if isinstance(result, tuple) or isinstance(result, set):
|
| 76 |
+
return [clean_ppstructure_result(v, exclude_keys, include_img, img_mode) for v in result]
|
| 77 |
+
|
| 78 |
+
if isinstance(result, np.ndarray):
|
| 79 |
return result.tolist()
|
| 80 |
+
if isinstance(result, np.floating):
|
|
|
|
|
|
|
|
|
|
| 81 |
return float(result)
|
| 82 |
+
if isinstance(result, np.integer):
|
| 83 |
return int(result)
|
| 84 |
+
if isinstance(result, np.bool_):
|
| 85 |
return bool(result)
|
| 86 |
+
|
| 87 |
+
return result
|
|
|
|
|
|
|
| 88 |
|
| 89 |
# --- API Endpoints ---
|
| 90 |
|
|
|
|
| 132 |
|
| 133 |
print("** Structure Analysis Complete. Converting to JSON-safe **")
|
| 134 |
json_safe_result = clean_ppstructure_result(raw_result)
|
| 135 |
+
return {"result": json_safe_result}
|
| 136 |
+
#return {"result": to_jsonable(raw_result)}
|
| 137 |
|
| 138 |
except HTTPException:
|
| 139 |
raise
|