ruanchaves commited on
Commit
9d08804
1 Parent(s): 4e3c7ed

feat: first version

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
+
5
+
6
+ model_list = [
7
+ "ruanchaves/mdeberta-v3-base-assin2-similarity",
8
+ "ruanchaves/bert-base-portuguese-cased-assin2-similarity",
9
+ "ruanchaves/bert-large-portuguese-cased-assin2-similarity",
10
+ "ruanchaves/mdeberta-v3-base-assin-similarity",
11
+ "ruanchaves/bert-base-portuguese-cased-assin-similarity",
12
+ "ruanchaves/bert-large-portuguese-cased-assin-similarity",
13
+ ]
14
+
15
+ model_array = []
16
+
17
+ for model_name in model_list:
18
+ row = {}
19
+ row["tokenizer"] = AutoTokenizer.from_pretrained(model_name)
20
+ row["model"] = AutoModelForSequenceClassification.from_pretrained(model_name)
21
+ model_array.append(row)
22
+
23
+
24
+ def similarity(s1, s2):
25
+ scores = []
26
+ for row in model_array:
27
+ tokenizer = row["tokenizer"]
28
+ model = row["model"]
29
+ model_input = tokenizer(*([s1, s1], [s2, s1]), padding=True, return_tensors="pt")
30
+ with torch.no_grad():
31
+ output = model(**model_input)
32
+ score = output[0][0].item()
33
+ scores.append(score)
34
+ return sum(scores) / len(scores)
35
+
36
+
37
+ inputs = [
38
+ gr.inputs.Textbox(label="Text 1"),
39
+ gr.inputs.Textbox(label="Text 2")
40
+ ]
41
+
42
+ outputs = gr.outputs.Textbox(label="Similarity Score")
43
+
44
+
45
+ gr.Interface(fn=similarity, inputs=inputs, outputs=outputs, title="Semantic Similarity",
46
+ description="Calculates semantic similarity between two pieces of text using multiple pre-trained models.",
47
+ examples=[["A quem é atribuida a invenção do ábaco?", "A primeira ferramenta conhecida para a computação foi o ábaco, cuja invenção é atribuída a habitantes da Mesopotâmia, em torno de 2700–2300 a.C.."],
48
+ ["I love pizza", "Pizza is my favorite food"],
49
+ ["I hate cats", "I love dogs"]]).launch()