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