lvwerra HF staff commited on
Commit
56a1d02
1 Parent(s): 2a7fe78

Update Space (evaluate main: e4a27243)

Browse files
Files changed (2) hide show
  1. perplexity.py +26 -2
  2. requirements.txt +1 -1
perplexity.py CHANGED
@@ -13,6 +13,9 @@
13
  # limitations under the License.
14
  """Perplexity Metric."""
15
 
 
 
 
16
  import datasets
17
  import numpy as np
18
  import torch
@@ -84,14 +87,30 @@ Examples:
84
  """
85
 
86
 
 
 
 
 
 
 
 
 
 
 
 
87
  @evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
88
  class Perplexity(evaluate.Metric):
89
- def _info(self):
 
 
 
 
90
  return evaluate.MetricInfo(
91
  module_type="metric",
92
  description=_DESCRIPTION,
93
  citation=_CITATION,
94
  inputs_description=_KWARGS_DESCRIPTION,
 
95
  features=datasets.Features(
96
  {
97
  "predictions": datasets.Value("string"),
@@ -100,7 +119,12 @@ class Perplexity(evaluate.Metric):
100
  reference_urls=["https://huggingface.co/docs/transformers/perplexity"],
101
  )
102
 
103
- def _compute(self, predictions, model_id, batch_size: int = 16, add_start_token: bool = True, device=None):
 
 
 
 
 
104
 
105
  if device is not None:
106
  assert device in ["gpu", "cpu", "cuda"], "device should be either gpu or cpu."
 
13
  # limitations under the License.
14
  """Perplexity Metric."""
15
 
16
+ from dataclasses import dataclass
17
+ from typing import Optional
18
+
19
  import datasets
20
  import numpy as np
21
  import torch
 
87
  """
88
 
89
 
90
+ @dataclass
91
+ class PerplexityConfig(evaluate.info.Config):
92
+
93
+ name: str = "default"
94
+
95
+ model_id: str = "binary"
96
+ batch_size: int = 16
97
+ add_start_token: bool = True
98
+ device: Optional[str] = None
99
+
100
+
101
  @evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
102
  class Perplexity(evaluate.Metric):
103
+
104
+ CONFIG_CLASS = PerplexityConfig
105
+ ALLOWED_CONFIG_NAMES = ["default"]
106
+
107
+ def _info(self, config):
108
  return evaluate.MetricInfo(
109
  module_type="metric",
110
  description=_DESCRIPTION,
111
  citation=_CITATION,
112
  inputs_description=_KWARGS_DESCRIPTION,
113
+ config=config,
114
  features=datasets.Features(
115
  {
116
  "predictions": datasets.Value("string"),
 
119
  reference_urls=["https://huggingface.co/docs/transformers/perplexity"],
120
  )
121
 
122
+ def _compute(self, predictions):
123
+
124
+ model_id = self.config.model_id
125
+ device = self.config.device
126
+ batch_size = self.config.batch_size
127
+ add_start_token = self.config.add_start_token
128
 
129
  if device is not None:
130
  assert device in ["gpu", "cpu", "cuda"], "device should be either gpu or cpu."
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- git+https://github.com/huggingface/evaluate@80448674f5447a9682afe051db243c4a13bfe4ff
2
  torch
3
  torch
4
  transformers
 
1
+ git+https://github.com/huggingface/evaluate@e4a2724377909fe2aeb4357e3971e5a569673b39
2
  torch
3
  torch
4
  transformers