Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,13 +10,6 @@ from torch.utils.data import TensorDataset, DataLoader, RandomSampler, Sequentia
|
|
| 10 |
st.markdown("### Paper category classification")
|
| 11 |
st.markdown("<img width=200px src='https://rozetked.me/images/uploads/dwoilp3BVjlE.jpg'>", unsafe_allow_html=True)
|
| 12 |
# ^-- можно показывать пользователю текст, картинки, ограниченное подмножество html - всё как в jupyter
|
| 13 |
-
|
| 14 |
-
title = st.text_area("INPUT TITLE HERE")
|
| 15 |
-
abstract = st.text_area("INPUT ABSTRACT HERE")
|
| 16 |
-
# ^-- показать текстовое поле. В поле text лежит строка, которая находится там в данный момент
|
| 17 |
-
if len(title) == 0 and len(abstract):
|
| 18 |
-
st.markdown(f"Could you input paper title/abstrac :)")
|
| 19 |
-
|
| 20 |
@st.cache
|
| 21 |
def load_model_and_tokenizer():
|
| 22 |
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
|
|
@@ -27,85 +20,95 @@ def load_model_and_tokenizer():
|
|
| 27 |
return model, tokenizer
|
| 28 |
|
| 29 |
model, tokenizer = load_model_and_tokenizer()
|
| 30 |
-
MAX_LEN = 64
|
| 31 |
-
# Преобразуем название статьи в токены
|
| 32 |
-
tokens = tokenizer(title, padding=True, truncation=True, return_tensors="pt")
|
| 33 |
-
|
| 34 |
-
# Получаем предсказание модели для названия статьи и абстракта (если есть)
|
| 35 |
-
input_ids = tokens['input_ids']
|
| 36 |
-
attention_mask = tokens['attention_mask']
|
| 37 |
-
logits = model(input_ids, attention_mask)[0]
|
| 38 |
-
|
| 39 |
-
tags_names = ['Accelerator Physics',
|
| 40 |
-
'adap-org',
|
| 41 |
-
"adap-org",
|
| 42 |
-
'Algebra-Geometry',
|
| 43 |
-
'Astro-physics',
|
| 44 |
-
"Astro-physics",
|
| 45 |
-
'Chao-dynamics',
|
| 46 |
-
'Chemistry-physics',
|
| 47 |
-
'cmp-lg',
|
| 48 |
-
"cmp-lg",
|
| 49 |
-
'comp-gas',
|
| 50 |
-
'cond-mat',
|
| 51 |
-
"cond-mat",
|
| 52 |
-
'Computer Science',
|
| 53 |
-
'dg-ga',
|
| 54 |
-
'Economics',
|
| 55 |
-
'eess',
|
| 56 |
-
'funct-an',
|
| 57 |
-
'gr-qc',
|
| 58 |
-
"gr-qc",
|
| 59 |
-
'hep-ex',
|
| 60 |
-
"hep-ex",
|
| 61 |
-
'hep-lat',
|
| 62 |
-
"hep-lat",
|
| 63 |
-
'hep-ph',
|
| 64 |
-
"hep-ph",
|
| 65 |
-
'hep-th',
|
| 66 |
-
"hep-th",
|
| 67 |
-
'Math',
|
| 68 |
-
'math-ph',
|
| 69 |
-
'mtrl-th',
|
| 70 |
-
'nlin',
|
| 71 |
-
'nucl-ex',
|
| 72 |
-
'nucl-th',
|
| 73 |
-
"nucl-th",
|
| 74 |
-
'patt-sol',
|
| 75 |
-
'Physics',
|
| 76 |
-
'q-alg',
|
| 77 |
-
'Quantitie-biology',
|
| 78 |
-
'q-fin',
|
| 79 |
-
'quant-ph',
|
| 80 |
-
"quant-ph",
|
| 81 |
-
'solv-int',
|
| 82 |
-
'Statistics']
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
st.markdown("### Paper category classification")
|
| 11 |
st.markdown("<img width=200px src='https://rozetked.me/images/uploads/dwoilp3BVjlE.jpg'>", unsafe_allow_html=True)
|
| 12 |
# ^-- можно показывать пользователю текст, картинки, ограниченное подмножество html - всё как в jupyter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
@st.cache
|
| 14 |
def load_model_and_tokenizer():
|
| 15 |
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
|
|
|
|
| 20 |
return model, tokenizer
|
| 21 |
|
| 22 |
model, tokenizer = load_model_and_tokenizer()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
title = st.text_area("INPUT TITLE HERE")
|
| 25 |
+
abstract = st.text_area("INPUT ABSTRACT HERE")
|
| 26 |
+
# ^-- показать текстовое поле. В поле text лежит строка, которая находится там в данный момент
|
| 27 |
+
if len(title) == 0 and len(abstract) == 0:
|
| 28 |
+
st.markdown(f"Could you input paper title/abstract :)")
|
| 29 |
+
elif len(title) == 0 and len(abstract) > 0:
|
| 30 |
+
st.markdown(f"Could you input paper title :)")
|
| 31 |
+
else:
|
| 32 |
+
|
| 33 |
+
MAX_LEN = 64
|
| 34 |
+
# Преобразуем название статьи в токены
|
| 35 |
+
tokens = tokenizer(title, padding=True, truncation=True, return_tensors="pt")
|
| 36 |
+
|
| 37 |
+
# Получаем предсказание модели для названия статьи и абстракта (если есть)
|
| 38 |
+
input_ids = tokens['input_ids']
|
| 39 |
+
attention_mask = tokens['attention_mask']
|
| 40 |
+
logits = model(input_ids, attention_mask)[0]
|
| 41 |
+
|
| 42 |
+
tags_names = ['Accelerator Physics',
|
| 43 |
+
'adap-org',
|
| 44 |
+
"adap-org",
|
| 45 |
+
'Algebra-Geometry',
|
| 46 |
+
'Astro-physics',
|
| 47 |
+
"Astro-physics",
|
| 48 |
+
'Chao-dynamics',
|
| 49 |
+
'Chemistry-physics',
|
| 50 |
+
'cmp-lg',
|
| 51 |
+
"cmp-lg",
|
| 52 |
+
'comp-gas',
|
| 53 |
+
'cond-mat',
|
| 54 |
+
"cond-mat",
|
| 55 |
+
'Computer Science',
|
| 56 |
+
'dg-ga',
|
| 57 |
+
'Economics',
|
| 58 |
+
'eess',
|
| 59 |
+
'funct-an',
|
| 60 |
+
'gr-qc',
|
| 61 |
+
"gr-qc",
|
| 62 |
+
'hep-ex',
|
| 63 |
+
"hep-ex",
|
| 64 |
+
'hep-lat',
|
| 65 |
+
"hep-lat",
|
| 66 |
+
'hep-ph',
|
| 67 |
+
"hep-ph",
|
| 68 |
+
'hep-th',
|
| 69 |
+
"hep-th",
|
| 70 |
+
'Math',
|
| 71 |
+
'math-ph',
|
| 72 |
+
'mtrl-th',
|
| 73 |
+
'nlin',
|
| 74 |
+
'nucl-ex',
|
| 75 |
+
'nucl-th',
|
| 76 |
+
"nucl-th",
|
| 77 |
+
'patt-sol',
|
| 78 |
+
'Physics',
|
| 79 |
+
'q-alg',
|
| 80 |
+
'Quantitie-biology',
|
| 81 |
+
'q-fin',
|
| 82 |
+
'quant-ph',
|
| 83 |
+
"quant-ph",
|
| 84 |
+
'solv-int',
|
| 85 |
+
'Statistics']
|
| 86 |
+
|
| 87 |
+
if abstract:
|
| 88 |
+
abstract_tokens = tokenizer(abstract, padding=True, truncation=True, return_tensors="pt")
|
| 89 |
+
abstract_input_ids = abstract_tokens['input_ids']
|
| 90 |
+
abstract_attention_mask = abstract_tokens['attention_mask']
|
| 91 |
+
abstract_logits = model(abstract_input_ids, abstract_attention_mask)[0]
|
| 92 |
+
logits += abstract_logits
|
| 93 |
+
|
| 94 |
+
# Получаем вероятности и сортируем их в порядке убывания
|
| 95 |
+
probs = torch.softmax(logits, dim=-1).squeeze()
|
| 96 |
+
sorted_probs, sorted_indices = torch.sort(probs, descending=True)
|
| 97 |
+
|
| 98 |
+
# Считаем сумму вероятностей
|
| 99 |
+
sum_probs = 0.0
|
| 100 |
+
top_classes = []
|
| 101 |
+
for i in range(len(sorted_probs)):
|
| 102 |
+
sum_probs += sorted_probs[i]
|
| 103 |
+
if sum_probs > 0.95 or sorted_probs[i] < 0.001:
|
| 104 |
+
break
|
| 105 |
+
top_classes.append((tags_names[sorted_indices[i].item()], sorted_probs[i].item()))
|
| 106 |
+
|
| 107 |
+
# Выводим список тем с их вероятностями
|
| 108 |
+
# from transformers import pipeline
|
| 109 |
+
# pipe = pipeline("ner", "Davlan/distilbert-base-multilingual-cased-ner-hrl")
|
| 110 |
+
raw_predictions = top_classes#le.inverse_transform(prediction)#pipe(text)
|
| 111 |
+
# тут уже знакомый вам код с huggingface.transformers -- его можно заменить на что угодно от fairseq до catboost
|
| 112 |
+
|
| 113 |
+
st.markdown(f"Possible categories with their probabilities for this paper : {raw_predictions}")
|
| 114 |
+
# выводим результаты модели в текстовое поле, на потеху пользователю
|