Update app.py
Browse files
app.py
CHANGED
@@ -1,147 +1,148 @@
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
3 |
|
4 |
-
id_to_cat = {
|
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 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
|
|
|
|
142 |
|
143 |
-
|
144 |
-
# @st.cache_resource
|
145 |
def load_model():
|
146 |
tokenizer = AutoTokenizer.from_pretrained('distilbert-base-cased')
|
147 |
model = AutoModelForSequenceClassification.from_pretrained(
|
@@ -151,76 +152,61 @@ def load_model():
|
|
151 |
)
|
152 |
return model, tokenizer
|
153 |
|
154 |
-
# Load model/tokenizer once and cache it
|
155 |
try:
|
156 |
model, tokenizer = load_model()
|
157 |
except OSError as e:
|
158 |
-
st.error(f"Ошибка
|
159 |
st.stop()
|
160 |
|
161 |
-
|
162 |
def classify_text(title, description):
|
163 |
-
""
|
164 |
-
Классифицирует текст и возвращает результаты в отсортированном виде.
|
165 |
-
Args:
|
166 |
-
title (str): Заголовок текста.
|
167 |
-
description (str): Краткое описание текста.
|
168 |
-
Returns:
|
169 |
-
list: Отсортированный список результатов классификации.
|
170 |
-
"""
|
171 |
-
text = f"{title} {description}" # Объединяем заголовок и описание
|
172 |
-
topic_classifier = pipeline("text-classification", model=model, tokenizer=tokenizer, top_k = len(id_to_cat))
|
173 |
try:
|
174 |
-
|
|
|
175 |
except Exception as e:
|
176 |
-
st.error(f"Ошибка
|
177 |
return []
|
178 |
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
filtered_results.append((i['label'], i['score']))
|
185 |
-
return filtered_results
|
186 |
-
|
187 |
|
188 |
-
|
189 |
-
st.title("Классификация статей
|
|
|
190 |
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
top = st.text_input("Top x%")
|
195 |
|
196 |
-
|
197 |
-
if st.button("Классифицировать"):
|
198 |
if not title and not description:
|
199 |
-
st.warning("Пожалуйста,
|
200 |
else:
|
201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
202 |
results = classify_text(title, description)
|
|
|
203 |
if results:
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
elif (float(top) > 1):
|
212 |
-
t = float(top) / 100
|
213 |
-
except ValueError:
|
214 |
-
t = 0.95
|
215 |
-
|
216 |
-
st.subheader(f'Результаты классификации (top {int(min(t * 100, 100))}%):')
|
217 |
-
for label, score in results:
|
218 |
-
st.write(f"- **{label}**: {score:.4f}")
|
219 |
-
cumulative_prob += score
|
220 |
-
if cumulative_prob >= t:
|
221 |
-
break
|
222 |
else:
|
223 |
st.info("Не удалось получить результаты классификации.")
|
224 |
-
|
225 |
-
|
226 |
-
st.warning("Пожалуйста, заполните хотя бы одно поле.")
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
3 |
|
4 |
+
id_to_cat = {
|
5 |
+
0: 'Cryptography and Security',
|
6 |
+
1: 'Medical Physics',
|
7 |
+
2: 'Audio and Speech Processing',
|
8 |
+
3: 'Combinatorics',
|
9 |
+
4: 'Information Theory',
|
10 |
+
5: 'Quantum Physics',
|
11 |
+
6: 'Nuclear Theory',
|
12 |
+
7: 'Computers and Society',
|
13 |
+
8: 'Pattern Formation and Solitons',
|
14 |
+
9: 'General Finance',
|
15 |
+
10: 'Multiagent Systems',
|
16 |
+
11: 'Trading and Market Microstructure',
|
17 |
+
12: 'Mesoscale and Nanoscale Physics',
|
18 |
+
13: 'Instrumentation and Detectors',
|
19 |
+
14: 'Emerging Technologies',
|
20 |
+
15: 'Software Engineering',
|
21 |
+
16: 'Computational Physics',
|
22 |
+
17: 'Econometrics',
|
23 |
+
18: 'Materials Science',
|
24 |
+
19: 'Computer Vision and Pattern Recognition',
|
25 |
+
20: 'Differential Geometry',
|
26 |
+
21: 'General Literature',
|
27 |
+
22: 'Computation and Language',
|
28 |
+
23: 'Superconductivity',
|
29 |
+
24: 'Risk Management',
|
30 |
+
25: 'Other Condensed Matter',
|
31 |
+
26: 'Other Quantitative Biology',
|
32 |
+
27: 'High Energy Physics - Phenomenology',
|
33 |
+
28: 'Analysis of PDEs',
|
34 |
+
29: 'Earth and Planetary Astrophysics',
|
35 |
+
30: 'Optics',
|
36 |
+
31: 'Hardware Architecture',
|
37 |
+
32: 'Optimization and Control',
|
38 |
+
33: 'Methodology',
|
39 |
+
34: 'Number Theory',
|
40 |
+
35: 'General Topology',
|
41 |
+
36: 'Populations and Evolution',
|
42 |
+
37: 'Solar and Stellar Astrophysics',
|
43 |
+
38: 'Distributed, Parallel, and Cluster Computing',
|
44 |
+
39: 'Chaotic Dynamics',
|
45 |
+
40: 'History and Philosophy of Physics',
|
46 |
+
41: 'Computational Engineering, Finance, and Science',
|
47 |
+
42: 'Discrete Mathematics',
|
48 |
+
43: 'Statistical Mechanics',
|
49 |
+
44: 'Operating Systems',
|
50 |
+
45: 'Data Structures and Algorithms',
|
51 |
+
46: 'Geophysics',
|
52 |
+
47: 'Quantum Algebra',
|
53 |
+
48: 'Systems and Control',
|
54 |
+
49: 'Statistics Theory',
|
55 |
+
50: 'High Energy Physics - Theory',
|
56 |
+
51: 'Rings and Algebras',
|
57 |
+
52: 'Neural and Evolutionary Computing',
|
58 |
+
53: 'General Physics',
|
59 |
+
54: 'Computational Geometry',
|
60 |
+
55: 'Signal Processing',
|
61 |
+
56: 'Computational Finance',
|
62 |
+
57: 'History and Overview',
|
63 |
+
58: 'Space Physics',
|
64 |
+
59: 'Physics and Society',
|
65 |
+
60: 'Cosmology and Nongalactic Astrophysics',
|
66 |
+
61: 'Information Retrieval',
|
67 |
+
62: 'Symbolic Computation',
|
68 |
+
63: 'Statistical Finance',
|
69 |
+
64: 'Image and Video Processing',
|
70 |
+
65: 'Quantum Gases',
|
71 |
+
66: 'Artificial Intelligence',
|
72 |
+
67: 'Nuclear Experiment',
|
73 |
+
68: 'General Mathematics',
|
74 |
+
69: 'Complex Variables',
|
75 |
+
70: 'Logic in Computer Science',
|
76 |
+
71: 'Data Analysis, Statistics and Probability',
|
77 |
+
72: 'Fluid Dynamics',
|
78 |
+
73: 'Dynamical Systems',
|
79 |
+
74: 'High Energy Astrophysical Phenomena',
|
80 |
+
75: 'Programming Languages',
|
81 |
+
76: 'Mathematical Physics',
|
82 |
+
77: 'Logic',
|
83 |
+
78: 'Social and Information Networks',
|
84 |
+
79: 'Numerical Analysis',
|
85 |
+
80: 'Sound',
|
86 |
+
81: 'Chemical Physics',
|
87 |
+
82: 'Genomics',
|
88 |
+
83: 'Instrumentation and Methods for Astrophysics',
|
89 |
+
84: 'Applications',
|
90 |
+
85: 'Representation Theory',
|
91 |
+
86: 'Machine Learning',
|
92 |
+
87: 'Formal Languages and Automata Theory',
|
93 |
+
88: 'Quantitative Methods',
|
94 |
+
89: 'Atmospheric and Oceanic Physics',
|
95 |
+
90: 'Subcellular Processes',
|
96 |
+
91: 'Networking and Internet Architecture',
|
97 |
+
92: 'Functional Analysis',
|
98 |
+
93: 'Metric Geometry',
|
99 |
+
94: 'General Relativity and Quantum Cosmology',
|
100 |
+
95: 'Spectral Theory',
|
101 |
+
96: 'Graphics',
|
102 |
+
97: 'Adaptation and Self-Organizing Systems',
|
103 |
+
98: 'Economics',
|
104 |
+
99: 'Classical Analysis and ODEs',
|
105 |
+
100: 'Other Computer Science',
|
106 |
+
101: 'Geometric Topology',
|
107 |
+
102: 'Pricing of Securities',
|
108 |
+
103: 'High Energy Physics - Experiment',
|
109 |
+
104: 'Category Theory',
|
110 |
+
105: 'Human-Computer Interaction',
|
111 |
+
106: 'Biological Physics',
|
112 |
+
107: 'Popular Physics',
|
113 |
+
108: 'Probability',
|
114 |
+
109: 'Commutative Algebra',
|
115 |
+
110: 'Strongly Correlated Electrons',
|
116 |
+
111: 'Group Theory',
|
117 |
+
112: 'Computation',
|
118 |
+
113: 'Digital Libraries',
|
119 |
+
114: 'Classical Physics',
|
120 |
+
115: 'Neurons and Cognition',
|
121 |
+
116: 'Operator Algebras',
|
122 |
+
117: 'Tissues and Organs',
|
123 |
+
118: 'High Energy Physics - Lattice',
|
124 |
+
119: 'Robotics',
|
125 |
+
120: 'Portfolio Management',
|
126 |
+
121: 'Computational Complexity',
|
127 |
+
122: 'Soft Condensed Matter',
|
128 |
+
123: 'Mathematical Software',
|
129 |
+
124: 'Applied Physics',
|
130 |
+
125: 'Computer Science and Game Theory',
|
131 |
+
126: 'Multimedia',
|
132 |
+
127: 'Molecular Networks',
|
133 |
+
128: 'Disordered Systems and Neural Networks',
|
134 |
+
129: 'Other Statistics',
|
135 |
+
130: 'Cell Behavior',
|
136 |
+
131: 'Performance',
|
137 |
+
132: 'Biomolecules',
|
138 |
+
133: 'Astrophysics of Galaxies',
|
139 |
+
134: 'Databases',
|
140 |
+
135: 'Algebraic Topology',
|
141 |
+
136: 'Cellular Automata and Lattice Gases',
|
142 |
+
137: 'Algebraic Geometry'
|
143 |
+
}
|
144 |
|
145 |
+
@st.cache_resource
|
|
|
146 |
def load_model():
|
147 |
tokenizer = AutoTokenizer.from_pretrained('distilbert-base-cased')
|
148 |
model = AutoModelForSequenceClassification.from_pretrained(
|
|
|
152 |
)
|
153 |
return model, tokenizer
|
154 |
|
|
|
155 |
try:
|
156 |
model, tokenizer = load_model()
|
157 |
except OSError as e:
|
158 |
+
st.error(f"Ошибка при загрузке модели: {e}")
|
159 |
st.stop()
|
160 |
|
|
|
161 |
def classify_text(title, description):
|
162 |
+
text = f"{title.strip()} {description.strip()}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
try:
|
164 |
+
classifier = pipeline("text-classification", model=model, tokenizer=tokenizer, top_k=len(id_to_cat))
|
165 |
+
results = classifier(text)
|
166 |
except Exception as e:
|
167 |
+
st.error(f"Ошибка при классификации текста: {e}")
|
168 |
return []
|
169 |
|
170 |
+
readable_results = [
|
171 |
+
(id_to_cat[int(entry['label'].split('_')[1])], entry['score'])
|
172 |
+
for entry in results[0]
|
173 |
+
]
|
174 |
+
return readable_results
|
|
|
|
|
|
|
175 |
|
176 |
+
st.set_page_config(page_title="Классификация статей", layout="wide")
|
177 |
+
st.title("🔬 Классификация научных статей")
|
178 |
+
st.markdown("Введите заголовок и краткое описание научной статьи, чтобы определить её тематические категории.")
|
179 |
|
180 |
+
title = st.text_input("📝 Заголовок статьи", placeholder="Например: Deep Learning for Image Recognition")
|
181 |
+
description = st.text_area("🧾 Краткое описание статьи", height=150, placeholder="Кратко опишите содержание статьи...")
|
182 |
+
top_percent = st.text_input("📊 Порог вероятности (например, 95 или 0.95 для top 95%)", value="95")
|
|
|
183 |
|
184 |
+
if st.button("🚀 Классифицировать"):
|
|
|
185 |
if not title and not description:
|
186 |
+
st.warning("Пожалуйста, введите заголовок или описание статьи.")
|
187 |
else:
|
188 |
+
try:
|
189 |
+
t = float(top_percent)
|
190 |
+
if t > 1:
|
191 |
+
t = t / 100
|
192 |
+
if not (0 < t <= 1):
|
193 |
+
raise ValueError()
|
194 |
+
except ValueError:
|
195 |
+
st.warning("Некорректное значение для порога вероятности. Используем значение по умолчанию: 95%.")
|
196 |
+
t = 0.95
|
197 |
+
|
198 |
+
with st.spinner("🔍 Классификация..."):
|
199 |
results = classify_text(title, description)
|
200 |
+
|
201 |
if results:
|
202 |
+
cumulative_prob = 0.0
|
203 |
+
st.subheader(f"📚 Топ категорий (до {int(t*100)}% совокупной вероятности):")
|
204 |
+
for label, score in results:
|
205 |
+
st.write(f"- **{label}**: {score:.4f}")
|
206 |
+
cumulative_prob += score
|
207 |
+
if cumulative_prob >= t:
|
208 |
+
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
209 |
else:
|
210 |
st.info("Не удалось получить результаты классификации.")
|
211 |
+
elif title or description:
|
212 |
+
st.warning("Нажмите кнопку 'Классифицировать', чтобы получить результат.")
|
|