soldni commited on
Commit
72140da
1 Parent(s): 7f17d57

:reliability of download

Browse files
Files changed (2) hide show
  1. prepare_for_release.py +26 -24
  2. requirements.txt +1 -1
prepare_for_release.py CHANGED
@@ -9,19 +9,18 @@ import json
9
  from contextlib import ExitStack
10
  from functools import partial
11
  from multiprocessing import Manager, Pool, cpu_count, set_start_method
 
12
  from queue import Queue
13
  from threading import Thread
14
  from time import sleep
 
15
  from typing import List, Optional, Tuple
16
 
17
  from smashed.utils import (
18
  MultiPath,
19
- compress_stream,
20
- decompress_stream,
21
- open_file_for_write,
22
  recursively_list_files,
23
- stream_file_for_read,
24
  )
 
25
  from tqdm import tqdm
26
 
27
 
@@ -33,30 +32,32 @@ def process_single(
33
  all_src, dst = io_paths
34
  docs_cnt = 0
35
 
36
- with ExitStack() as in_stack, ExitStack() as out_stack:
37
- out_file = out_stack.enter_context(open_file_for_write(dst, "wb"))
38
- out_stream = out_stack.enter_context(compress_stream(out_file, "wt"))
 
39
  for src in all_src:
40
- in_file = in_stack.enter_context(stream_file_for_read(src, "rb"))
41
- in_stream = in_stack.enter_context(decompress_stream(in_file, "rt"))
42
 
43
- for line in in_stream:
44
- data = json.loads(line)
45
- data.pop("metadata", None)
46
- data["source"] = "s2ag" if "dataset=s2ag" in src.as_str else "s2orc"
47
- data["split"] = "train" if "split=train" in src.as_str else "valid"
48
- data["version"] = version
 
 
 
49
 
50
- out_stream.write(json.dumps(data) + "\n")
51
- docs_cnt += 1
52
 
53
- if pbar_queue is not None and docs_cnt % 1000 == 0:
54
- pbar_queue.put((0, docs_cnt))
55
- docs_cnt = 0
56
 
57
- in_stack.pop_all().close()
58
- if pbar_queue is not None:
59
- pbar_queue.put((1, 0))
60
 
61
  if pbar_queue is not None:
62
  pbar_queue.put((0, docs_cnt))
@@ -110,7 +111,8 @@ def main():
110
  src_paths.append([MultiPath.parse(p) for p in dir_values])
111
  dir_path = MultiPath.parse(dir_name.replace("part_id=", "") + ".gz")
112
  dst_path = dst / (diff) if len(diff := (dir_path - src)) > 0 else dst
113
- dst_path.path = dst_path.path.replace("split=", "").replace("dataset=", "")
 
114
  dst_paths.append(dst_path)
115
 
116
  if opts.debug:
 
9
  from contextlib import ExitStack
10
  from functools import partial
11
  from multiprocessing import Manager, Pool, cpu_count, set_start_method
12
+ from pathlib import Path
13
  from queue import Queue
14
  from threading import Thread
15
  from time import sleep
16
+ import re
17
  from typing import List, Optional, Tuple
18
 
19
  from smashed.utils import (
20
  MultiPath,
 
 
 
21
  recursively_list_files,
 
22
  )
23
+ import smart_open
24
  from tqdm import tqdm
25
 
26
 
 
32
  all_src, dst = io_paths
33
  docs_cnt = 0
34
 
35
+ if dst.is_local:
36
+ Path(dst.as_str).parent.mkdir(parents=True, exist_ok=True)
37
+
38
+ with smart_open.open(dst.as_str, "wt") as out_stream:
39
  for src in all_src:
40
+ with smart_open.open(src.as_str, "rt") as in_stream:
 
41
 
42
+ for line in in_stream:
43
+ data = json.loads(line)
44
+ data.pop("metadata", None)
45
+ data["source"] = (
46
+ ("s2ag" if "dataset=s2ag" in src.as_str else "s2orc") +
47
+ '/' +
48
+ ("train" if "split=train" in src.as_str else "valid")
49
+ )
50
+ data["version"] = version
51
 
52
+ out_stream.write(json.dumps(data) + "\n")
53
+ docs_cnt += 1
54
 
55
+ if pbar_queue is not None and docs_cnt % 10000 == 0:
56
+ pbar_queue.put((0, docs_cnt))
57
+ docs_cnt = 0
58
 
59
+ if pbar_queue is not None:
60
+ pbar_queue.put((1, 0))
 
61
 
62
  if pbar_queue is not None:
63
  pbar_queue.put((0, docs_cnt))
 
111
  src_paths.append([MultiPath.parse(p) for p in dir_values])
112
  dir_path = MultiPath.parse(dir_name.replace("part_id=", "") + ".gz")
113
  dst_path = dst / (diff) if len(diff := (dir_path - src)) > 0 else dst
114
+ dst_path.path = re.sub(r'dataset=(\w+)/', '\\1.', dst_path.path)
115
+ dst_path.path = re.sub(r'split=(\w+)/', '\\1.', dst_path.path)
116
  dst_paths.append(dst_path)
117
 
118
  if opts.debug:
requirements.txt CHANGED
@@ -1,2 +1,2 @@
1
- smashed[remote]
2
  tqdm
 
1
+ smart_open
2
  tqdm