Alender777 commited on
Commit
60e2fe8
·
verified ·
1 Parent(s): 60b8912

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -3
app.py CHANGED
@@ -135,17 +135,59 @@ def evaluate_amount_predictions(y_true, y_pred, relaxed_tol=0.01):
135
  # CORD v2 GT
136
  # ============================================================
137
  def get_gt_total_from_cord_item(item):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  gt = item["ground_truth"]
 
 
139
  if not isinstance(gt, dict):
140
  try:
141
  gt = json.loads(gt)
142
  except Exception:
143
  return None
 
 
144
  try:
145
- val = gt.get("total", {}).get("total_price")
 
 
 
 
 
 
146
  except Exception:
147
- val = None
148
- return val
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
 
150
 
151
  # ============================================================
 
135
  # CORD v2 GT
136
  # ============================================================
137
  def get_gt_total_from_cord_item(item):
138
+ """
139
+ 從 CORD v2 的 ground_truth 中取出 total amount (total_price).
140
+ CORD v2 結構大致為:
141
+ {
142
+ "gt_parse": {
143
+ "menu": [...],
144
+ "sub_total": {"subtotal_price": "..."},
145
+ "total": {"total_price": "...", ...}
146
+ },
147
+ "meta": {...},
148
+ "valid_line": [...],
149
+ ...
150
+ }
151
+ """
152
  gt = item["ground_truth"]
153
+
154
+ # ground_truth 在 HF 裡是 JSON string,要先 parse 成 dict
155
  if not isinstance(gt, dict):
156
  try:
157
  gt = json.loads(gt)
158
  except Exception:
159
  return None
160
+
161
+ # 1) 優先走官方路徑 gt_parse.total.total_price
162
  try:
163
+ val = (
164
+ gt.get("gt_parse", {})
165
+ .get("total", {})
166
+ .get("total_price", None)
167
+ )
168
+ if val is not None:
169
+ return val
170
  except Exception:
171
+ pass
172
+
173
+ # 2) 保險:如果結構有變,遞迴搜尋任何名為 "total_price" 的欄位
174
+ def _find_total_price(node):
175
+ if isinstance(node, dict):
176
+ for k, v in node.items():
177
+ if k == "total_price" and isinstance(v, str):
178
+ return v
179
+ found = _find_total_price(v)
180
+ if found is not None:
181
+ return found
182
+ elif isinstance(node, list):
183
+ for item_ in node:
184
+ found = _find_total_price(item_)
185
+ if found is not None:
186
+ return found
187
+ return None
188
+
189
+ return _find_total_price(gt)
190
+
191
 
192
 
193
  # ============================================================