Spaces:
Sleeping
Sleeping
evstifeev.stepan
commited on
Commit
·
920d148
0
Parent(s):
init
Browse files- .gitattributes +35 -0
- README.md +17 -0
- app.py +177 -0
- requirements.txt +4 -0
.gitattributes
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Arxiv Tag Classifier
|
3 |
+
emoji: 🌖
|
4 |
+
colorFrom: green
|
5 |
+
colorTo: indigo
|
6 |
+
sdk: streamlit
|
7 |
+
sdk_version: 1.44.1
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
license: mit
|
11 |
+
short_description: Classify ArXiv paper with ease
|
12 |
+
---
|
13 |
+
|
14 |
+
# ArXiv Paper Classifier
|
15 |
+
A Streamlit web application to predict subject category (or categories) for scientific papers from
|
16 |
+
[arXiv](https://arxiv.org/), using a fine-tuned transformer model.
|
17 |
+
|
app.py
ADDED
@@ -0,0 +1,177 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import logging
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
import streamlit as st
|
5 |
+
import torch
|
6 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
7 |
+
|
8 |
+
logging.basicConfig(level=logging.INFO)
|
9 |
+
logger = logging.getLogger(__name__)
|
10 |
+
|
11 |
+
MAPPING_FROM_TAG_TO_CATEGORY = {
|
12 |
+
"cs.AI": ["Computer Science", "Artificial Intelligence"],
|
13 |
+
"cs.CL": ["Computer Science", "Computation and Language"],
|
14 |
+
"cs.CV": ["Computer Science", "Computer Vision and Pattern Recognition"],
|
15 |
+
"cs.NE": ["Computer Science", "Neural and Evolutionary Computing"],
|
16 |
+
"stat.ML": ["Statistics", "Machine Learning"],
|
17 |
+
"cs.LG": ["Computer Science", "Machine Learning"],
|
18 |
+
"physics.soc-ph": ["Physics", "Physics and Society"],
|
19 |
+
"stat.AP": ["Statistics", "Applications"],
|
20 |
+
"cs.RO": ["Computer Science", "Robotics"],
|
21 |
+
"cs.MA": ["Computer Science", "Multiagent Systems"],
|
22 |
+
"math.OC": ["Mathematics", "Optimization and Control"],
|
23 |
+
"cs.IR": ["Computer Science", "Information Retrieval"],
|
24 |
+
"stat.ME": ["Statistics", "Methodology"],
|
25 |
+
"cs.DC": ["Computer Science", "Distributed, Parallel, and Cluster Computing"],
|
26 |
+
"stat.CO": ["Statistics", "Computation"],
|
27 |
+
"q-bio.NC": ["Quantitative Biology", "Neurons and Cognition"],
|
28 |
+
"cs.GT": ["Computer Science", "Computer Science and Game Theory"],
|
29 |
+
"cs.MM": ["Computer Science", "Multimedia"],
|
30 |
+
"cs.CR": ["Computer Science", "Cryptography and Security"],
|
31 |
+
"cs.HC": ["Computer Science", "Human-Computer Interaction"],
|
32 |
+
"cs.SD": ["Computer Science", "Sound"],
|
33 |
+
"cs.GR": ["Computer Science", "Graphics"],
|
34 |
+
"cs.CY": ["Computer Science", "Computers and Society"],
|
35 |
+
"math.ST": ["Mathematics", "Statistics Theory"],
|
36 |
+
"stat.TH": ["Statistics", "Statistics Theory"],
|
37 |
+
"cs.IT": ["Computer Science", "Information Theory"],
|
38 |
+
"math.IT": ["Mathematics", "Information Theory"],
|
39 |
+
"cs.SI": ["Computer Science", "Social and Information Networks"],
|
40 |
+
"cs.DB": ["Computer Science", "Databases"],
|
41 |
+
"cs.LO": ["Computer Science", "Logic in Computer Science"],
|
42 |
+
"cs.SY": ["Computer Science", "Systems and Control"],
|
43 |
+
"q-bio.QM": ["Quantitative Biology", "Quantitative Methods"],
|
44 |
+
"cs.DS": ["Computer Science", "Data Structures and Algorithms"],
|
45 |
+
"cs.NA": ["Computer Science", "Numerical Analysis"],
|
46 |
+
"cs.CE": ["Computer Science", "Computational Engineering, Finance, and Science"],
|
47 |
+
}
|
48 |
+
|
49 |
+
MODEL_PATH = "minemile/arxiv-tag-classifier"
|
50 |
+
MODEL_MAX_LENGTH = 512
|
51 |
+
CUM_PROB_THRESHOLD = 0.95
|
52 |
+
|
53 |
+
|
54 |
+
@st.cache_resource
|
55 |
+
def load_model_and_tokenizer(model_path):
|
56 |
+
logger.info("Loading model and tokenizer from %s", model_path)
|
57 |
+
try:
|
58 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
59 |
+
model = AutoModelForSequenceClassification.from_pretrained(
|
60 |
+
model_path, device_map="cpu"
|
61 |
+
)
|
62 |
+
model.eval()
|
63 |
+
logger.info("Model and tokenizer loaded successfully.")
|
64 |
+
return model, tokenizer
|
65 |
+
except Exception as e:
|
66 |
+
st.error(f"An error occurred during prediction: {e}")
|
67 |
+
return None, None
|
68 |
+
|
69 |
+
|
70 |
+
def inference(model, tokenizer, title, summary=None):
|
71 |
+
if not model or not tokenizer:
|
72 |
+
st.error("Model or tokenizer not loaded. Cannot predict.")
|
73 |
+
return None, None
|
74 |
+
|
75 |
+
combined_text = title
|
76 |
+
if summary is not None:
|
77 |
+
combined_text += " " + summary
|
78 |
+
|
79 |
+
logger.info("Predicting for text: %s", combined_text)
|
80 |
+
|
81 |
+
try:
|
82 |
+
tokenized_inputs = tokenizer(
|
83 |
+
combined_text,
|
84 |
+
return_tensors="pt",
|
85 |
+
truncation=True,
|
86 |
+
padding=True,
|
87 |
+
max_length=MODEL_MAX_LENGTH,
|
88 |
+
)
|
89 |
+
|
90 |
+
with torch.no_grad():
|
91 |
+
logits = model(**tokenized_inputs).logits
|
92 |
+
probabilities = torch.softmax(logits, dim=-1).squeeze().cpu().numpy()
|
93 |
+
return probabilities
|
94 |
+
|
95 |
+
except Exception as e:
|
96 |
+
st.error(f"An error occurred during prediction: {e}")
|
97 |
+
return None, None
|
98 |
+
|
99 |
+
|
100 |
+
def prepare_output(probabilities, id2label):
|
101 |
+
top_indices = np.argsort(probabilities)[::-1]
|
102 |
+
cum_sum = 0.0
|
103 |
+
top_tags = []
|
104 |
+
top_category = []
|
105 |
+
top_subcategory = []
|
106 |
+
top_probas = []
|
107 |
+
for indx in top_indices:
|
108 |
+
tag = id2label[indx]
|
109 |
+
if tag not in MAPPING_FROM_TAG_TO_CATEGORY:
|
110 |
+
top_tags.append(f"{tag}")
|
111 |
+
top_category.append("Unknown")
|
112 |
+
top_subcategory.append("Unknown")
|
113 |
+
logger.warning("Tag %s not found in mapping from tag to category.", tag)
|
114 |
+
else:
|
115 |
+
top_tags.append(f"{tag}")
|
116 |
+
top_category.append(MAPPING_FROM_TAG_TO_CATEGORY[tag][0])
|
117 |
+
top_subcategory.append(MAPPING_FROM_TAG_TO_CATEGORY[tag][1])
|
118 |
+
top_probas.append(f"{probabilities[indx]*100:.2f}%")
|
119 |
+
cum_sum += probabilities[indx]
|
120 |
+
if cum_sum >= CUM_PROB_THRESHOLD or len(top_tags) >= 5:
|
121 |
+
break
|
122 |
+
return {
|
123 |
+
"Tag": top_tags,
|
124 |
+
"Category": top_category,
|
125 |
+
"Subcategory": top_subcategory,
|
126 |
+
"Probability": top_probas,
|
127 |
+
}
|
128 |
+
|
129 |
+
|
130 |
+
def main():
|
131 |
+
st.set_page_config(page_title="ArXiv Category Tag Classifier", layout="wide")
|
132 |
+
st.title("ArXiv Category Tag Classifier")
|
133 |
+
with st.spinner("Loading model and tokenizer..."):
|
134 |
+
model, tokenizer = load_model_and_tokenizer(MODEL_PATH)
|
135 |
+
if model is None or tokenizer is None:
|
136 |
+
st.error("Failed to load model/tokenizer.")
|
137 |
+
return
|
138 |
+
|
139 |
+
st.markdown(
|
140 |
+
f"Enter the title (required) and summary (abstract) of an ArXiv paper to predict its "
|
141 |
+
f"ArXiv category using a transformer model. There are {len(model.config.id2label)} available categories."
|
142 |
+
)
|
143 |
+
st.divider()
|
144 |
+
col1, col2 = st.columns(2)
|
145 |
+
with col1:
|
146 |
+
title = st.text_input(
|
147 |
+
"Title",
|
148 |
+
placeholder="Enter the title of the paper (Required)",
|
149 |
+
)
|
150 |
+
with col2:
|
151 |
+
paper_summary = st.text_area(
|
152 |
+
"Paper Summary (Optional):",
|
153 |
+
placeholder="Paste the paper's abstract here. It can increase the accuracy of the prediction.",
|
154 |
+
height=180,
|
155 |
+
)
|
156 |
+
|
157 |
+
predict_button = st.button("Predict Category", type="primary")
|
158 |
+
|
159 |
+
st.markdown("---")
|
160 |
+
|
161 |
+
if predict_button:
|
162 |
+
if not title:
|
163 |
+
st.error("Title of the paper is required!")
|
164 |
+
else:
|
165 |
+
with st.spinner("Predicting category..."):
|
166 |
+
class_probabilities = inference(model, tokenizer, title, paper_summary)
|
167 |
+
|
168 |
+
if class_probabilities is not None:
|
169 |
+
st.subheader("Top Tags Predictions:")
|
170 |
+
output = prepare_output(class_probabilities, model.config.id2label)
|
171 |
+
logger.info("Output: %s", output)
|
172 |
+
st.dataframe(output, use_container_width=True)
|
173 |
+
st.markdown("---")
|
174 |
+
|
175 |
+
|
176 |
+
if __name__ == "__main__":
|
177 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
transformers
|
3 |
+
torch
|
4 |
+
numpy
|