ahd75 commited on
Commit
acd143d
·
verified ·
1 Parent(s): 207f627

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -40
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
- # Case 1: List - Recurse through all elements
62
- return [clean_ppstructure_result(item) for item in result]
63
-
64
- # Case 2: Dictionary or Custom Object
65
- # Custom objects (with a __dict__) need to be converted to dicts before processing.
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
- # Exclude raw image data, as it's too large and unnecessary for JSON
77
- if key == 'img':
78
- continue
79
-
80
- cleaned_dict[key] = clean_ppstructure_result(value)
81
- return cleaned_dict
82
-
83
- # Case 3: Numpy array - Convert to standard Python list
84
- elif isinstance(result, np.ndarray):
 
 
 
 
 
 
 
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
- elif isinstance(result, np.integer):
92
  return int(result)
93
- elif isinstance(result, np.bool_):
94
  return bool(result)
95
-
96
- # Case 5: Standard Python primitive or unhandled type - return as is
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
- #return {"result": json_safe_result}
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