Spaces:
Running
Running
Update Fluency_Score.py
Browse files- Fluency_Score.py +23 -24
Fluency_Score.py
CHANGED
@@ -3,13 +3,12 @@ import datasets
|
|
3 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
4 |
import torch
|
5 |
|
6 |
-
class
|
7 |
-
|
8 |
def _info(self):
|
9 |
-
return evaluate.
|
10 |
-
description="",
|
11 |
citation="",
|
12 |
-
inputs_description="",
|
13 |
features=datasets.Features(
|
14 |
{
|
15 |
"texts": datasets.Value("string", id="sequence"),
|
@@ -17,31 +16,31 @@ class Fluency_Score(evaluate.Measurement):
|
|
17 |
),
|
18 |
reference_urls=[],
|
19 |
)
|
20 |
-
|
21 |
-
def
|
|
|
22 |
if device is None:
|
23 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
24 |
-
|
25 |
-
|
|
|
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
|
33 |
-
|
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 |
-
|
|
|
44 |
output = self.model(**inputs)
|
45 |
-
|
46 |
|
47 |
-
return {"
|
|
|
3 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
4 |
import torch
|
5 |
|
6 |
+
class FluencyScore(evaluate.Metric):
|
|
|
7 |
def _info(self):
|
8 |
+
return evaluate.MetricInfo(
|
9 |
+
description="Computes the fluency score of a given text using a pre-trained model.",
|
10 |
citation="",
|
11 |
+
inputs_description="A list of text strings to evaluate for fluency.",
|
12 |
features=datasets.Features(
|
13 |
{
|
14 |
"texts": datasets.Value("string", id="sequence"),
|
|
|
16 |
),
|
17 |
reference_urls=[],
|
18 |
)
|
19 |
+
|
20 |
+
def __init__(self, device=None):
|
21 |
+
super().__init__()
|
22 |
if device is None:
|
23 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
24 |
+
self.device = device
|
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 |
+
self.model.to(self.device)
|
|
|
|
|
30 |
|
31 |
+
def _compute(self, texts):
|
32 |
+
# Tokenize the input texts
|
|
|
33 |
inputs = self.tokenizer(
|
34 |
+
texts,
|
35 |
+
return_tensors="pt",
|
36 |
+
truncation=True,
|
37 |
+
padding='max_length',
|
38 |
max_length=128
|
39 |
+
).to(self.device)
|
40 |
+
|
41 |
+
# Get model predictions
|
42 |
+
with torch.no_grad():
|
43 |
output = self.model(**inputs)
|
44 |
+
predictions = output.logits.clip(0, 1).squeeze().tolist() # Convert to list
|
45 |
|
46 |
+
return {"fluency_scores": predictions}
|