Spaces:
Sleeping
Sleeping
Merge branch 'main' of https://huggingface.co/spaces/ronaldahmed/ccl_win into main
Browse files- ccl_win.py +92 -10
ccl_win.py
CHANGED
@@ -15,6 +15,13 @@
|
|
15 |
|
16 |
import evaluate
|
17 |
import datasets
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
|
20 |
# TODO: Add BibTeX citation
|
@@ -28,7 +35,7 @@ year={2020}
|
|
28 |
|
29 |
# TODO: Add description of the module here
|
30 |
_DESCRIPTION = """\
|
31 |
-
|
32 |
"""
|
33 |
|
34 |
|
@@ -55,11 +62,12 @@ Examples:
|
|
55 |
|
56 |
# TODO: Define external resources urls if needed
|
57 |
BAD_WORDS_URL = "http://url/to/external/resource/bad_words.txt"
|
58 |
-
|
59 |
|
60 |
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
|
61 |
class ccl_win(evaluate.Measurement):
|
62 |
"""TODO: Short description of my evaluation module."""
|
|
|
63 |
|
64 |
def _info(self):
|
65 |
# TODO: Specifies the evaluate.EvaluationModuleInfo object
|
@@ -71,8 +79,7 @@ class ccl_win(evaluate.Measurement):
|
|
71 |
inputs_description=_KWARGS_DESCRIPTION,
|
72 |
# This defines the format of each prediction and reference
|
73 |
features=datasets.Features({
|
74 |
-
'predictions': datasets.Value('
|
75 |
-
'references': datasets.Value('int64'),
|
76 |
}),
|
77 |
# Homepage of the module for documentation
|
78 |
homepage="http://module.homepage",
|
@@ -86,10 +93,85 @@ class ccl_win(evaluate.Measurement):
|
|
86 |
# TODO: Download external resources if needed
|
87 |
pass
|
88 |
|
89 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
"""Returns the scores"""
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
"
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
import evaluate
|
17 |
import datasets
|
18 |
+
import numpy as np
|
19 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
20 |
+
import getpass
|
21 |
+
import pdb
|
22 |
+
import os
|
23 |
+
import torch
|
24 |
+
from rouge_score import scoring
|
25 |
|
26 |
|
27 |
# TODO: Add BibTeX citation
|
|
|
35 |
|
36 |
# TODO: Add description of the module here
|
37 |
_DESCRIPTION = """\
|
38 |
+
local coherecence with classifier trained on the shuffle task, window=3 sentences
|
39 |
"""
|
40 |
|
41 |
|
|
|
62 |
|
63 |
# TODO: Define external resources urls if needed
|
64 |
BAD_WORDS_URL = "http://url/to/external/resource/bad_words.txt"
|
65 |
+
WINDOW_SIZE = 3
|
66 |
|
67 |
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
|
68 |
class ccl_win(evaluate.Measurement):
|
69 |
"""TODO: Short description of my evaluation module."""
|
70 |
+
|
71 |
|
72 |
def _info(self):
|
73 |
# TODO: Specifies the evaluate.EvaluationModuleInfo object
|
|
|
79 |
inputs_description=_KWARGS_DESCRIPTION,
|
80 |
# This defines the format of each prediction and reference
|
81 |
features=datasets.Features({
|
82 |
+
'predictions': datasets.Value('string'),
|
|
|
83 |
}),
|
84 |
# Homepage of the module for documentation
|
85 |
homepage="http://module.homepage",
|
|
|
93 |
# TODO: Download external resources if needed
|
94 |
pass
|
95 |
|
96 |
+
def preprocess_adjacent_window(self,preds):
|
97 |
+
pred_list = []
|
98 |
+
lens = []
|
99 |
+
for pred in preds:
|
100 |
+
sents = pred.split("\n")
|
101 |
+
ns = len(sents)
|
102 |
+
if ns <= WINDOW_SIZE:
|
103 |
+
pred_list.append(pred)
|
104 |
+
lens.append(1)
|
105 |
+
else:
|
106 |
+
llen = 0
|
107 |
+
for i in range(0,ns-WINDOW_SIZE+1):
|
108 |
+
sss = sents[i:i+WINDOW_SIZE]
|
109 |
+
ss = "\n".join(sss)
|
110 |
+
pred_list.append(ss)
|
111 |
+
llen += 1
|
112 |
+
lens.append(llen)
|
113 |
+
#
|
114 |
+
return pred_list,lens
|
115 |
+
|
116 |
+
|
117 |
+
|
118 |
+
def _compute(self, predictions, dataset="arxiv", batch_size: int = 16, device=None, use_aggregator=True):
|
119 |
"""Returns the scores"""
|
120 |
+
MODEL_CACHE_DIR = "/home/rcardena/.cache/huggingface/"
|
121 |
+
BASEDIR = "/gfs/team/nlp/users/rcardena/tools/new_evals/ccl_win"
|
122 |
+
if getpass.getuser() == "s1987051":
|
123 |
+
MODEL_CACHE_DIR="/disk/ocean/rcardenas/tools/huggingface/"
|
124 |
+
elif getpass.getuser() == "rcardena":
|
125 |
+
MODEL_CACHE_DIR="/gfs/team/nlp/users/rcardena/tools/huggingface/"
|
126 |
+
|
127 |
+
if device is not None:
|
128 |
+
# assert device in ["gpu", "cpu", "cuda"], "device should be either gpu or cpu."
|
129 |
+
if device == "gpu":
|
130 |
+
device = "cuda"
|
131 |
+
else:
|
132 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
133 |
+
|
134 |
+
results = []
|
135 |
+
sent_lens = [len(x.split("\n")) for x in predictions]
|
136 |
+
aggregator = None
|
137 |
+
if use_aggregator:
|
138 |
+
np.random.seed(42)
|
139 |
+
aggregator = scoring.BootstrapAggregator()
|
140 |
+
|
141 |
+
tokenizer = AutoTokenizer.from_pretrained("roberta-large")
|
142 |
+
|
143 |
+
model = AutoModelForSequenceClassification.from_pretrained(os.path.join(BASEDIR,dataset))
|
144 |
+
model.to(device)
|
145 |
+
model.eval()
|
146 |
+
|
147 |
+
pred_list,len_by_sample = self.preprocess_adjacent_window(predictions)
|
148 |
+
|
149 |
+
scores = []
|
150 |
+
n_preds = len(pred_list)
|
151 |
+
with torch.no_grad():
|
152 |
+
for b in range(0,n_preds,batch_size):
|
153 |
+
strides = [x.lower() for x in pred_list[b:b+batch_size]]
|
154 |
+
tinput = tokenizer(strides,padding=True,truncation=True,max_length=512,return_tensors="pt")
|
155 |
+
tinput = {k:v.to(device) for k,v in tinput.items()}
|
156 |
+
output = model(**tinput)
|
157 |
+
probs = torch.softmax(output.logits,dim=-1).detach().cpu().numpy()
|
158 |
+
scores.extend(probs[:,0].tolist())
|
159 |
+
#
|
160 |
+
|
161 |
+
offset = 0
|
162 |
+
for i,_len in enumerate(len_by_sample):
|
163 |
+
score = float(np.mean(scores[offset:offset+_len])) if sent_lens[i]>1 else 0.
|
164 |
+
if use_aggregator:
|
165 |
+
aggregator.add_scores({"loc_coh_ccl": score})
|
166 |
+
else:
|
167 |
+
results.append(score)
|
168 |
+
offset += _len
|
169 |
+
#
|
170 |
+
outres = {}
|
171 |
+
if use_aggregator:
|
172 |
+
res = aggregator.aggregate()
|
173 |
+
for k in res: outres[k] = res[k].mid
|
174 |
+
else:
|
175 |
+
outres = {"loc_coh_ccl": results}
|
176 |
+
|
177 |
+
return outres
|