Spaces:
Runtime error
Runtime error
Upload 6 files
Browse files- app.py +61 -0
- distilbert.safetensors +3 -0
- gpt.safetensors +3 -0
- models.py +27 -0
- requirements.txt +4 -0
- roberta.safetensors +3 -0
app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from utils import get_roberta, get_gpt, get_distilbert
|
3 |
+
import torch
|
4 |
+
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
+
st.title('Sentence Entailment')
|
9 |
+
col1, col2 = st.columns([1,1])
|
10 |
+
|
11 |
+
with col1:
|
12 |
+
sentence1 = st.text_input('Premise')
|
13 |
+
|
14 |
+
with col2:
|
15 |
+
sentence2 = st.text_input('Hypothesis')
|
16 |
+
btn = st.button("Submit")
|
17 |
+
|
18 |
+
label_dict = {
|
19 |
+
0 : 'entailment',
|
20 |
+
1 : 'neutral',
|
21 |
+
2 : 'contradiction'
|
22 |
+
}
|
23 |
+
|
24 |
+
if btn:
|
25 |
+
# Get Roberta Output
|
26 |
+
roberta_tokenizer, roberta_model = get_roberta()
|
27 |
+
roberta_input = roberta_tokenizer(
|
28 |
+
sentence1,
|
29 |
+
sentence2,
|
30 |
+
return_tensors="pt",
|
31 |
+
padding=True,
|
32 |
+
truncation=True,
|
33 |
+
max_length=512
|
34 |
+
)
|
35 |
+
roberta_logits = roberta_model(**roberta_input)['logits']
|
36 |
+
st.write('ROBERTA', label_dict[roberta_logits.argmax().item()])
|
37 |
+
|
38 |
+
distilbert_tokenizer, distilbert_model = get_distilbert()
|
39 |
+
distilbert_input = distilbert_tokenizer(
|
40 |
+
sentence1,
|
41 |
+
sentence2,
|
42 |
+
return_tensors="pt",
|
43 |
+
padding=True,
|
44 |
+
truncation=True,
|
45 |
+
max_length=512
|
46 |
+
)
|
47 |
+
distilbert_logits = distilbert_model(**distilbert_input)['logits']
|
48 |
+
st.write('DistilBERT', label_dict[distilbert_logits.argmax().item()])
|
49 |
+
#
|
50 |
+
|
51 |
+
gpt_tokenizer, gpt_model = get_gpt()
|
52 |
+
gpt_input = gpt_tokenizer(
|
53 |
+
sentence1 + ' [SEP] ' + sentence2,
|
54 |
+
truncation=True,
|
55 |
+
padding='max_length',
|
56 |
+
max_length=512,
|
57 |
+
return_tensors='pt'
|
58 |
+
)
|
59 |
+
|
60 |
+
gpt_logits = gpt_model(**gpt_input)['logits']
|
61 |
+
st.write('GPT', label_dict[gpt_logits.argmax().item()])
|
distilbert.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c1b10cc8d1439918ae5ef54b0bb1c281a849f3bfdc4bd25cd5f5f2ebd3a1fab2
|
3 |
+
size 267835644
|
gpt.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4a507a34036782f5bffa980aa816200cd116e50b8eaa6118d2baab578e00ab4d
|
3 |
+
size 497783504
|
models.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from torch import nn
|
2 |
+
from transformers import RobertaModel, RobertaConfig
|
3 |
+
|
4 |
+
class RobertaSNLI(nn.Module):
|
5 |
+
def __init__(self):
|
6 |
+
super(RobertaSNLI, self).__init__()
|
7 |
+
config = RobertaConfig.from_pretrained('roberta-base')
|
8 |
+
config.output_attentions = True # activer sortie des poids d'attention
|
9 |
+
config.max_position_embeddings = 130 # gérer la longueur des séquences
|
10 |
+
config.hidden_size = 256 # taille des états cachés du modèle
|
11 |
+
config.num_hidden_layers = 4 # nombre de couches cachées dans le transformateur
|
12 |
+
config.intermediate_size = 512 # taille couche intermédiaire dans modèle de transformateur
|
13 |
+
config.num_attention_heads = 4 # nombre de têtes d'attentions
|
14 |
+
|
15 |
+
self.roberta = RobertaModel(config)
|
16 |
+
self.roberta.requires_grad = True
|
17 |
+
self.output = nn.Linear(256, 3) # couche de sortie linéaire. Entrée la taille des états cachées et 3 sorties
|
18 |
+
|
19 |
+
def forward(self, input_ids, attention_mask=None):
|
20 |
+
outputs = self.roberta(input_ids, attention_mask=attention_mask)
|
21 |
+
roberta_out = outputs[0] # séquence des états cachés à la sortie de la dernière couche
|
22 |
+
attentions = outputs.attentions # poids d'attention du modèle RoBERTa
|
23 |
+
|
24 |
+
return self.output(roberta_out[:, 0]), attentions
|
25 |
+
|
26 |
+
|
27 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
safetensors
|
3 |
+
transformers
|
4 |
+
streamlit
|
roberta.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e4a4410d23d0fd92450aaef7f8d3e98665b1bba58601fa3561c07cd65f3e2420
|
3 |
+
size 498615900
|