File size: 2,299 Bytes
0bfbe31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3805e6f
0bfbe31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import json
import streamlit as st
import torch
import torch.nn.functional as F
from transformers import AutoTokenizer, DistilBertForSequenceClassification

CHECKPOINT_PATH = "checkpoints/epoch_8.pt"
LABELS_PATH = "checkpoints/labels_info.json"

with open(LABELS_PATH, 'r') as f:
    LABELS = json.load(f)

print(len(LABELS))
BASE_MODEL = "distilbert-base-cased"

@st.cache_resource
def load_model():
    tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
    # The same model
    model = DistilBertForSequenceClassification.from_pretrained(BASE_MODEL, num_labels=len(LABELS))
    state_dict = torch.load(CHECKPOINT_PATH, map_location=torch.device('cpu'))
    model.load_state_dict(state_dict)
    model.eval()

    return tokenizer, model

tokenizer, model = load_model()

st.title("Классификатор научных статей по заголовку и описанию")
st.write("Введите название и аннотацию статьи для предсказания её тематики по таксономии arxiv.org")

title = st.text_input("Название статьи:")
abstract = st.text_area("Аннотация (abstract):")

if st.button("Классифицировать"):
    if not title and not abstract:
        st.warning("Введите хотя бы название статьи.")
    else:
        text = title if not abstract else f"{title} {abstract}"

        inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=256)

        with torch.no_grad():
            outputs = model(**inputs)
            probs = F.softmax(outputs.logits, dim=1).squeeze()

        label_probs = [(label, prob.item()) for label, prob in zip(list(LABELS.values()), probs)]

        # Sorting for getting 95% afterwards
        label_probs.sort(key=lambda x: x[1], reverse=True)

        cumulative = 0.0
        top_labels = []
        for label, prob in label_probs:
            cumulative += prob
            top_labels.append((label, prob))
            if cumulative >= 0.95:
                break

        # Вывод
        st.subheader("Наиболее вероятные тематики (суммарно ≥95%):")
        for label, prob in top_labels:
            st.write(f"**{label}** — {prob * 100:.2f}%")