Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
4 |
+
import fitz
|
5 |
+
import os
|
6 |
+
|
7 |
+
model = AutoModelForSequenceClassification.from_pretrained("Reem333/Citaion-Classifier")
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained("allenai/longformer-base-4096")
|
9 |
+
|
10 |
+
def extract_text_from_pdf(file_path):
|
11 |
+
text = ''
|
12 |
+
with fitz.open(file_path) as pdf_document:
|
13 |
+
for page_number in range(pdf_document.page_count):
|
14 |
+
page = pdf_document.load_page(page_number)
|
15 |
+
text += page.get_text()
|
16 |
+
return text
|
17 |
+
|
18 |
+
def predict_class(text):
|
19 |
+
try:
|
20 |
+
max_length = 4096
|
21 |
+
truncated_text = text[:max_length]
|
22 |
+
|
23 |
+
inputs = tokenizer(truncated_text, return_tensors="pt", padding=True, truncation=True, max_length=max_length)
|
24 |
+
with torch.no_grad():
|
25 |
+
outputs = model(**inputs)
|
26 |
+
logits = outputs.logits
|
27 |
+
predicted_class = torch.argmax(logits, dim=1).item()
|
28 |
+
return predicted_class
|
29 |
+
except Exception as e:
|
30 |
+
st.error(f"Error during prediction: {e}")
|
31 |
+
return None
|
32 |
+
|
33 |
+
uploaded_files_dir = "uploaded_files"
|
34 |
+
os.makedirs(uploaded_files_dir, exist_ok=True)
|
35 |
+
|
36 |
+
class_colors = {
|
37 |
+
0: "#d62728", # Level 1
|
38 |
+
1: "#ff7f0e", # Level 2
|
39 |
+
2: "#2ca02c", # Level 3
|
40 |
+
3: "#1f77b4" # Level 4
|
41 |
+
}
|
42 |
+
|
43 |
+
st.set_page_config(page_title="Paper Citation Classifier", page_icon="logo.png")
|
44 |
+
|
45 |
+
with st.sidebar:
|
46 |
+
st.image("logo.png", width=70)
|
47 |
+
st.markdown('<div style="position: absolute; left: 5px;"></div>', unsafe_allow_html=True)
|
48 |
+
|
49 |
+
st.markdown("# Paper Citation Classifier")
|
50 |
+
st.markdown("---")
|
51 |
+
st.markdown("## About")
|
52 |
+
st.markdown('''
|
53 |
+
This is a tool to classify paper citations into different levels based on their number of citations.
|
54 |
+
Powered by Fine-Tuned [Longformer model](https://huggingface.co/REEM-ALRASHIDI/LongFormer-Paper-Citaion-Classifier) with custom data.
|
55 |
+
''')
|
56 |
+
st.markdown("### Class Levels:")
|
57 |
+
st.markdown("- Level 1: Highly cited papers")
|
58 |
+
st.markdown("- Level 2: Average cited papers")
|
59 |
+
st.markdown("- Level 3: More cited papers")
|
60 |
+
st.markdown("- Level 4: Low cited papers")
|
61 |
+
st.markdown("---")
|
62 |
+
st.markdown('Tabuk University')
|
63 |
+
|
64 |
+
st.title("Check Your Paper Now!")
|
65 |
+
|
66 |
+
option = st.radio("Select input type:", ("Text", "PDF"))
|
67 |
+
|
68 |
+
if option == "Text":
|
69 |
+
title_input = st.text_area("Enter Title:")
|
70 |
+
abstract_input = st.text_area("Enter Abstract:")
|
71 |
+
full_text_input = st.text_area("Enter Full Text:")
|
72 |
+
affiliations_input = st.text_area("Enter Affiliations:")
|
73 |
+
keywords_input = st.text_area("Enter Keywords:")
|
74 |
+
options=["Nursing", "Physics", "Maths", "Chemical", "Nuclear", "Engineering" ,"Other"]
|
75 |
+
|
76 |
+
selected_category = st.selectbox("Select WoS categories:", options, index= None)
|
77 |
+
if selected_category == "Other":
|
78 |
+
custom_category = st.text_input("Enter custom category:")
|
79 |
+
selected_category = custom_category if custom_category else "Other"
|
80 |
+
|
81 |
+
combined_text = f"{title_input} [SEP] {keywords_input} [SEP] {abstract_input} [SEP] {selected_category} [SEP] {affiliations_input} [SEP] {' [SEP] '.join(full_text_input)}"
|
82 |
+
|
83 |
+
if st.button("Predict"):
|
84 |
+
if not any([title_input, abstract_input,keywords_input, full_text_input, affiliations_input]):
|
85 |
+
st.warning("Please enter paper text.")
|
86 |
+
else:
|
87 |
+
with st.spinner("Predicting..."):
|
88 |
+
predicted_class = predict_class(combined_text)
|
89 |
+
if predicted_class is not None:
|
90 |
+
class_labels = ["Level 1", "Level 2", "Level 3", "Level 4"]
|
91 |
+
|
92 |
+
st.text("Predicted Class:")
|
93 |
+
for i, label in enumerate(class_labels):
|
94 |
+
if i == predicted_class:
|
95 |
+
st.markdown(
|
96 |
+
f'<div style="background-color: {class_colors[predicted_class]}; padding: 10px; border-radius: 5px; color: white; font-weight: bold;">{label}</div>',
|
97 |
+
unsafe_allow_html=True
|
98 |
+
)
|
99 |
+
else:
|
100 |
+
st.text(label)
|
101 |
+
|
102 |
+
elif option == "PDF":
|
103 |
+
uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"])
|
104 |
+
|
105 |
+
if uploaded_file is not None:
|
106 |
+
with st.spinner("Processing PDF..."):
|
107 |
+
file_path = os.path.join(uploaded_files_dir, uploaded_file.name)
|
108 |
+
with open(file_path, "wb") as f:
|
109 |
+
f.write(uploaded_file.getbuffer())
|
110 |
+
st.success("File uploaded successfully.")
|
111 |
+
st.text(f"File Path: {file_path}")
|
112 |
+
|
113 |
+
file_text = extract_text_from_pdf(file_path)
|
114 |
+
st.text("Extracted Text:")
|
115 |
+
st.text(file_text)
|
116 |
+
|
117 |
+
if st.button("Predict from PDF Text"):
|
118 |
+
if not file_text.strip():
|
119 |
+
st.warning("Please upload a PDF with text content.")
|
120 |
+
else:
|
121 |
+
with st.spinner("Predicting..."):
|
122 |
+
predicted_class = predict_class(file_text)
|
123 |
+
if predicted_class is not None:
|
124 |
+
class_labels = ["Level 1", "Level 2", "Level 3", "Level 4"]
|
125 |
+
st.text("**Predicted Class:**")
|
126 |
+
for i, label in enumerate(class_labels):
|
127 |
+
if i == predicted_class:
|
128 |
+
st.markdown(
|
129 |
+
f'<div style="background-color: {class_colors[predicted_class]}; padding: 10px; border-radius: 5px; color: white; font-weight: bold;">{label}</div>',
|
130 |
+
unsafe_allow_html=True
|
131 |
+
)
|
132 |
+
else:
|
133 |
+
st.text(label)
|