Spaces:
Sleeping
Sleeping
File size: 2,778 Bytes
8b91654 31fe8a6 8b91654 3655658 8b91654 3655658 8b91654 3655658 31fe8a6 3655658 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
import streamlit as st
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import fitz
import os
# Load the model and tokenizer
model = AutoModelForSequenceClassification.from_pretrained("REEM-ALRASHIDI/LongFormer-Paper-Citaion-Classifier")
tokenizer = AutoTokenizer.from_pretrained("allenai/longformer-base-4096")
def extract_text_from_pdf(file_path):
text = ''
with fitz.open(file_path) as pdf_document:
for page_number in range(pdf_document.page_count):
page = pdf_document.load_page(page_number)
text += page.get_text()
return text
def predict_class(text):
try:
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
predicted_class = torch.argmax(logits, dim=1).item()
return predicted_class
except Exception as e:
st.error(f"Error during prediction: {e}")
return None
# Create a directory to store uploaded files
uploaded_files_dir = "uploaded_files"
os.makedirs(uploaded_files_dir, exist_ok=True)
st.title("Paper Citation Classifier")
option = st.radio("Select input type:", ("Text", "PDF"))
if option == "Text":
# Input text boxes for abstract, full text, and affiliations
abstract_input = st.text_area("Enter Abstract:")
full_text_input = st.text_area("Enter Full Text:")
affiliations_input = st.text_area("Enter Affiliations:")
# Concatenate inputs with [SEP]
combined_text = f"{abstract_input} [SEP] {full_text_input} [SEP] {affiliations_input}"
if st.button("Predict"):
predicted_class = predict_class(combined_text)
if predicted_class is not None:
class_labels = ["Level 1", "Level 2", "Level 3", "Level 4"]
st.text(f"Predicted Class: {class_labels[predicted_class]}")
elif option == "PDF":
uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"])
if uploaded_file is not None:
file_path = os.path.join(uploaded_files_dir, uploaded_file.name)
with open(file_path, "wb") as f:
f.write(uploaded_file.getbuffer())
st.success("File uploaded successfully.")
st.text(f"File Path: {file_path}")
file_text = extract_text_from_pdf(file_path)
st.text("Extracted Text:")
st.text(file_text)
# Provide an option to predict from PDF text
if st.button("Predict from PDF Text"):
predicted_class = predict_class(file_text)
if predicted_class is not None:
class_labels = ["Level 1", "Level 2", "Level 3", "Level 4"]
st.text(f"Predicted Class: {class_labels[predicted_class]}")
|