voroninip commited on
Commit
6cfce6c
·
verified ·
1 Parent(s): e9d357a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -0
app.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+
4
+
5
+ @st.cache_resource # кэширование
6
+ def load_model():
7
+ return pipeline("text-classification", model="Wi/arxiv-distilbert-base-cased") # скачивание модели
8
+
9
+ model = load_model()
10
+
11
+ def top_pct(preds, threshold=.95):
12
+ preds = sorted(preds, key=lambda x: -x["score"])
13
+
14
+ cum_score = 0
15
+ for i, item in enumerate(preds):
16
+ cum_score += item["score"]
17
+ if cum_score >= threshold:
18
+ break
19
+
20
+ preds = preds[:(i+1)]
21
+
22
+ return preds
23
+
24
+
25
+ def format_predictions(preds) -> str:
26
+ """
27
+ Prepare predictions and their scores for printing to the user
28
+ """
29
+ out = ""
30
+ for i, item in enumerate(preds):
31
+ out += f"{i+1}. {item['label']} (score {item['score']:.2f})\n"
32
+ return out
33
+
34
+
35
+ st.markdown("""
36
+ <div style='text-align: center;'>
37
+ <img src='https://info.arxiv.org/brand/images/brand-logo-primary.jpg' alt='Centered Image' width='300'/>
38
+ </div>
39
+ """, unsafe_allow_html=True)
40
+
41
+ st.markdown("""
42
+ <h2 style='text-align: center; color: #e80ad8; font-family: Arial;'>
43
+ 🚀 arXiv paper categories predictor
44
+ </h2>
45
+ """, unsafe_allow_html=True)
46
+
47
+
48
+ # CSS to change the background of the entire app
49
+ background_color_css = """
50
+ <style>
51
+ .stApp {
52
+ background-color: black; /* #eefcfa */
53
+ }
54
+ </style>
55
+ """
56
+
57
+ st.markdown("""
58
+ <br><br> <!-- Adds vertical space -->
59
+ <p style='
60
+ color: white;
61
+ font-size: 20px;
62
+ font-family: "Courier New", monospace;
63
+ '>
64
+ Paste Title and Abstract of the paper and get most likely categories of the paper in the
65
+ <a href="https://arxiv.org/category_taxonomy" target="_blank" style="color: cyan; text-decoration: none;">
66
+ arXiv taxonomy
67
+ </a>
68
+ </p>
69
+ """, unsafe_allow_html=True)
70
+
71
+ title = st.text_input("Title", value="")
72
+
73
+ abstract = st.text_input("Abstract", value="")
74
+
75
+ query = title + '\n' + abstract
76
+ if query:
77
+ st.markdown("""
78
+ <br><br> <!-- Adds vertical space -->
79
+ <p style='
80
+ color: white;
81
+ font-size: 20px;
82
+ font-family: "Courier New", monospace;
83
+ '>
84
+ Most likely categories of the paper:
85
+ </p>
86
+ """, unsafe_allow_html=True)
87
+ result = format_predictions(top_pct(model(query)[0]))
88
+
89
+ st.write(result)