Spaces:
Runtime error
Runtime error
Add application file
Browse files
app.py
CHANGED
@@ -1,12 +1,11 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import transformers
|
3 |
-
from transformers import pipeline
|
4 |
import PyPDF2
|
5 |
import pdfplumber
|
6 |
from pdfminer.high_level import extract_pages, extract_text
|
7 |
from pdfminer.layout import LTTextContainer, LTChar, LTRect, LTFigure
|
8 |
import re
|
9 |
import torch
|
|
|
|
|
10 |
from datasets import load_dataset
|
11 |
import soundfile as sf
|
12 |
from IPython.display import Audio
|
@@ -15,89 +14,60 @@ from datasets import load_dataset
|
|
15 |
import sentencepiece as spm
|
16 |
import os
|
17 |
import tempfile
|
|
|
18 |
|
19 |
|
20 |
-
|
21 |
def text_extraction(element):
|
22 |
-
# Extracting the text from the in-line text element
|
23 |
line_text = element.get_text()
|
24 |
|
25 |
-
# Find the formats of the text
|
26 |
-
# Initialize the list with all the formats that appeared in the line of text
|
27 |
line_formats = []
|
28 |
for text_line in element:
|
29 |
if isinstance(text_line, LTTextContainer):
|
30 |
-
# Iterating through each character in the line of text
|
31 |
for character in text_line:
|
32 |
if isinstance(character, LTChar):
|
33 |
-
# Append the font name of the character
|
34 |
line_formats.append(character.fontname)
|
35 |
-
# Append the font size of the character
|
36 |
line_formats.append(character.size)
|
37 |
-
# Find the unique font sizes and names in the line
|
38 |
format_per_line = list(set(line_formats))
|
39 |
|
40 |
-
# Return a tuple with the text in each line along with its format
|
41 |
return (line_text, format_per_line)
|
42 |
|
43 |
def read_pdf(pdf_pathy):
|
44 |
-
# create a PDF file object
|
45 |
pdfFileObj = open(pdf_pathy, 'rb')
|
46 |
-
# create a PDF reader object
|
47 |
pdfReaded = PyPDF2.PdfReader(pdfFileObj)
|
48 |
|
49 |
-
# Create the dictionary to extract text from each image
|
50 |
text_per_pagy = {}
|
51 |
-
# We extract the pages from the PDF
|
52 |
for pagenum, page in enumerate(extract_pages(pdf_pathy)):
|
53 |
print("Elaborating Page_" +str(pagenum))
|
54 |
-
# Initialize the variables needed for the text extraction from the page
|
55 |
pageObj = pdfReaded.pages[pagenum]
|
56 |
page_text = []
|
57 |
line_format = []
|
58 |
page_content = []
|
59 |
|
60 |
-
# Open the pdf file
|
61 |
pdf = pdfplumber.open(pdf_pathy)
|
62 |
|
63 |
-
|
64 |
-
# Find all the elements
|
65 |
page_elements = [(element.y1, element) for element in page._objs]
|
66 |
-
# Sort all the elements as they appear in the page
|
67 |
page_elements.sort(key=lambda a: a[0], reverse=True)
|
68 |
|
69 |
-
# Find the elements that composed a page
|
70 |
for i,component in enumerate(page_elements):
|
71 |
-
# Extract the position of the top side of the element in the PDF
|
72 |
pos= component[0]
|
73 |
-
# Extract the element of the page layout
|
74 |
element = component[1]
|
75 |
|
76 |
-
# Check if the element is a text element
|
77 |
if isinstance(element, LTTextContainer):
|
78 |
-
# Check if the text appeared in a table
|
79 |
-
# Use the function to extract the text and format for each text element
|
80 |
(line_text, format_per_line) = text_extraction(element)
|
81 |
-
# Append the text of each line to the page text
|
82 |
page_text.append(line_text)
|
83 |
-
# Append the format for each line containing text
|
84 |
line_format.append(format_per_line)
|
85 |
page_content.append(line_text)
|
86 |
|
87 |
|
88 |
-
# Create the key of the dictionary
|
89 |
dctkey = 'Page_'+str(pagenum)
|
90 |
-
# Add the list of list as the value of the page key
|
91 |
text_per_pagy[dctkey]= [page_text, line_format, page_content]
|
92 |
|
93 |
-
# Closing the pdf file object
|
94 |
pdfFileObj.close()
|
95 |
|
96 |
|
97 |
return text_per_pagy
|
98 |
|
99 |
-
#performing a cleaning of the contents
|
100 |
-
import re
|
101 |
|
102 |
def clean_text(text):
|
103 |
# remove extra spaces
|
@@ -109,20 +79,14 @@ def clean_text(text):
|
|
109 |
def extract_abstract(text_per_pagy):
|
110 |
abstract_text = ""
|
111 |
|
112 |
-
#iterate through each page in the extracted text dictionary
|
113 |
for page_num, page_text in text_per_pagy.items():
|
114 |
if page_text:
|
115 |
-
# Replace hyphens used for line breaks
|
116 |
page_text = page_text.replace("- ", "")
|
117 |
|
118 |
-
# Looking for the start of the abstract
|
119 |
start_index = page_text.find("Abstract")
|
120 |
if start_index != -1:
|
121 |
-
# Adjust the start index to exclude the word "Abstract" itself
|
122 |
-
# The length of "Abstract" is 8 characters; we also add 1 to skip the space after it
|
123 |
start_index += len("Abstract") + 1
|
124 |
|
125 |
-
# Searching the possible end markers of the abstract
|
126 |
end_markers = ["Introduction", "Summary", "Overview", "Background"]
|
127 |
end_index = -1
|
128 |
|
@@ -132,36 +96,33 @@ def extract_abstract(text_per_pagy):
|
|
132 |
end_index = temp_index
|
133 |
break
|
134 |
|
135 |
-
# If no end marker found, take entire text after "Abstract"
|
136 |
if end_index == -1:
|
137 |
end_index = len(page_text)
|
138 |
|
139 |
-
# Extract the abstract text
|
140 |
abstract = page_text[start_index:end_index].strip()
|
141 |
|
142 |
-
# Add the abstract to the complete text
|
143 |
abstract_text += " " + abstract
|
144 |
|
145 |
break
|
146 |
|
147 |
return abstract_text
|
148 |
|
149 |
-
|
150 |
def main_function(uploaded_filepath):
|
151 |
-
#a control to see if there is a file uploaded
|
152 |
if uploaded_filepath is None:
|
153 |
return "No file loaded", None
|
154 |
|
155 |
-
#read and process the file
|
156 |
text_per_pagy = read_pdf(uploaded_filepath)
|
157 |
|
158 |
-
#cleaning the text and getting the abstract
|
159 |
for key, value in text_per_pagy.items():
|
160 |
cleaned_text = clean_text(' '.join(value[0]))
|
161 |
text_per_pagy[key] = cleaned_text
|
162 |
abstract_text = extract_abstract(text_per_pagy)
|
163 |
|
164 |
-
#abstract summary
|
165 |
summarizer = pipeline("summarization", model="pszemraj/long-t5-tglobal-base-sci-simplify")
|
166 |
summary = summarizer(abstract_text, max_length=50, min_length=30, do_sample=False)[0]['summary_text']
|
167 |
|
@@ -171,20 +132,20 @@ def main_function(uploaded_filepath):
|
|
171 |
speaker_embedding = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
|
172 |
speech = synthesiser(summary, forward_params={"speaker_embeddings": speaker_embedding})
|
173 |
|
174 |
-
#saving the audio in a
|
175 |
audio_file_path = "summary.wav"
|
176 |
sf.write(audio_file_path, speech["audio"], samplerate=speech["sampling_rate"])
|
177 |
|
178 |
#the function returns the 2 pieces we need
|
179 |
return summary, audio_file_path
|
180 |
|
181 |
-
|
182 |
iface = gr.Interface(
|
183 |
fn=main_function,
|
184 |
-
inputs=gr.File(type="filepath"),
|
185 |
outputs=[gr.Textbox(label="Summary Text"), gr.Audio(label="Summary Audio", type="filepath")]
|
186 |
)
|
187 |
|
188 |
-
#
|
189 |
if __name__ == "__main__":
|
190 |
iface.launch()
|
|
|
|
|
|
|
|
|
1 |
import PyPDF2
|
2 |
import pdfplumber
|
3 |
from pdfminer.high_level import extract_pages, extract_text
|
4 |
from pdfminer.layout import LTTextContainer, LTChar, LTRect, LTFigure
|
5 |
import re
|
6 |
import torch
|
7 |
+
import transformers
|
8 |
+
from transformers import pipeline
|
9 |
from datasets import load_dataset
|
10 |
import soundfile as sf
|
11 |
from IPython.display import Audio
|
|
|
14 |
import sentencepiece as spm
|
15 |
import os
|
16 |
import tempfile
|
17 |
+
import gradio as gr
|
18 |
|
19 |
|
20 |
+
#reporting the created functions for the part 1
|
21 |
def text_extraction(element):
|
|
|
22 |
line_text = element.get_text()
|
23 |
|
|
|
|
|
24 |
line_formats = []
|
25 |
for text_line in element:
|
26 |
if isinstance(text_line, LTTextContainer):
|
|
|
27 |
for character in text_line:
|
28 |
if isinstance(character, LTChar):
|
|
|
29 |
line_formats.append(character.fontname)
|
|
|
30 |
line_formats.append(character.size)
|
|
|
31 |
format_per_line = list(set(line_formats))
|
32 |
|
|
|
33 |
return (line_text, format_per_line)
|
34 |
|
35 |
def read_pdf(pdf_pathy):
|
|
|
36 |
pdfFileObj = open(pdf_pathy, 'rb')
|
|
|
37 |
pdfReaded = PyPDF2.PdfReader(pdfFileObj)
|
38 |
|
|
|
39 |
text_per_pagy = {}
|
|
|
40 |
for pagenum, page in enumerate(extract_pages(pdf_pathy)):
|
41 |
print("Elaborating Page_" +str(pagenum))
|
|
|
42 |
pageObj = pdfReaded.pages[pagenum]
|
43 |
page_text = []
|
44 |
line_format = []
|
45 |
page_content = []
|
46 |
|
|
|
47 |
pdf = pdfplumber.open(pdf_pathy)
|
48 |
|
|
|
|
|
49 |
page_elements = [(element.y1, element) for element in page._objs]
|
|
|
50 |
page_elements.sort(key=lambda a: a[0], reverse=True)
|
51 |
|
|
|
52 |
for i,component in enumerate(page_elements):
|
|
|
53 |
pos= component[0]
|
|
|
54 |
element = component[1]
|
55 |
|
|
|
56 |
if isinstance(element, LTTextContainer):
|
|
|
|
|
57 |
(line_text, format_per_line) = text_extraction(element)
|
|
|
58 |
page_text.append(line_text)
|
|
|
59 |
line_format.append(format_per_line)
|
60 |
page_content.append(line_text)
|
61 |
|
62 |
|
|
|
63 |
dctkey = 'Page_'+str(pagenum)
|
|
|
64 |
text_per_pagy[dctkey]= [page_text, line_format, page_content]
|
65 |
|
|
|
66 |
pdfFileObj.close()
|
67 |
|
68 |
|
69 |
return text_per_pagy
|
70 |
|
|
|
|
|
71 |
|
72 |
def clean_text(text):
|
73 |
# remove extra spaces
|
|
|
79 |
def extract_abstract(text_per_pagy):
|
80 |
abstract_text = ""
|
81 |
|
|
|
82 |
for page_num, page_text in text_per_pagy.items():
|
83 |
if page_text:
|
|
|
84 |
page_text = page_text.replace("- ", "")
|
85 |
|
|
|
86 |
start_index = page_text.find("Abstract")
|
87 |
if start_index != -1:
|
|
|
|
|
88 |
start_index += len("Abstract") + 1
|
89 |
|
|
|
90 |
end_markers = ["Introduction", "Summary", "Overview", "Background"]
|
91 |
end_index = -1
|
92 |
|
|
|
96 |
end_index = temp_index
|
97 |
break
|
98 |
|
|
|
99 |
if end_index == -1:
|
100 |
end_index = len(page_text)
|
101 |
|
|
|
102 |
abstract = page_text[start_index:end_index].strip()
|
103 |
|
|
|
104 |
abstract_text += " " + abstract
|
105 |
|
106 |
break
|
107 |
|
108 |
return abstract_text
|
109 |
|
110 |
+
#let's define a main function that gets the uploaded file (pdf) to do the job
|
111 |
def main_function(uploaded_filepath):
|
112 |
+
#put a control to see if there is a file uploaded
|
113 |
if uploaded_filepath is None:
|
114 |
return "No file loaded", None
|
115 |
|
116 |
+
#read and process the file according to read_pdf
|
117 |
text_per_pagy = read_pdf(uploaded_filepath)
|
118 |
|
119 |
+
#cleaning the text and getting the abstract using the 2 other functions
|
120 |
for key, value in text_per_pagy.items():
|
121 |
cleaned_text = clean_text(' '.join(value[0]))
|
122 |
text_per_pagy[key] = cleaned_text
|
123 |
abstract_text = extract_abstract(text_per_pagy)
|
124 |
|
125 |
+
#abstract the summary with my pipeline and model, deciding the length
|
126 |
summarizer = pipeline("summarization", model="pszemraj/long-t5-tglobal-base-sci-simplify")
|
127 |
summary = summarizer(abstract_text, max_length=50, min_length=30, do_sample=False)[0]['summary_text']
|
128 |
|
|
|
132 |
speaker_embedding = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
|
133 |
speech = synthesiser(summary, forward_params={"speaker_embeddings": speaker_embedding})
|
134 |
|
135 |
+
#saving the audio in a temporary file
|
136 |
audio_file_path = "summary.wav"
|
137 |
sf.write(audio_file_path, speech["audio"], samplerate=speech["sampling_rate"])
|
138 |
|
139 |
#the function returns the 2 pieces we need
|
140 |
return summary, audio_file_path
|
141 |
|
142 |
+
#let's communicate with gradio what it has to put in
|
143 |
iface = gr.Interface(
|
144 |
fn=main_function,
|
145 |
+
inputs=gr.File(type="filepath"),
|
146 |
outputs=[gr.Textbox(label="Summary Text"), gr.Audio(label="Summary Audio", type="filepath")]
|
147 |
)
|
148 |
|
149 |
+
#launching the app
|
150 |
if __name__ == "__main__":
|
151 |
iface.launch()
|