Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -64,34 +64,36 @@ model, tokenizer = load_model()
|
|
| 64 |
|
| 65 |
|
| 66 |
st.markdown("### Угадыватель")
|
| 67 |
-
# st.markdown("<img width=200px src='https://rozetked.me/images/uploads/dwoilp3BVjlE.jpg'>", unsafe_allow_html=True)
|
| 68 |
-
# ^-- можно показывать пользователю текст, картинки, ограниченное подмножество html - всё как в jupyter
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
|
|
|
| 72 |
ans = None
|
| 73 |
|
| 74 |
|
| 75 |
if st.button('Предположить'):
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
if st.button("Посмотреть топ"):
|
| 97 |
if not ans:
|
|
@@ -112,26 +114,29 @@ if st.button("Посмотреть топ"):
|
|
| 112 |
token_type_ids = torch.Tensor(inputs['token_type_ids']).long()
|
| 113 |
|
| 114 |
ans = model(ids.unsqueeze(0), mask.unsqueeze(0), token_type_ids.unsqueeze(0))
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
current_prob
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
}))
|
| 135 |
|
| 136 |
|
| 137 |
|
|
|
|
| 64 |
|
| 65 |
|
| 66 |
st.markdown("### Угадыватель")
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
|
| 69 |
+
title = st.text_area("Title здесь")
|
| 70 |
+
abstract = st.text_area("Abstract здесь")
|
| 71 |
ans = None
|
| 72 |
|
| 73 |
|
| 74 |
if st.button('Предположить'):
|
| 75 |
+
if len(title) == 0 or len(abstract) == 0:
|
| 76 |
+
st.write("Вы ничего не ввели =(")
|
| 77 |
+
else:
|
| 78 |
+
text = title + " : " + abstract
|
| 79 |
+
inputs = tokenizer.encode_plus(
|
| 80 |
+
text,
|
| 81 |
+
None,
|
| 82 |
+
add_special_tokens=True,
|
| 83 |
+
max_length=256,
|
| 84 |
+
pad_to_max_length=True,
|
| 85 |
+
return_token_type_ids=True
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
ids = torch.Tensor(inputs['input_ids']).long()
|
| 90 |
+
mask = torch.Tensor(inputs['attention_mask']).long()
|
| 91 |
+
token_type_ids = torch.Tensor(inputs['token_type_ids']).long()
|
| 92 |
+
|
| 93 |
+
ans = model(ids.unsqueeze(0), mask.unsqueeze(0), token_type_ids.unsqueeze(0))
|
| 94 |
+
idx = torch.nn.functional.softmax(ans[0], dim=0).argmax().item()
|
| 95 |
+
print('ANSLEN', ans.shape)
|
| 96 |
+
st.markdown(f'{idx_to_tag[idx]}')
|
| 97 |
|
| 98 |
if st.button("Посмотреть топ"):
|
| 99 |
if not ans:
|
|
|
|
| 114 |
token_type_ids = torch.Tensor(inputs['token_type_ids']).long()
|
| 115 |
|
| 116 |
ans = model(ids.unsqueeze(0), mask.unsqueeze(0), token_type_ids.unsqueeze(0))
|
| 117 |
+
if len(title) == 0 or len(abstract) == 0:
|
| 118 |
+
st.write("Вы ничего не ввели =(")
|
| 119 |
+
else:
|
| 120 |
+
elems = [el.item() for el in ans[0].argsort(descending=True)]
|
| 121 |
+
probs = ans[0].softmax(dim=0).detach().numpy()
|
| 122 |
+
str_ans = ''
|
| 123 |
+
current_prob = 0
|
| 124 |
+
current_elems = []
|
| 125 |
+
current_probs = []
|
| 126 |
+
idx = 0
|
| 127 |
+
|
| 128 |
+
while current_prob < 0.95 and idx < len(elems):
|
| 129 |
+
current_elems.append(idx_to_tag[elems[idx]])
|
| 130 |
+
current_probs.append(probs[elems[idx]])
|
| 131 |
+
current_prob += probs[elems[idx]]
|
| 132 |
+
idx += 1
|
| 133 |
+
|
| 134 |
|
| 135 |
|
| 136 |
+
st.write(pd.DataFrame({
|
| 137 |
+
'Направление': current_elems,
|
| 138 |
+
'Вероятность': current_probs,
|
| 139 |
+
}))
|
|
|
|
| 140 |
|
| 141 |
|
| 142 |
|