Spaces:
Runtime error
Runtime error
Simon Salmon
commited on
Commit
·
a9365ea
1
Parent(s):
3e92229
Update app.py
Browse files
app.py
CHANGED
@@ -15,9 +15,10 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
15 |
from transformers import AutoTokenizer, AutoModelForMaskedLM
|
16 |
tokenizer = AutoTokenizer.from_pretrained("roberta-large")
|
17 |
model = AutoModelForMaskedLM.from_pretrained("BigSalmon/FormalRobertaa")
|
|
|
18 |
|
19 |
|
20 |
-
with st.expander('
|
21 |
with st.form(key='my_form'):
|
22 |
prompt = st.text_area(label='Enter Text. Put <mask> where you want the model to fill in the blank. You can use more than one at a time.')
|
23 |
submit_button = st.form_submit_button(label='Submit')
|
@@ -31,6 +32,25 @@ with st.expander('Expand'):
|
|
31 |
with torch.no_grad():
|
32 |
output = model(token_ids)
|
33 |
last_hidden_state = output[0].squeeze()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
for mask_index in masked_pos:
|
35 |
mask_hidden_state = last_hidden_state[mask_index]
|
36 |
idx = torch.topk(mask_hidden_state, k=100, dim=0)[1]
|
|
|
15 |
from transformers import AutoTokenizer, AutoModelForMaskedLM
|
16 |
tokenizer = AutoTokenizer.from_pretrained("roberta-large")
|
17 |
model = AutoModelForMaskedLM.from_pretrained("BigSalmon/FormalRobertaa")
|
18 |
+
model2 = AutoModelForMaskedLM.from_pretrained("roberta-base")
|
19 |
|
20 |
|
21 |
+
with st.expander('BigSalmon/FormalRobertaa'):
|
22 |
with st.form(key='my_form'):
|
23 |
prompt = st.text_area(label='Enter Text. Put <mask> where you want the model to fill in the blank. You can use more than one at a time.')
|
24 |
submit_button = st.form_submit_button(label='Submit')
|
|
|
32 |
with torch.no_grad():
|
33 |
output = model(token_ids)
|
34 |
last_hidden_state = output[0].squeeze()
|
35 |
+
for mask_index in masked_pos:
|
36 |
+
mask_hidden_state = last_hidden_state[mask_index]
|
37 |
+
idx = torch.topk(mask_hidden_state, k=100, dim=0)[1]
|
38 |
+
words = [tokenizer.decode(i.item()).strip() for i in idx]
|
39 |
+
st.text_area(label = 'Infill:', value=words)
|
40 |
+
|
41 |
+
with st.expander('roberta-large'):
|
42 |
+
with st.form(key='my_form'):
|
43 |
+
prompt = st.text_area(label='Enter Text. Put <mask> where you want the model to fill in the blank. You can use more than one at a time.')
|
44 |
+
submit_button = st.form_submit_button(label='Submit')
|
45 |
+
if submit_button:
|
46 |
+
a_list = []
|
47 |
+
token_ids = tokenizer.encode(prompt, return_tensors='pt')
|
48 |
+
token_ids_tk = tokenizer.tokenize(prompt, return_tensors='pt')
|
49 |
+
masked_position = (token_ids.squeeze() == tokenizer.mask_token_id).nonzero()
|
50 |
+
masked_pos = [mask.item() for mask in masked_position ]
|
51 |
+
with torch.no_grad():
|
52 |
+
output = model2(token_ids)
|
53 |
+
last_hidden_state = output[0].squeeze()
|
54 |
for mask_index in masked_pos:
|
55 |
mask_hidden_state = last_hidden_state[mask_index]
|
56 |
idx = torch.topk(mask_hidden_state, k=100, dim=0)[1]
|