jordyvl commited on
Commit
b1849b5
1 Parent(s): 789c81a

cache loader on server

Browse files
Files changed (1) hide show
  1. cache_loader.py +24 -17
cache_loader.py CHANGED
@@ -3,11 +3,12 @@ from io import BytesIO
3
  from tqdm import tqdm
4
  from typing import Callable, Dict, List, Literal, NamedTuple, Optional, Tuple, Union
5
 
6
- from datasets import load_dataset
7
  from PIL import Image
8
 
9
  import fitz as PyMuPDF
10
 
 
11
 
12
  def pymupdf_image_extraction(data: bytes):
13
  images = []
@@ -18,38 +19,44 @@ def pymupdf_image_extraction(data: bytes):
18
  xref = img[0]
19
  base_image = pdf_file.extract_image(xref)
20
  image_bytes = base_image["image"]
21
- image_ext = base_image["ext"]
22
  im = Image.open(BytesIO(image_bytes))
23
- images.append(im)
24
  return images
25
 
26
 
27
- def batch_get_images(streams, wrapper):
28
  return {
29
- "images": tqdm(
30
- [pymupdf_image_extraction(pdf_stream) for pdf_stream in streams], desc="get_images"
31
- )
32
  }
33
 
34
 
35
- def batch_get_pages(streams, wrapper):
 
 
 
 
 
 
 
 
36
  return {
37
- "pages": tqdm(
38
- [len(PyMuPDF.open(stream=pdf_stream, filetype="pdf")) for pdf_stream in streams],
39
- desc="get_pages",
40
- )
41
  }
42
 
43
 
44
- testds = load_dataset("jordyvl/rvl_cdip_multi", cache_dir="/mnt/lerna/data/HFcache", split="test")
45
- func = lambda batch: batch_get_pages(batch["file"], wrapper=binding)
 
46
  testds = testds.map(func, batched=True, keep_in_memory=False)
 
47
 
48
- func = lambda batch: batch_get_images(batch["file"], wrapper=binding)
49
  testds = testds.map(func, batched=True, keep_in_memory=False)
50
- import pdb
51
 
52
- pdb.set_trace() # breakpoint 37054086 //
53
 
54
  """
55
  Filter based on page count?
 
3
  from tqdm import tqdm
4
  from typing import Callable, Dict, List, Literal, NamedTuple, Optional, Tuple, Union
5
 
6
+ from datasets import load_dataset, logging
7
  from PIL import Image
8
 
9
  import fitz as PyMuPDF
10
 
11
+ logger = logging.get_logger(__name__)
12
 
13
  def pymupdf_image_extraction(data: bytes):
14
  images = []
 
19
  xref = img[0]
20
  base_image = pdf_file.extract_image(xref)
21
  image_bytes = base_image["image"]
 
22
  im = Image.open(BytesIO(image_bytes))
23
+ images.append(im) #<PIL.PngImagePlugin.PngImageFile image mode=L size=2560x3320 at 0x7FD32E7430A0>
24
  return images
25
 
26
 
27
+ def batch_get_images(streams):
28
  return {
29
+ "images":
30
+ [pymupdf_image_extraction(pdf_stream) for pdf_stream in tqdm(streams, desc="get_images")]
31
+
32
  }
33
 
34
 
35
+ def pymupdf_page_extraction(pdf_stream):
36
+ try:
37
+ return len(PyMuPDF.open(stream=pdf_stream, filetype="pdf"))
38
+ except Exception as e:
39
+ logger.warning(f"{e}")
40
+ return 0
41
+
42
+
43
+ def batch_get_pages(streams):
44
  return {
45
+ "pages":
46
+ [pymupdf_page_extraction(pdf_stream) for pdf_stream in tqdm(streams, desc="get_pages")],
 
 
47
  }
48
 
49
 
50
+ testds = load_dataset("jordyvl/rvl_cdip_multi", cache_dir="/mnt/lerna/data/HFcache", split="test", revision="d3a654c9f63f14d0aaa94e08aa30aa3dc20713c1")
51
+
52
+ func = lambda batch: batch_get_pages(batch["file"])
53
  testds = testds.map(func, batched=True, keep_in_memory=False)
54
+ testds = testds.filter(lambda example: example["pages"] != 0)
55
 
56
+ func = lambda batch: batch_get_images(batch["file"])
57
  testds = testds.map(func, batched=True, keep_in_memory=False)
 
58
 
59
+ import pdb; pdb.set_trace() # breakpoint 70086661 //
60
 
61
  """
62
  Filter based on page count?