Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +50 -0
- arxiv_model.zip +3 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
@st.cache_resource
|
| 7 |
+
def load_model():
|
| 8 |
+
model = AutoModelForSequenceClassification.from_pretrained("arxiv_model")
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained("arxiv_model")
|
| 10 |
+
return model, tokenizer
|
| 11 |
+
|
| 12 |
+
model, tokenizer = load_model()
|
| 13 |
+
id2label = model.config.id2label
|
| 14 |
+
|
| 15 |
+
st.title("🔬 ArXiv Article Classifier")
|
| 16 |
+
|
| 17 |
+
st.markdown("Введите **название** и (по желанию) **аннотацию** статьи. Сервис предскажет вероятные темы!")
|
| 18 |
+
|
| 19 |
+
title_input = st.text_input("Название статьи")
|
| 20 |
+
abstract_input = st.text_area("Аннотация (необязательно)")
|
| 21 |
+
|
| 22 |
+
if st.button("Классифицировать") and title_input:
|
| 23 |
+
text = title_input.strip()
|
| 24 |
+
if abstract_input.strip():
|
| 25 |
+
text += " " + abstract_input.strip()
|
| 26 |
+
|
| 27 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
| 28 |
+
with torch.no_grad():
|
| 29 |
+
outputs = model(**inputs)
|
| 30 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=1).numpy()[0]
|
| 31 |
+
|
| 32 |
+
sorted_indices = np.argsort(probs)[::-1]
|
| 33 |
+
# top_labels = [(id2label[str(i)], probs[i]) for i in sorted_indices]
|
| 34 |
+
top_labels = [(id2label[i], probs[i]) for i in sorted_indices]
|
| 35 |
+
|
| 36 |
+
# Топ-95% по суммарной вероятности
|
| 37 |
+
cumulative = 0.0
|
| 38 |
+
top95 = []
|
| 39 |
+
for label, prob in top_labels:
|
| 40 |
+
top95.append((label, prob))
|
| 41 |
+
cumulative += prob
|
| 42 |
+
if cumulative >= 0.95:
|
| 43 |
+
break
|
| 44 |
+
|
| 45 |
+
st.markdown(f"### 🎯 Основная тема: `{top_labels[0][0]}` ({top_labels[0][1]*100:.2f}%)")
|
| 46 |
+
st.markdown("### 📋 Категории (до 95% суммарной вероятности):")
|
| 47 |
+
for label, prob in top95:
|
| 48 |
+
st.write(f"- `{label}`: {prob*100:.2f}%")
|
| 49 |
+
else:
|
| 50 |
+
st.markdown("_Введите название статьи и нажмите кнопку выше_")
|
arxiv_model.zip
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:06a278334f7ae905b9a6553606501b6a224a798c792c907241c7a05c14c0ffda
|
| 3 |
+
size 247670290
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
streamlit
|