Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -18,7 +18,6 @@ def extract_text_from_pdf(file_path):
|
|
18 |
|
19 |
def predict_class(text):
|
20 |
try:
|
21 |
-
|
22 |
max_length = 4096
|
23 |
truncated_text = text[:max_length]
|
24 |
|
@@ -32,11 +31,9 @@ def predict_class(text):
|
|
32 |
st.error(f"Error during prediction: {e}")
|
33 |
return None
|
34 |
|
35 |
-
|
36 |
uploaded_files_dir = "uploaded_files"
|
37 |
os.makedirs(uploaded_files_dir, exist_ok=True)
|
38 |
|
39 |
-
|
40 |
class_colors = {
|
41 |
0: "#d62728", # Level 1
|
42 |
1: "#ff7f0e", # Level 2
|
@@ -50,8 +47,6 @@ st.image("logo2.png", width=70)
|
|
50 |
|
51 |
st.markdown('<div style="position: absolute; top: 5px; left: 5px;"></div>', unsafe_allow_html=True)
|
52 |
|
53 |
-
# col1, col2 = st.columns([1, 3])
|
54 |
-
|
55 |
st.title("Paper Citation Classifier")
|
56 |
|
57 |
option = st.radio("Select input type:", ("Text", "PDF"))
|
@@ -65,25 +60,26 @@ if option == "Text":
|
|
65 |
|
66 |
categories = pills("Select WoS category", options)
|
67 |
|
68 |
-
# categories = st.multiselect("Select WoS categories:", options)
|
69 |
-
|
70 |
combined_text = f"{title_input} [SEP] {abstract_input} [SEP] {full_text_input} [SEP] {affiliations_input} [SEP] {' [SEP] '.join(categories)}"
|
71 |
|
72 |
if st.button("Predict"):
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
87 |
|
88 |
elif option == "PDF":
|
89 |
uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"])
|
@@ -101,20 +97,19 @@ elif option == "PDF":
|
|
101 |
st.text(file_text)
|
102 |
|
103 |
if st.button("Predict from PDF Text"):
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
|
|
18 |
|
19 |
def predict_class(text):
|
20 |
try:
|
|
|
21 |
max_length = 4096
|
22 |
truncated_text = text[:max_length]
|
23 |
|
|
|
31 |
st.error(f"Error during prediction: {e}")
|
32 |
return None
|
33 |
|
|
|
34 |
uploaded_files_dir = "uploaded_files"
|
35 |
os.makedirs(uploaded_files_dir, exist_ok=True)
|
36 |
|
|
|
37 |
class_colors = {
|
38 |
0: "#d62728", # Level 1
|
39 |
1: "#ff7f0e", # Level 2
|
|
|
47 |
|
48 |
st.markdown('<div style="position: absolute; top: 5px; left: 5px;"></div>', unsafe_allow_html=True)
|
49 |
|
|
|
|
|
50 |
st.title("Paper Citation Classifier")
|
51 |
|
52 |
option = st.radio("Select input type:", ("Text", "PDF"))
|
|
|
60 |
|
61 |
categories = pills("Select WoS category", options)
|
62 |
|
|
|
|
|
63 |
combined_text = f"{title_input} [SEP] {abstract_input} [SEP] {full_text_input} [SEP] {affiliations_input} [SEP] {' [SEP] '.join(categories)}"
|
64 |
|
65 |
if st.button("Predict"):
|
66 |
+
if not combined_text.strip(): # Check if combined_text is empty or contains only whitespace
|
67 |
+
st.warning("Please enter paper text.")
|
68 |
+
else:
|
69 |
+
with st.spinner("Predicting..."):
|
70 |
+
predicted_class = predict_class(combined_text)
|
71 |
+
if predicted_class is not None:
|
72 |
+
class_labels = ["Level 1 (Highly Cited Paper)", "Level 2 (Average Cited Paper)", "Level 3 (More Cited Paper)", "Level 4 (Low Cited Paper)"]
|
73 |
+
|
74 |
+
st.text("Predicted Class:")
|
75 |
+
for i, label in enumerate(class_labels):
|
76 |
+
if i == predicted_class:
|
77 |
+
st.markdown(
|
78 |
+
f'<div style="background-color: {class_colors[predicted_class]}; padding: 10px; border-radius: 5px; color: white; font-weight: bold;">{label}</div>',
|
79 |
+
unsafe_allow_html=True
|
80 |
+
)
|
81 |
+
else:
|
82 |
+
st.text(label)
|
83 |
|
84 |
elif option == "PDF":
|
85 |
uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"])
|
|
|
97 |
st.text(file_text)
|
98 |
|
99 |
if st.button("Predict from PDF Text"):
|
100 |
+
if not file_text.strip(): # Check if file_text is empty or contains only whitespace
|
101 |
+
st.warning("Please upload a PDF file with text content.")
|
102 |
+
else:
|
103 |
+
with st.spinner("Predicting..."):
|
104 |
+
predicted_class = predict_class(file_text)
|
105 |
+
if predicted_class is not None:
|
106 |
+
class_labels = ["Level 1 (Highly Cited Paper)", "Level 2 (Average Cited Paper)", "Level 3 (More Cited Paper)", "Level 4 (Low Cited Paper)"]
|
107 |
+
st.text("Predicted Class:")
|
108 |
+
for i, label in enumerate(class_labels):
|
109 |
+
if i == predicted_class:
|
110 |
+
st.markdown(
|
111 |
+
f'<div style="background-color: {class_colors[predicted_class]}; padding: 10px; border-radius: 5px; color: white; font-weight: bold;">{label}</div>',
|
112 |
+
unsafe_allow_html=True
|
113 |
+
)
|
114 |
+
else:
|
115 |
+
st.text(label)
|
|