Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,14 +7,6 @@ import os
|
|
| 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
|
|
@@ -30,8 +22,6 @@ def predict_class(text):
|
|
| 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
|
|
@@ -63,71 +53,36 @@ with st.sidebar:
|
|
| 63 |
|
| 64 |
st.title("Check Your Paper Now!")
|
| 65 |
|
| 66 |
-
option = st.radio("Select input type:", ("Text", "PDF"))
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
options=["Nursing", "Physics", "Maths", "Chemical", "Nuclear", "Engineering" ,"Other"]
|
| 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 |
-
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)
|
|
|
|
| 7 |
model = AutoModelForSequenceClassification.from_pretrained("Reem333/Citaion-Classifier")
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained("allenai/longformer-base-4096")
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def predict_class(text):
|
| 11 |
try:
|
| 12 |
max_length = 4096
|
|
|
|
| 22 |
st.error(f"Error during prediction: {e}")
|
| 23 |
return None
|
| 24 |
|
|
|
|
|
|
|
| 25 |
|
| 26 |
class_colors = {
|
| 27 |
0: "#d62728", # Level 1
|
|
|
|
| 53 |
|
| 54 |
st.title("Check Your Paper Now!")
|
| 55 |
|
|
|
|
| 56 |
|
| 57 |
+
title_input = st.text_area("Enter Title:")
|
| 58 |
+
abstract_input = st.text_area("Enter Abstract:")
|
| 59 |
+
full_text_input = st.text_area("Enter Full Text:")
|
| 60 |
+
affiliations_input = st.text_area("Enter Affiliations:")
|
| 61 |
+
keywords_input = st.text_area("Enter Keywords:")
|
| 62 |
+
options=["Nursing", "Physics", "Maths", "Chemical", "Nuclear", "Engineering" ,"Other"]
|
|
|
|
| 63 |
|
| 64 |
+
selected_category = st.selectbox("Select WoS categories:", options, index= None)
|
| 65 |
+
if selected_category == "Other":
|
| 66 |
+
custom_category = st.text_input("Enter custom category:")
|
| 67 |
+
selected_category = custom_category if custom_category else "Other"
|
| 68 |
+
|
| 69 |
+
combined_text = f"{title_input} [SEP] {keywords_input} [SEP] {abstract_input} [SEP] {selected_category} [SEP] {affiliations_input} [SEP] {' [SEP] '.join(full_text_input)}"
|
| 70 |
+
|
| 71 |
+
if st.button("Predict"):
|
| 72 |
+
if not any([title_input, abstract_input,keywords_input, full_text_input, affiliations_input]):
|
| 73 |
+
st.warning("Please enter paper text.")
|
| 74 |
+
else:
|
| 75 |
+
with st.spinner("Predicting..."):
|
| 76 |
+
predicted_class = predict_class(combined_text)
|
| 77 |
+
if predicted_class is not None:
|
| 78 |
+
class_labels = ["Level 1", "Level 2", "Level 3", "Level 4"]
|
| 79 |
+
|
| 80 |
+
st.text("Predicted Class:")
|
| 81 |
+
for i, label in enumerate(class_labels):
|
| 82 |
+
if i == predicted_class:
|
| 83 |
+
st.markdown(
|
| 84 |
+
f'<div style="background-color: {class_colors[predicted_class]}; padding: 10px; border-radius: 5px; color: white; font-weight: bold;">{label}</div>',
|
| 85 |
+
unsafe_allow_html=True
|
| 86 |
+
)
|
| 87 |
+
else:
|
| 88 |
+
st.text(label)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|