Views
No views yet
regression-w-m-vote-epoch-41from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
2from transformers.pipelines import TextClassificationPipeline
3
4class TextRegressionPipeline(TextClassificationPipeline):
5 """
6 Class based on the TextClassificationPipeline from transformers.
7 The difference is that instead of being based on a classifier, it is based on a regressor.
8 You can specify the regression threshold when you call the pipeline or when you instantiate the pipeline.
9 """
10 def __init__(self, **kwargs):
11 """
12 Builds a new Pipeline based on regression.
13 regression_threshold: Optional(float). If None, the pipeline will simply output the score. If set to a specific value, the output will be both the score and the label.
14 """
15 self.regression_threshold = kwargs.pop("regression_threshold", None)
16 super().__init__(**kwargs)
17 def __call__(self, *args, **kwargs):
18 """
19 You can also specify the regression threshold when you call the pipeline.
20 regression_threshold: Optional(float). If None, the pipeline will simply output the score. If set to a specific value, the output will be both the score and the label.
21 """
22 self.regression_threshold_call = kwargs.pop("regression_threshold", None)
23 result = super().__call__(*args, **kwargs)
24 return result
25 def postprocess(self, model_outputs, function_to_apply=None, return_all_scores=False):
26 outputs = model_outputs["logits"][0]
27 outputs = outputs.numpy()
28 scores = outputs
29 score = scores[0]
30 regression_threshold = self.regression_threshold
31 # override the specific threshold if it is specified in the call
32 if self.regression_threshold_call:
33 regression_threshold = self.regression_threshold_call
34 if regression_threshold:
35 return {"label": 'racist' if score > regression_threshold else 'non-racist', "score": score}
36 else:
37 return {"score": score}
38
39
40
41model_name = 'regression-w-m-vote-epoch-4'
42tokenizer = AutoTokenizer.from_pretrained("dccuchile/bert-base-spanish-wwm-uncased")
43full_model_path = f'MartinoMensio/racism-models-{model_name}'
44model = AutoModelForSequenceClassification.from_pretrained(full_model_path)
45
46pipe = TextRegressionPipeline(model=model, tokenizer=tokenizer)
47
48texts = [
49 'y porqué es lo que hay que hacer con los menas y con los adultos también!!!! NO a los inmigrantes ilegales!!!!',
50 'Es que los judíos controlan el mundo'
51]
52# just get the score of regression
53print(pipe(texts))
54# [{'score': 0.8345461}, {'score': 0.48615143}]
55
56# or also specify a threshold to cut racist/non-racist
57print(pipe(texts, regression_threshold=0.9))
58# [{'label': 'non-racist', 'score': 0.8345461}, {'label': 'non-racist', 'score': 0.48615143}]