versae commited on
Commit
97222ed
1 Parent(s): 99745f2

Update mc4-sampling.py

Browse files
Files changed (1) hide show
  1. mc4-sampling.py +12 -1
mc4-sampling.py CHANGED
@@ -5,7 +5,16 @@ import gzip
5
  import json
6
 
7
  import datasets
8
- import kenlm # pip install https://github.com/kpu/kenlm/archive/master.zip
 
 
 
 
 
 
 
 
 
9
  import numpy as np
10
  from numpy.random import default_rng
11
 
@@ -302,6 +311,8 @@ class Mc4Sampling(datasets.GeneratorBasedBuilder):
302
  # http://dl.fbaipublicfiles.com/cc_net/lm/es.arpa.bin
303
  logger.info("loading model = %s", str(self.perplexity_model))
304
  if isinstance(self.perplexity_model, str):
 
 
305
  self.pp_model = kenlm.Model(self.perplexity_model)
306
  else:
307
  self.pp_model = self.perplexity_model
 
5
  import json
6
 
7
  import datasets
8
+ try:
9
+ import kenlm # pip install https://github.com/kpu/kenlm/archive/master.zip
10
+ except ImportError:
11
+ import warnings
12
+ KENLM_IMPORT = (
13
+ "To be able to use bertin-project/mc4-sampling, you need to install the following dependency: kenlm.\n"
14
+ "Please install it using 'pip install https://github.com/kpu/kenlm/archive/master.zip' for instance."
15
+ )
16
+ kenlm = None
17
+ warnings.warn(KENLM_IMPORT)
18
  import numpy as np
19
  from numpy.random import default_rng
20
 
 
311
  # http://dl.fbaipublicfiles.com/cc_net/lm/es.arpa.bin
312
  logger.info("loading model = %s", str(self.perplexity_model))
313
  if isinstance(self.perplexity_model, str):
314
+ if not kenlm:
315
+ raise ImportError(KENLM_IMPORT)
316
  self.pp_model = kenlm.Model(self.perplexity_model)
317
  else:
318
  self.pp_model = self.perplexity_model