Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import DistilBertForSequenceClassification, DistilBertTokenizerFast
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
target_to_ind =
|
| 7 |
+
{'cs': 0,
|
| 8 |
+
'econ': 1,
|
| 9 |
+
'eess': 2,
|
| 10 |
+
'math': 3,
|
| 11 |
+
'phys': 4,
|
| 12 |
+
'q-bio': 5,
|
| 13 |
+
'q-fin': 6,
|
| 14 |
+
'stat': 7}
|
| 15 |
+
|
| 16 |
+
ind_to_target = {ind: target for target, ind in target_to_ind.items()}
|
| 17 |
+
|
| 18 |
+
@st.cache_resource
|
| 19 |
+
def load_model_and_tokenizer():
|
| 20 |
+
model_name = 'distilbert/distilbert-base-cased'
|
| 21 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 22 |
+
model = AutoModelForSequenceClassification.from_pretrained("./weights/model.safetensors", num_labels=len(target_to_ind))
|
| 23 |
+
|
| 24 |
+
return model, tokenizer
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def get_predict(title: str, abstract: str) -> (str, float, dict):
|
| 28 |
+
tokenized_text = tokenizer(title + tokenizer.sep_token + abstact[:128], padding="max_length", truncation=True)
|
| 29 |
+
|
| 30 |
+
with torch.no_grad():
|
| 31 |
+
outputs = model(tokenized_text)
|
| 32 |
+
probs = torch.nn.functional.softmax(out.logits, dim=-1)
|
| 33 |
+
|
| 34 |
+
return list(sorted([(p, ind_to_target[i]) for i, p in enumerate(probs)], reversed=True))
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
title = st.text_area("Title ", "", height=100)
|
| 38 |
+
abstract = st.text_area("Abstract ", "", height=150)
|
| 39 |
+
|
| 40 |
+
if st.button("Классифицировать", key="manual"):
|
| 41 |
+
if len(title_text) == 0:
|
| 42 |
+
st.error("Please, provide paper's title")
|
| 43 |
+
else:
|
| 44 |
+
with st.spinner("Be patient, I'm doing my best"):
|
| 45 |
+
predict = get_predict(title, abstract)
|
| 46 |
+
st.success(f"Предсказанный тэг: **{predict[0][1]}**")
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
model, tokenizer = load_model_and_tokenizer()
|