Spaces:
Running
Running
Update Fluency_Score.py
Browse files- Fluency_Score.py +23 -22
Fluency_Score.py
CHANGED
@@ -3,12 +3,13 @@ import datasets
|
|
3 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
4 |
import torch
|
5 |
|
6 |
-
class
|
|
|
7 |
def _info(self):
|
8 |
return evaluate.MetricInfo(
|
9 |
-
description="
|
10 |
citation="",
|
11 |
-
inputs_description="
|
12 |
features=datasets.Features(
|
13 |
{
|
14 |
"texts": datasets.Value("string", id="sequence"),
|
@@ -16,31 +17,31 @@ class FluencyScore(evaluate.Metric):
|
|
16 |
),
|
17 |
reference_urls=[],
|
18 |
)
|
19 |
-
|
20 |
-
def
|
21 |
-
super().__init__()
|
22 |
if device is None:
|
23 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
24 |
-
|
25 |
-
|
26 |
-
# Load the tokenizer and model
|
27 |
self.tokenizer = AutoTokenizer.from_pretrained("Baleegh/Fluency_Score")
|
28 |
self.model = AutoModelForSequenceClassification.from_pretrained("Baleegh/Fluency_Score")
|
29 |
-
|
|
|
|
|
30 |
|
31 |
-
def _compute(self, texts):
|
32 |
-
|
|
|
33 |
inputs = self.tokenizer(
|
34 |
-
texts,
|
35 |
-
return_tensors="pt",
|
36 |
-
truncation=True,
|
37 |
-
padding='max_length',
|
38 |
max_length=128
|
39 |
-
).to(
|
40 |
-
|
41 |
-
|
42 |
-
with torch.no_grad():
|
43 |
output = self.model(**inputs)
|
44 |
-
|
45 |
|
46 |
-
return {"
|
|
|
3 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
4 |
import torch
|
5 |
|
6 |
+
class Fluency_Score(evaluate.Metric):
|
7 |
+
|
8 |
def _info(self):
|
9 |
return evaluate.MetricInfo(
|
10 |
+
description="",
|
11 |
citation="",
|
12 |
+
inputs_description="",
|
13 |
features=datasets.Features(
|
14 |
{
|
15 |
"texts": datasets.Value("string", id="sequence"),
|
|
|
17 |
),
|
18 |
reference_urls=[],
|
19 |
)
|
20 |
+
|
21 |
+
def _download_and_prepare(self, dl_manager, device=None):
|
|
|
22 |
if device is None:
|
23 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
24 |
+
|
25 |
+
# Load the tokenizer and model from the specified repository
|
|
|
26 |
self.tokenizer = AutoTokenizer.from_pretrained("Baleegh/Fluency_Score")
|
27 |
self.model = AutoModelForSequenceClassification.from_pretrained("Baleegh/Fluency_Score")
|
28 |
+
|
29 |
+
self.model.to(device)
|
30 |
+
self.device = device
|
31 |
|
32 |
+
def _compute(self, texts, temperature=2):
|
33 |
+
device = self.device
|
34 |
+
|
35 |
inputs = self.tokenizer(
|
36 |
+
texts,
|
37 |
+
return_tensors="pt",
|
38 |
+
truncation=True,
|
39 |
+
padding='max_length',
|
40 |
max_length=128
|
41 |
+
).to(device)
|
42 |
+
|
43 |
+
with torch.inference_mode():
|
|
|
44 |
output = self.model(**inputs)
|
45 |
+
prediction = output.logits.clip(0, 1)
|
46 |
|
47 |
+
return {"classical_score": prediction}|
|