xu1998hz commited on
Commit
bfa7fa8
1 Parent(s): f93a3d8

add all modules

Browse files
Files changed (4) hide show
  1. README.md +2 -2
  2. __init__.py +38 -0
  3. requirements.txt +2 -1
  4. sescore.py +20 -12
README.md CHANGED
@@ -5,9 +5,9 @@ datasets:
5
  tags:
6
  - evaluate
7
  - metric
8
- description: "TODO: add a description here"
9
  sdk: gradio
10
- sdk_version: 3.0.2
11
  app_file: app.py
12
  pinned: false
13
  ---
 
5
  tags:
6
  - evaluate
7
  - metric
8
+ description: "SEScore: a text generation evaluation metric"
9
  sdk: gradio
10
+ sdk_version: 0.0.1
11
  app_file: app.py
12
  pinned: false
13
  ---
__init__.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import comet
2
+ from typing import Dict
3
+ import torch
4
+ from comet.encoders.base import Encoder
5
+ from comet.encoders.bert import BERTEncoder
6
+ from transformers import AutoModel, AutoTokenizer
7
+
8
+ class robertaEncoder(BERTEncoder):
9
+ def __init__(self, pretrained_model: str) -> None:
10
+ super(Encoder, self).__init__()
11
+ self.tokenizer = AutoTokenizer.from_pretrained(pretrained_model)
12
+ self.model = AutoModel.from_pretrained(
13
+ pretrained_model, add_pooling_layer=False
14
+ )
15
+ self.model.encoder.output_hidden_states = True
16
+
17
+ @classmethod
18
+ def from_pretrained(cls, pretrained_model: str) -> Encoder:
19
+ return robertaEncoder(pretrained_model)
20
+
21
+ def forward(
22
+ self, input_ids: torch.Tensor, attention_mask: torch.Tensor, **kwargs
23
+ ) -> Dict[str, torch.Tensor]:
24
+ last_hidden_states, _, all_layers = self.model(
25
+ input_ids=input_ids,
26
+ attention_mask=attention_mask,
27
+ output_hidden_states=True,
28
+ return_dict=False,
29
+ )
30
+ return {
31
+ "sentemb": last_hidden_states[:, 0, :],
32
+ "wordemb": last_hidden_states,
33
+ "all_layers": all_layers,
34
+ "attention_mask": attention_mask,
35
+ }
36
+
37
+ # initialize roberta into str2encoder
38
+ comet.encoders.str2encoder['RoBERTa'] = robertaEncoder
requirements.txt CHANGED
@@ -1 +1,2 @@
1
- git+https://github.com/huggingface/evaluate@main
 
 
1
+ git+https://github.com/huggingface/evaluate@main
2
+ gdown
sescore.py CHANGED
@@ -11,7 +11,7 @@
11
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
  # See the License for the specific language governing permissions and
13
  # limitations under the License.
14
- """TODO: Add a description here."""
15
 
16
  import evaluate
17
  import datasets
@@ -28,7 +28,7 @@ year={2020}
28
 
29
  # TODO: Add description of the module here
30
  _DESCRIPTION = """\
31
- This new module is designed to solve this great ML task and is crafted with a lot of care.
32
  """
33
 
34
 
@@ -82,14 +82,22 @@ class SEScore(evaluate.Metric):
82
  )
83
 
84
  def _download_and_prepare(self, dl_manager):
85
- """Optional: download external resources useful to compute the scores"""
86
- # TODO: Download external resources if needed
87
- pass
 
 
 
 
 
 
 
 
88
 
89
- def _compute(self, predictions, references):
90
- """Returns the scores"""
91
- # TODO: Compute the different scores of the module
92
- accuracy = sum(i == j for i, j in zip(predictions, references)) / len(predictions)
93
- return {
94
- "accuracy": accuracy,
95
- }
 
11
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
  # See the License for the specific language governing permissions and
13
  # limitations under the License.
14
+ """SEScore: a text generation evaluation metric"""
15
 
16
  import evaluate
17
  import datasets
 
28
 
29
  # TODO: Add description of the module here
30
  _DESCRIPTION = """\
31
+ SEScore is an evaluation metric that trys to compute an overall score to measure text generation quality.
32
  """
33
 
34
 
 
82
  )
83
 
84
  def _download_and_prepare(self, dl_manager):
85
+ """download SEScore checkpoints to compute the scores"""
86
+ # Download SEScore checkpoint
87
+ from comet import load_from_checkpoint
88
+ import gdown
89
+ import os
90
+ url = "https://drive.google.com/uc?id=1QgMP_Y4QCbvDMTeVacYt0J76OYvwWK9V&export=download&confirm=true"
91
+ output = 'sescore_download.gz'
92
+ gdown.download(url, output, quiet=False)
93
+ cmd = 'tar -xvf sescore_download.gz'
94
+ os.system(cmd)
95
+ self.scorer = load_from_checkpoint('sescore_download/zh_en/checkpoint/sescore_english.ckpt')
96
 
97
+ def _compute(self, sources, predictions, references, gpus=None, progress_bar=False):
98
+ if gpus is None:
99
+ gpus = 1 if torch.cuda.is_available() else 0
100
+ data = {"src": references, "mt": predictions}
101
+ data = [dict(zip(data, t)) for t in zip(*data.values())]
102
+ scores, mean_score = self.scorer.predict(data, gpus=gpus, progress_bar=progress_bar)
103
+ return {"mean_score": mean_score, "scores": scores}