File size: 1,297 Bytes
14bc120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer

def top_proba(preds, p=.95):
    preds.sort(key=lambda pr: pr["score"])
    top_preds = []
    proba = 0.
    for pr in reversed(preds):
        proba += pr["score"]
        top_preds.append(pr)
        if proba >= p:
            return top_preds

@st.cache_resource
def load_model():
    model = AutoModelForSequenceClassification.from_pretrained("Smomitya/distilbert-paper-classifier")
    tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-cased")
    return pipeline("text-classification", model=model, tokenizer=tokenizer, top_k=None)

model = load_model()

st.title("Topic Classification")

title = st.text_input("Enter the title of the article:")
title_only = st.checkbox("Use only title for classification")
if not title_only:
    abstract = st.text_input("Add the abstract or description of the article:")
st.header("Results")
show_scores = st.checkbox("Show scores")
if title and (title_only or abstract):
    query = title if title_only else title + " " + abstract
    preds = top_proba(model(query)[0])
    for pr in preds:
        label = pr["label"]
        score = pr["score"]
        st.markdown(f"`{label}` " + (f"{score:.3f}" if show_scores else ""))