gvlktejaswi commited on
Commit
7af52cf
·
verified ·
1 Parent(s): bdc9b48

Update page_files/categorized/Backend/category_push.py

Browse files
page_files/categorized/Backend/category_push.py CHANGED
@@ -52,7 +52,7 @@ EXTRAS_COLUMNS = [
52
  "confirmed_by_gpt", "confirmed_by_claude", "verified_by", "match_layer",
53
  "doi_url", "mapped_figure_id", "mapped_figure_caption", "mapped_figure_number",
54
  "mapped_figure_page", "mapped_subplot", "map_score", "map_signals",
55
- "plot_image_path",
56
  ]
57
 
58
  _CLASS_OF = {"Composites_materials": "composite", "Fibers": "fiber", "Polymers": "polymer"}
@@ -204,7 +204,8 @@ def conform_to_category(
204
 
205
  # resolve -> S3 (preferred) or local copy -> optionally embed
206
  img_bytes = None
207
- stored_path = "" # what extras.plot_image_path records
 
208
  rel = _s(row.get("plot_image_path"))
209
  if rel and _is_url(rel):
210
  # mapper already uploaded this crop to S3 - reuse the URL as-is
@@ -217,6 +218,15 @@ def conform_to_category(
217
  if key:
218
  try:
219
  img_bytes = _s3_get(key)
 
 
 
 
 
 
 
 
 
220
  except Exception as e:
221
  log.warning(f"category_push: S3 fetch failed for {rel} ({e})")
222
  elif rel:
@@ -227,15 +237,25 @@ def conform_to_category(
227
  data = fh.read()
228
  if embed_image:
229
  img_bytes = data
 
 
 
 
 
 
 
 
 
 
230
  if _s3_ready():
231
  try:
232
- stored_path = _s3_put(data, _s3_key(category_table, stem, fn))
233
  except Exception as e:
234
- log.warning(f"category_push: S3 upload failed for {fn} ({e}); copying locally.")
235
- if not stored_path: # no S3, or upload failed -> local copy
236
- os.makedirs(dest_dir, exist_ok=True)
237
- shutil.copyfile(abs_src, os.path.join(dest_dir, fn))
238
- stored_path = os.path.join(category_table, stem, fn)
239
  if fn not in copied:
240
  copied.add(fn)
241
  n_plots += 1
@@ -301,6 +321,7 @@ def conform_to_category(
301
  "map_score": _s(row.get("map_score")),
302
  "map_signals": _s(row.get("map_signals")),
303
  "plot_image_path": stored_path,
 
304
  })
305
 
306
  main_df = pd.DataFrame(main_rows, columns=TARGET_COLUMNS)
 
52
  "confirmed_by_gpt", "confirmed_by_claude", "verified_by", "match_layer",
53
  "doi_url", "mapped_figure_id", "mapped_figure_caption", "mapped_figure_number",
54
  "mapped_figure_page", "mapped_subplot", "map_score", "map_signals",
55
+ "plot_image_path", "plot_local_path",
56
  ]
57
 
58
  _CLASS_OF = {"Composites_materials": "composite", "Fibers": "fiber", "Polymers": "polymer"}
 
204
 
205
  # resolve -> S3 (preferred) or local copy -> optionally embed
206
  img_bytes = None
207
+ stored_path = "" # primary path extras.plot_image_path records
208
+ local_path = "" # local relative copy (always kept when possible)
209
  rel = _s(row.get("plot_image_path"))
210
  if rel and _is_url(rel):
211
  # mapper already uploaded this crop to S3 - reuse the URL as-is
 
218
  if key:
219
  try:
220
  img_bytes = _s3_get(key)
221
+ # also drop a local copy alongside so a local path exists too
222
+ try:
223
+ fn = os.path.basename(key)
224
+ os.makedirs(dest_dir, exist_ok=True)
225
+ with open(os.path.join(dest_dir, fn), "wb") as _fh:
226
+ _fh.write(img_bytes)
227
+ local_path = os.path.join(category_table, stem, fn)
228
+ except Exception as e:
229
+ log.warning(f"category_push: local copy from S3 failed for {rel} ({e}).")
230
  except Exception as e:
231
  log.warning(f"category_push: S3 fetch failed for {rel} ({e})")
232
  elif rel:
 
237
  data = fh.read()
238
  if embed_image:
239
  img_bytes = data
240
+ local_rel = ""
241
+ s3_url = ""
242
+ # ALWAYS keep a local copy under plots_root/<category>/<stem>/
243
+ try:
244
+ os.makedirs(dest_dir, exist_ok=True)
245
+ shutil.copyfile(abs_src, os.path.join(dest_dir, fn))
246
+ local_rel = os.path.join(category_table, stem, fn)
247
+ except Exception as e:
248
+ log.warning(f"category_push: local copy failed for {fn} ({e}).")
249
+ # AND upload to S3 when configured
250
  if _s3_ready():
251
  try:
252
+ s3_url = _s3_put(data, _s3_key(category_table, stem, fn))
253
  except Exception as e:
254
+ log.warning(f"category_push: S3 upload failed for {fn} ({e}); keeping local path.")
255
+ # primary path recorded in extras: prefer the S3 URL, else local
256
+ stored_path = s3_url or local_rel
257
+ # keep both resolvable references on the row
258
+ local_path = local_rel
259
  if fn not in copied:
260
  copied.add(fn)
261
  n_plots += 1
 
321
  "map_score": _s(row.get("map_score")),
322
  "map_signals": _s(row.get("map_signals")),
323
  "plot_image_path": stored_path,
324
+ "plot_local_path": local_path,
325
  })
326
 
327
  main_df = pd.DataFrame(main_rows, columns=TARGET_COLUMNS)