ilushado commited on
Commit
ee59b50
·
1 Parent(s): 76830fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -41
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
- title = st.text_area("Title HERE")
71
- abstract = st.text_area("Abstract HERE")
 
72
  ans = None
73
 
74
 
75
  if st.button('Предположить'):
76
- text = title + " : " + abstract
77
- inputs = tokenizer.encode_plus(
78
- text,
79
- None,
80
- add_special_tokens=True,
81
- max_length=256,
82
- pad_to_max_length=True,
83
- return_token_type_ids=True
84
- )
85
-
86
-
87
- ids = torch.Tensor(inputs['input_ids']).long()
88
- mask = torch.Tensor(inputs['attention_mask']).long()
89
- token_type_ids = torch.Tensor(inputs['token_type_ids']).long()
90
-
91
- ans = model(ids.unsqueeze(0), mask.unsqueeze(0), token_type_ids.unsqueeze(0))
92
- idx = torch.nn.functional.softmax(ans[0], dim=0).argmax().item()
93
- print('ANSLEN', ans.shape)
94
- st.markdown(f'{idx_to_tag[idx]}')
 
 
 
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
- elems = [el.item() for el in ans[0].argsort(descending=True)]
116
- probs = ans[0].softmax(dim=0).detach().numpy()
117
- str_ans = ''
118
- current_prob = 0
119
- current_elems = []
120
- current_probs = []
121
- idx = 0
122
-
123
- while current_prob < 0.95 and idx < len(elems):
124
- current_elems.append(idx_to_tag[elems[idx]])
125
- current_probs.append(probs[elems[idx]])
126
- current_prob += probs[elems[idx]]
127
- idx += 1
 
 
 
 
128
 
129
 
130
-
131
- st.write(pd.DataFrame({
132
- 'Направление': current_elems,
133
- 'Вероятность': current_probs,
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