Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,51 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
-
|
4 |
-
import
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
tokenizer
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
|
3 |
+
import torch
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
MAPPING = {
|
7 |
+
'cs': 'Computer Science', 'econ': 'Economics', 'eess': 'Electrical Engineering and Systems Science', 'math': 'Mathematics',
|
8 |
+
'q-bio': 'Quantitative Biology', 'q-fin': 'Quantitative Finance', 'stat': 'Statistics'
|
9 |
+
}
|
10 |
+
|
11 |
+
@st.cache_resource
|
12 |
+
def load_model():
|
13 |
+
tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-cased')
|
14 |
+
model = DistilBertForSequenceClassification.from_pretrained('model/')
|
15 |
+
return tokenizer, model
|
16 |
+
|
17 |
+
tokenizer, model = load_model()
|
18 |
+
|
19 |
+
st.title('arXiv Article Classifier')
|
20 |
+
title = st.text_input('Title')
|
21 |
+
abstract = st.text_area('Abstract')
|
22 |
+
text = title + ' ' + abstract if abstract else title
|
23 |
+
|
24 |
+
if st.button('Predict'):
|
25 |
+
if not text.strip():
|
26 |
+
st.error('Please enter at least a title.')
|
27 |
+
else:
|
28 |
+
inputs = tokenizer(
|
29 |
+
text,
|
30 |
+
truncation=True,
|
31 |
+
padding=True,
|
32 |
+
max_length=512,
|
33 |
+
return_tensors='pt'
|
34 |
+
)
|
35 |
+
with torch.no_grad():
|
36 |
+
logits = model(**inputs).logits
|
37 |
+
probs = torch.nn.functional.softmax(logits, dim=1).numpy()[0]
|
38 |
+
sorted_indices = np.argsort(-probs)
|
39 |
+
|
40 |
+
cumulative = 0
|
41 |
+
result = []
|
42 |
+
for idx in sorted_indices:
|
43 |
+
cumulative += probs[idx]
|
44 |
+
result.append((model.config.id2label[idx], probs[idx]))
|
45 |
+
if cumulative >= 0.95:
|
46 |
+
break
|
47 |
+
for tag, prob in result:
|
48 |
+
if tag in MAPPING:
|
49 |
+
st.write(f'{MAPPING[tag]}: {prob:.2%}')
|
50 |
+
else:
|
51 |
+
st.write(f'{tag}: {prob:.2%}')
|