Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,134 @@
|
|
1 |
#!pip install gradio
|
2 |
-
import gradio as
|
3 |
-
def
|
4 |
-
|
5 |
-
|
6 |
-
#
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
)
|
17 |
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
#!pip install gradio
|
2 |
+
import gradio as gr
|
3 |
+
def read_pdf(pdf_path):
|
4 |
+
# create a PDF file object
|
5 |
+
pdfFileObj = open(pdf_path, 'rb')
|
6 |
+
# create a PDF reader object
|
7 |
+
pdfReader = PyPDF2.PdfReader(pdfFileObj)
|
8 |
+
|
9 |
+
# Create the dictionary to extract text from each page
|
10 |
+
text_per_page = {}
|
11 |
+
# We extract the pages from the PDF
|
12 |
+
for pagenum, page in enumerate(extract_pages(pdf_path)):
|
13 |
+
# Initialize the variables needed for the text extraction from the page
|
14 |
+
pageObj = pdfReader.pages[pagenum]
|
15 |
+
page_text = []
|
16 |
+
line_format = []
|
17 |
+
text_from_images = []
|
18 |
+
text_from_tables = []
|
19 |
+
page_content = []
|
20 |
+
# Initialize the number of the examined tables
|
21 |
+
table_num = 0
|
22 |
+
first_element= True
|
23 |
+
table_extraction_flag= False
|
24 |
+
# Open the pdf file
|
25 |
+
pdf = pdfplumber.open(pdf_path)
|
26 |
+
# Find the examined page
|
27 |
+
page_tables = pdf.pages[pagenum]
|
28 |
+
# Find the number of tables on the page
|
29 |
+
tables = page_tables.find_tables()
|
30 |
+
|
31 |
+
# Find all the elements
|
32 |
+
page_elements = [(element.y1, element) for element in page._objs]
|
33 |
+
# Sort all the elements as they appear in the page
|
34 |
+
page_elements.sort(key=lambda a: a[0], reverse=True)
|
35 |
+
|
36 |
+
# Find the elements that composed a page
|
37 |
+
for i, component in enumerate(page_elements):
|
38 |
+
# Extract the position of the top side of the element in the PDF
|
39 |
+
pos = component[0]
|
40 |
+
# Extract the element of the page layout
|
41 |
+
element = component[1]
|
42 |
+
|
43 |
+
# Check if the element is a text element
|
44 |
+
if isinstance(element, LTTextContainer):
|
45 |
+
# Check if the text appeared in a table
|
46 |
+
if table_extraction_flag == False:
|
47 |
+
# Use the function to extract the text and format for each text element
|
48 |
+
(line_text, format_per_line) = text_extraction(element)
|
49 |
+
# Append the text of each line to the page text
|
50 |
+
page_text.append(line_text)
|
51 |
+
# Append the format for each line containing text
|
52 |
+
line_format.append(format_per_line)
|
53 |
+
page_content.append(line_text)
|
54 |
+
else:
|
55 |
+
# Omit the text that appeared in a table
|
56 |
+
pass
|
57 |
+
|
58 |
+
# Create the key of the dictionary
|
59 |
+
dctkey = 'Page_'+str(pagenum)
|
60 |
+
# Add the list of list as the value of the page key
|
61 |
+
text_per_page[dctkey] = [page_text, line_format, text_from_images, text_from_tables, page_content]
|
62 |
+
|
63 |
+
# Closing the pdf file object
|
64 |
+
pdfFileObj.close()
|
65 |
+
return text_per_page
|
66 |
+
pdf_path = '/content/Article 11 Hidden Technical Debt in Machine Learning Systems.pdf'
|
67 |
+
|
68 |
+
text_per_page = read_pdf(pdf_path)
|
69 |
+
|
70 |
+
Page_0 = text_per_page['Page_0']
|
71 |
+
|
72 |
+
def nested_list_to_string(nested_list):
|
73 |
+
result = ''
|
74 |
+
for element in nested_list:
|
75 |
+
if isinstance(element, list): # Check if the element is a list
|
76 |
+
result += nested_list_to_string(element) # Recursively process the list
|
77 |
+
elif isinstance(element, str): # Check if the element is a string
|
78 |
+
result += element # Append the string to the result
|
79 |
+
return result
|
80 |
+
|
81 |
+
Page_0 = text_per_page['Page_0']
|
82 |
+
string_result = nested_list_to_string(Page_0)
|
83 |
|
84 |
+
def extract_abstract(page_0):
|
85 |
+
def nested_list_to_string(nested_list):
|
86 |
+
result = ''
|
87 |
+
for element in nested_list:
|
88 |
+
if isinstance(element, list): # Check if the element is a list
|
89 |
+
result += nested_list_to_string(element) # Recursively process the list
|
90 |
+
elif isinstance(element, str): # Check if the element is a string
|
91 |
+
result += element # Append the string to the result
|
92 |
+
return result
|
93 |
+
|
94 |
+
# Convert the nested list into a single string
|
95 |
+
full_text = nested_list_to_string(page_0)
|
96 |
+
|
97 |
+
# Find the start of the 'Abstract' section and the end of it (start of 'Introduction')
|
98 |
+
start_index = full_text.find('Abstract')
|
99 |
+
end_index = full_text.find('Introduction')
|
100 |
+
|
101 |
+
# If both 'Abstract' and 'Introduction' are found, extract the text in between
|
102 |
+
if start_index != -1 and end_index != -1:
|
103 |
+
# Extract the text and remove the word 'Abstract'
|
104 |
+
abstract_text = full_text[start_index + len('Abstract'):end_index]
|
105 |
+
return abstract_text.strip()
|
106 |
+
else:
|
107 |
+
return "Abstract or Introduction section not found."
|
108 |
+
|
109 |
+
# Example usage
|
110 |
+
Page_0 = text_per_page['Page_0']
|
111 |
+
abstract_text = extract_abstract(Page_0)
|
112 |
+
|
113 |
+
wall_of_text = abstract_text
|
114 |
+
|
115 |
+
result = summarizer(
|
116 |
+
wall_of_text,
|
117 |
+
min_length=1,
|
118 |
+
max_length=30,
|
119 |
+
no_repeat_ngram_size=3,
|
120 |
+
encoder_no_repeat_ngram_size=3,
|
121 |
+
repetition_penalty=3.5,
|
122 |
+
num_beams=4,
|
123 |
+
early_stopping=True,
|
124 |
)
|
125 |
|
126 |
+
# Access the first element of the list (which is the dictionary) and then the value of 'summary_text'
|
127 |
+
summary_string = result[0]['summary_text']
|
128 |
+
|
129 |
+
print(summary_string)
|
130 |
+
|
131 |
+
)
|
132 |
+
|
133 |
+
app = gra.Interface(fn = user_greeting, inputs=summary_string, outputs=summary_string)
|
134 |
+
app.launch()
|