Spaces:
Sleeping
Sleeping
Raghu commited on
Commit ·
3716fe1
1
Parent(s): ae56e5e
Sanitize numpy bools before JSON dump
Browse files
app.py
CHANGED
|
@@ -44,6 +44,26 @@ def _safe_json_schema_to_python_type(schema, defs=None):
|
|
| 44 |
grc_utils.get_type = _safe_get_type
|
| 45 |
grc_utils.json_schema_to_python_type = _safe_json_schema_to_python_type
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
# ============================================================================
|
| 48 |
# Configuration
|
| 49 |
# ============================================================================
|
|
@@ -657,7 +677,7 @@ def process_receipt(image):
|
|
| 657 |
{fields_html}
|
| 658 |
"""
|
| 659 |
|
| 660 |
-
return summary_html, ocr_image, ocr_text, json.dumps(results, indent=2)
|
| 661 |
|
| 662 |
|
| 663 |
# ============================================================================
|
|
|
|
| 44 |
grc_utils.get_type = _safe_get_type
|
| 45 |
grc_utils.json_schema_to_python_type = _safe_json_schema_to_python_type
|
| 46 |
|
| 47 |
+
# ---------------------------------------------------------------------------
|
| 48 |
+
# JSON sanitation helper (convert numpy types & PIL-friendly outputs)
|
| 49 |
+
# ---------------------------------------------------------------------------
|
| 50 |
+
def to_jsonable(obj):
|
| 51 |
+
if isinstance(obj, dict):
|
| 52 |
+
return {k: to_jsonable(v) for k, v in obj.items()}
|
| 53 |
+
if isinstance(obj, (list, tuple)):
|
| 54 |
+
return [to_jsonable(v) for v in obj]
|
| 55 |
+
if isinstance(obj, (np.bool_, bool)):
|
| 56 |
+
return bool(obj)
|
| 57 |
+
if isinstance(obj, (np.integer,)):
|
| 58 |
+
return int(obj)
|
| 59 |
+
if isinstance(obj, (np.floating,)):
|
| 60 |
+
return float(obj)
|
| 61 |
+
if isinstance(obj, np.ndarray):
|
| 62 |
+
return obj.tolist()
|
| 63 |
+
if isinstance(obj, Image.Image):
|
| 64 |
+
return None # avoid serializing images; skip in JSON
|
| 65 |
+
return obj
|
| 66 |
+
|
| 67 |
# ============================================================================
|
| 68 |
# Configuration
|
| 69 |
# ============================================================================
|
|
|
|
| 677 |
{fields_html}
|
| 678 |
"""
|
| 679 |
|
| 680 |
+
return summary_html, ocr_image, ocr_text, json.dumps(to_jsonable(results), indent=2)
|
| 681 |
|
| 682 |
|
| 683 |
# ============================================================================
|