taisazero commited on
Commit
cf9f33d
1 Parent(s): f7617eb

fix-streaming (#6)

Browse files

- Use xopen to load files (7e22f871ebd8a49e1e324b9f305b34f74ce2e23a)
- Read rather than readlines (9ec7f28d8a4977f95898001c96c90723c733281c)
- gzip should be able to handle file handler (da5e8efcb1a28c1af16a8ba6da42b9580537610a)

Files changed (1) hide show
  1. code_clippy_github.py +26 -24
code_clippy_github.py CHANGED
@@ -20,6 +20,7 @@ import os
20
 
21
 
22
  import datasets
 
23
  from huggingface_hub import HfApi, HfFolder
24
  from datasets.data_files import DataFilesDict
25
 
@@ -174,30 +175,31 @@ class CodeClippyGithub(datasets.GeneratorBasedBuilder):
174
  def _generate_examples(self, files):
175
  key = 0
176
  for file_idx, file in enumerate(files):
177
- with gzip.open(file, "rb") as f:
178
-
179
- uncompressed_data = f.readlines()
180
-
181
- for batch_idx, code_base in enumerate(uncompressed_data):
182
- j_dict = json.loads(code_base.decode('utf-8'))
183
-
184
-
185
-
186
- lang = lang_from_name(j_dict['path'])
187
- license = j_dict["license"]
188
-
189
- if self.config.filter_languages and not lang in self.config.languages:
190
- continue
191
- if self.config.filter_licenses and not license in self.config.licenses:
192
- continue
193
- # TODO: Add more features like header comments, filename, and other features useful in a prompt.
194
- yield key, {"code_text": j_dict['content'],
195
- "repo_name": j_dict['repo_name'],
196
- "file_path": j_dict['path'],
197
- "license": license,
198
- "language": lang,
199
- "size": int(j_dict['f0_'])}
200
- key += 1
 
201
 
202
 
203
  def lang_from_name(name):
20
 
21
 
22
  import datasets
23
+ from datasets.download.streaming_download_manager import xopen
24
  from huggingface_hub import HfApi, HfFolder
25
  from datasets.data_files import DataFilesDict
26
 
175
  def _generate_examples(self, files):
176
  key = 0
177
  for file_idx, file in enumerate(files):
178
+ with xopen(file, "rb") as file: # download file if in streaming mode
179
+ with gzip.open(file, "rb") as f:
180
+
181
+ uncompressed_data = f.readlines()
182
+
183
+ for batch_idx, code_base in enumerate(uncompressed_data):
184
+ j_dict = json.loads(code_base.decode('utf-8'))
185
+
186
+
187
+
188
+ lang = lang_from_name(j_dict['path'])
189
+ license = j_dict["license"]
190
+
191
+ if self.config.filter_languages and not lang in self.config.languages:
192
+ continue
193
+ if self.config.filter_licenses and not license in self.config.licenses:
194
+ continue
195
+ # TODO: Add more features like header comments, filename, and other features useful in a prompt.
196
+ yield key, {"code_text": j_dict['content'],
197
+ "repo_name": j_dict['repo_name'],
198
+ "file_path": j_dict['path'],
199
+ "license": license,
200
+ "language": lang,
201
+ "size": int(j_dict['f0_'])}
202
+ key += 1
203
 
204
 
205
  def lang_from_name(name):