Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
+
import fitz
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Load the model and tokenizer
|
| 8 |
+
model = AutoModelForSequenceClassification.from_pretrained("REEM-ALRASHIDI/LongFormer-Paper-Citaion-Classifier")
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained("allenai/longformer-base-4096")
|
| 10 |
+
|
| 11 |
+
def extract_text_from_pdf(file_path):
|
| 12 |
+
text = ''
|
| 13 |
+
with fitz.open(file_path) as pdf_document:
|
| 14 |
+
for page_number in range(pdf_document.page_count):
|
| 15 |
+
page = pdf_document.load_page(page_number)
|
| 16 |
+
text += page.get_text()
|
| 17 |
+
return text
|
| 18 |
+
|
| 19 |
+
def predict_class(text):
|
| 20 |
+
try:
|
| 21 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
| 22 |
+
with torch.no_grad():
|
| 23 |
+
outputs = model(**inputs)
|
| 24 |
+
logits = outputs.logits
|
| 25 |
+
predicted_class = torch.argmax(logits, dim=1).item()
|
| 26 |
+
return predicted_class
|
| 27 |
+
except Exception as e:
|
| 28 |
+
st.error(f"Error during prediction: {e}")
|
| 29 |
+
return None
|
| 30 |
+
|
| 31 |
+
st.title("Paper Citation Classifier")
|
| 32 |
+
|
| 33 |
+
# Input text boxes for abstract, full text, and affiliations
|
| 34 |
+
abstract_input = st.text_area("Enter Abstract:")
|
| 35 |
+
full_text_input = st.text_area("Enter Full Text:")
|
| 36 |
+
affiliations_input = st.text_area("Enter Affiliations:")
|
| 37 |
+
|
| 38 |
+
# PDF upload option
|
| 39 |
+
uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"])
|
| 40 |
+
if uploaded_file is not None:
|
| 41 |
+
file_text = extract_text_from_pdf(uploaded_file)
|
| 42 |
+
st.text("Extracted Text from PDF:")
|
| 43 |
+
st.text(file_text)
|
| 44 |
+
|
| 45 |
+
# Concatenate inputs with [SEP]
|
| 46 |
+
combined_text = f"{abstract_input} [SEP] {full_text_input} [SEP] {affiliations_input} [SEP] {file_text}"
|
| 47 |
+
|
| 48 |
+
if st.button("Predict"):
|
| 49 |
+
predicted_class = predict_class(combined_text)
|
| 50 |
+
if predicted_class is not None:
|
| 51 |
+
class_labels = ["Level 1", "Level 2", "Level 3", "Level 4"]
|
| 52 |
+
st.text(f"Predicted Class: {class_labels[predicted_class]}")
|