ruanchaves commited on
Commit
3eb176d
1 Parent(s): 1d21e82

feat: textual entailment app

Browse files
Files changed (2) hide show
  1. app.py +131 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
+
5
+ from collections import Counter
6
+
7
+ def most_frequent(array):
8
+ occurence_count = Counter(array)
9
+ return occurence_count.most_common(1)[0][0]
10
+
11
+ article_string = "Author: <a href=\"https://huggingface.co/ruanchaves\">Ruan Chaves Rodrigues</a>. Read more about our <a href=\"https://github.com/ruanchaves/evaluation-portuguese-language-models\">research on the evaluation of Portuguese language models</a>."
12
+
13
+ app_title = "Textual entailment (Implicação)"
14
+
15
+ app_description = """
16
+ This app detects textual entailment and paraphrase between two pieces of Portuguese text using multiple models. You can either introduce your own sentences by filling in "Premise" and "Hypothesis" or click on one of the example pairs provided below.
17
+
18
+ (Este aplicativo detecta implicação e paráfrase entre dois trechos de texto em português usando vários modelos. Introduza suas próprias frases preenchendo "Premise" e "Hypothesis", ou clique em um dos pares de exemplo fornecidos abaixo.)
19
+ """
20
+
21
+ app_examples = [
22
+ ["Uma pessoa tem cabelo loiro e esvoaçante e está tocando violão.", "Um guitarrista tem cabelo loiro e esvoaçante."],
23
+ ["A batata está sendo descascada por uma mulher", "Uma mulher está descascando a batata"],
24
+ ["Uma mulher está temperando um pedaço de carne", "Não tem nenhum homem carregando um rifle com balas"]
25
+ ]
26
+
27
+ output_textbox_component_description = """
28
+ This box will display the relationship between premise and hypothesis based on the average score of multiple models.
29
+
30
+ (Esta caixa exibirá a relação entre premissa e hipótese com base na pontuação média de vários modelos.)
31
+ """
32
+
33
+ output_json_component_description = { "breakdown": """
34
+ This box presents a detailed breakdown of the evaluation for each model,
35
+ displaying the relationship between the text pairs.
36
+ """,
37
+ "detalhamento": """
38
+ (Esta caixa apresenta um detalhamento da avaliação para cada modelo,
39
+ exibindo a relação entre os pares textuais.)
40
+ """ }
41
+
42
+ score_descriptions = {
43
+ 1: "There is an entailment relation between premise and hypothesis. If the premise is true, then the hypothesis must also be true.",
44
+ 0: "There is no logical relation between the premise and the hypothesis.",
45
+ 2: "Additionally, the premise has also been detected as being a paraphrase of the hypothesis."
46
+ }
47
+
48
+
49
+ score_descriptions_pt = {
50
+ 1: "(Existe uma relação de implicação entre premissa e hipótese. Se a premissa é verdadeira, então a hipótese também deve ser verdadeira.)",
51
+ 0: "(Não há relação lógica entre a premissa e a hipótese.)",
52
+ 2: "(Além disso, a premissa também foi detectada como sendo uma paráfrase da hipótese.)"
53
+ }
54
+
55
+ score_short_keys = {
56
+ 0: "No relationship (Nenhuma relação)",
57
+ 1: "Entailment (Implicação)",
58
+ 2: "Paraphrase (Paráfrase)"
59
+ }
60
+
61
+
62
+ model_list = [
63
+ "ruanchaves/mdeberta-v3-base-assin2-entailment",
64
+ "ruanchaves/bert-base-portuguese-cased-assin2-entailment",
65
+ "ruanchaves/bert-large-portuguese-cased-assin2-entailment",
66
+ "ruanchaves/mdeberta-v3-base-assin-entailment",
67
+ "ruanchaves/bert-base-portuguese-cased-assin-entailment",
68
+ "ruanchaves/bert-large-portuguese-cased-assin-entailment",
69
+ ]
70
+
71
+ user_friendly_name = {
72
+ "ruanchaves/mdeberta-v3-base-assin2-entailment": "mDeBERTa-v3 (ASSIN 2)",
73
+ "ruanchaves/bert-base-portuguese-cased-assin2-entailment": "BERTimbau base (ASSIN 2)",
74
+ "ruanchaves/bert-large-portuguese-cased-assin2-entailment": "BERTimbau large (ASSIN 2)",
75
+ "ruanchaves/mdeberta-v3-base-assin-entailment": "mDeBERTa-v3 (ASSIN)",
76
+ "ruanchaves/bert-base-portuguese-cased-assin-entailment": "BERTimbau base (ASSIN)",
77
+ "ruanchaves/bert-large-portuguese-cased-assin-entailment": "BERTimbau large (ASSIN)"
78
+ }
79
+
80
+ model_array = []
81
+
82
+ for model_name in model_list:
83
+ row = {}
84
+ row["name"] = model_name
85
+ row["tokenizer"] = AutoTokenizer.from_pretrained(model_name)
86
+ row["model"] = AutoModelForSequenceClassification.from_pretrained(model_name)
87
+ model_array.append(row)
88
+
89
+
90
+ def entailment(s1, s2):
91
+ scores = {}
92
+ for row in model_array:
93
+ name = user_friendly_name[row["name"]]
94
+ tokenizer = row["tokenizer"]
95
+ model = row["model"]
96
+ model_input = tokenizer(*([s1], [s2]), padding=True, return_tensors="pt")
97
+ with torch.no_grad():
98
+ output = model(**model_input)
99
+ score = output[0][0].argmax().item()
100
+ scores[name] = score
101
+ assin2_scores = {k: v for k, v in scores.items() if "ASSIN 2" in k}
102
+ average_score = most_frequent(assin2_scores.values())
103
+ description = score_descriptions[average_score]
104
+ description_pt = score_descriptions_pt[average_score]
105
+
106
+ if 2 in scores.values():
107
+ description = description + "\n" + score_descriptions[2]
108
+ description_pt = description_pt + "\n" + score_descriptions_pt[2]
109
+ final_description = description + "\n \n" + description_pt
110
+
111
+ for key, value in scores.items():
112
+ scores[key] = score_short_keys[value]
113
+
114
+ return final_description, scores
115
+
116
+
117
+ inputs = [
118
+ gr.inputs.Textbox(label="Premise"),
119
+ gr.inputs.Textbox(label="Hypothesis")
120
+ ]
121
+
122
+ outputs = [
123
+ gr.Textbox(label="Evaluation", value=output_textbox_component_description),
124
+ gr.JSON(label="Results by model", value=output_json_component_description)
125
+ ]
126
+
127
+
128
+ gr.Interface(fn=entailment, inputs=inputs, outputs=outputs, title=app_title,
129
+ description=app_description,
130
+ examples=app_examples,
131
+ article = article_string).launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch
2
+ gradio
3
+ transformers