File size: 1,663 Bytes
c23c7fd
 
4b87ee5
c0a3229
c23c7fd
c3b53de
c23c7fd
 
71a36a2
c23c7fd
 
 
 
ccf784a
 
 
 
c23c7fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import streamlit as st
import torch
from transformers import AutoTokenizer, AutoModel, AutoModelForSequenceClassification
from safetensors.torch import load_file as safe_load

target_to_ind = {'cs': 0, 'econ': 1, 'eess': 2, 'math': 3, 'phys': 4, 'q-bio': 5, 'q-fin': 6, 'stat': 7}
ind_to_target = {ind: target for target, ind in target_to_ind.items()}


@st.cache_resource
def load_model_and_tokenizer():
    model_name = 'distilbert/distilbert-base-cased'
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=len(target_to_ind))
    
    state_dict = safe_load("model.safetensors")
    model.load_state_dict(state_dict)
    
    return model, tokenizer


def get_predict(title: str, abstract: str) -> (str, float, dict):
    tokenized_text = tokenizer(title + tokenizer.sep_token + abstact[:128], padding="max_length", truncation=True)

    with torch.no_grad():
        outputs = model(tokenized_text)
        probs = torch.nn.functional.softmax(out.logits, dim=-1)

        return list(sorted([(p, ind_to_target[i]) for i, p in enumerate(probs)], reversed=True))


title = st.text_area("Title ", "", height=100)
abstract = st.text_area("Abstract ", "", height=150)

if st.button("Классифицировать", key="manual"):
    if len(title_text) == 0:
        st.error("Please, provide paper's title")
    else:
        with st.spinner("Be patient, I'm doing my best"):
            predict = get_predict(title, abstract)
        st.success(f"Предсказанный тэг: **{predict[0][1]}**")
        
        
model, tokenizer = load_model_and_tokenizer()