Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,112 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import re
|
| 3 |
-
import PyPDF2
|
| 4 |
-
import pandas as pd
|
| 5 |
-
from transformers import pipeline, AutoTokenizer
|
| 6 |
-
import gradio as gr
|
| 7 |
-
import spaces
|
| 8 |
-
|
| 9 |
-
# Function to clean text by keeping only alphanumeric characters and spaces
|
| 10 |
-
def clean_text(text):
|
| 11 |
-
return re.sub(r'[^a-zA-Z0-9\s]', '', text)
|
| 12 |
-
|
| 13 |
-
# Function to extract text from PDF files
|
| 14 |
-
def extract_text(pdf_file):
|
| 15 |
-
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
| 16 |
-
text = ''
|
| 17 |
-
for page_num in range(len(pdf_reader.pages)):
|
| 18 |
-
text += pdf_reader.pages[page_num].extract_text()
|
| 19 |
-
return text
|
| 20 |
-
|
| 21 |
-
# Function to split text into chunks of a specified size
|
| 22 |
-
def split_text(text, chunk_size=1024):
|
| 23 |
-
words = text.split()
|
| 24 |
-
for i in range(0, len(words), chunk_size):
|
| 25 |
-
yield ' '.join(words[i:i + chunk_size])
|
| 26 |
-
|
| 27 |
-
# Load the LED tokenizer
|
| 28 |
-
led_tokenizer = AutoTokenizer.from_pretrained("allenai/led-base-16384-multi_lexsum-source-long")
|
| 29 |
-
|
| 30 |
-
# Function to classify text using LED model
|
| 31 |
-
@spaces.GPU(duration=120)
|
| 32 |
-
def classify_text(text):
|
| 33 |
-
classifier = pipeline("text-classification", model="allenai/led-base-16384-multi_lexsum-source-long", tokenizer=led_tokenizer, framework="pt")
|
| 34 |
-
try:
|
| 35 |
-
return classifier(text)[0]['label']
|
| 36 |
-
except IndexError:
|
| 37 |
-
return "Unable to classify"
|
| 38 |
-
|
| 39 |
-
# Function to summarize text using BGE-m3 model
|
| 40 |
-
@spaces.GPU(duration=120)
|
| 41 |
-
def summarize_text(text, max_length=100, min_length=30):
|
| 42 |
-
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6", tokenizer="sshleifer/distilbart-cnn-12-6", framework="pt")
|
| 43 |
-
try:
|
| 44 |
-
return summarizer(text, max_length=max_length, min_length=min_length, do_sample=False)[0]['summary_text']
|
| 45 |
-
except IndexError:
|
| 46 |
-
return "Unable to summarize"
|
| 47 |
-
|
| 48 |
-
# Function to extract a title-like summary from the beginning of the text
|
| 49 |
-
@spaces.GPU(duration=120)
|
| 50 |
-
def extract_title(text, max_length=20):
|
| 51 |
-
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6", tokenizer="sshleifer/distilbart-cnn-12-6", framework="pt")
|
| 52 |
-
try:
|
| 53 |
-
return summarizer(text, max_length=max_length, min_length=5, do_sample=False)[0]['summary_text']
|
| 54 |
-
except IndexError:
|
| 55 |
-
return "Unable to extract title"
|
| 56 |
-
|
| 57 |
-
# Function to process PDF files and generate summaries
|
| 58 |
-
@spaces.GPU(duration=120)
|
| 59 |
-
def process_pdfs(pdf_files):
|
| 60 |
-
data = []
|
| 61 |
-
|
| 62 |
-
for pdf_file in pdf_files:
|
| 63 |
-
text = extract_text(pdf_file)
|
| 64 |
-
|
| 65 |
-
# Extract a title from the beginning of the text
|
| 66 |
-
title_text = ' '.join(text.split()[:512]) # Take the first 512 tokens for title extraction
|
| 67 |
-
title = extract_title(title_text)
|
| 68 |
-
|
| 69 |
-
# Initialize placeholders for combined results
|
| 70 |
-
combined_abstract = []
|
| 71 |
-
combined_cleaned_text = []
|
| 72 |
-
|
| 73 |
-
# Split text into chunks and process each chunk
|
| 74 |
-
for chunk in split_text(text, chunk_size=512):
|
| 75 |
-
# Summarize the text chunk
|
| 76 |
-
abstract = summarize_text(chunk)
|
| 77 |
-
combined_abstract.append(abstract)
|
| 78 |
-
|
| 79 |
-
# Clean the text chunk
|
| 80 |
-
cleaned_text = clean_text(chunk)
|
| 81 |
-
combined_cleaned_text.append(cleaned_text)
|
| 82 |
-
|
| 83 |
-
# Combine results from all chunks
|
| 84 |
-
final_abstract = ' '.join(combined_abstract)
|
| 85 |
-
final_cleaned_text = ' '.join(combined_cleaned_text)
|
| 86 |
-
|
| 87 |
-
# Append the data to the list
|
| 88 |
-
data.append([title, final_abstract, final_cleaned_text])
|
| 89 |
-
|
| 90 |
-
# Create a DataFrame from the data list
|
| 91 |
-
df = pd.DataFrame(data, columns=['Title', 'Abstract', 'Content'])
|
| 92 |
-
|
| 93 |
-
# Save the DataFrame to a CSV file in the same folder as the source folder
|
| 94 |
-
csv_file_path = 'processed_pdfs.csv'
|
| 95 |
-
df.to_csv(csv_file_path, index=False)
|
| 96 |
-
|
| 97 |
-
return csv_file_path
|
| 98 |
-
|
| 99 |
-
# Gradio interface
|
| 100 |
-
pdf_input = gr.File(label="Upload PDF Files", file_types=[".pdf"], file_count="multiple")
|
| 101 |
-
csv_output = gr.File(label="Download CSV")
|
| 102 |
-
|
| 103 |
-
gr.Interface(
|
| 104 |
-
fn=process_pdfs,
|
| 105 |
-
inputs=pdf_input,
|
| 106 |
-
outputs=csv_output,
|
| 107 |
-
title="Dataset creation",
|
| 108 |
-
description="Upload PDF files and get a summarized CSV file.",
|
| 109 |
-
article="""<p>This is an experimental app that allows you to create a dataset from research papers.</p>
|
| 110 |
-
<p>This app uses the allenai/led-base-16384-multi_lexsum-source-long and sshleifer/distilbart-cnn-12-6 AI models.</p>
|
| 111 |
-
<p>The output file is a CSV with 3 columns: title, abstract, and content.</p>"""
|
| 112 |
-
).launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|