|
|
|
|
|
import gradio as gr |
|
from transformers import pipeline |
|
from tempfile import NamedTemporaryFile |
|
import PyPDF2 |
|
from PyPDF2 import PdfReader |
|
from pdfminer.high_level import extract_pages, extract_text |
|
import pdfplumber |
|
from PIL import Image |
|
from pdf2image import convert_from_path |
|
from pdfminer.layout import LTTextContainer, LTChar, LTRect, LTFigure |
|
import pytesseract |
|
import os |
|
import numpy as np |
|
import torch |
|
import sentencepiece |
|
import soundfile as sf |
|
from IPython.display import Audio |
|
from datasets import load_dataset |
|
from transformers import SpeechT5HifiGan |
|
|
|
def read_pdf(pdf_path): |
|
|
|
pdfFileObj = open(pdf_path, 'rb') |
|
|
|
pdfReaded = PyPDF2.PdfReader(pdfFileObj) |
|
|
|
|
|
text_per_page = {} |
|
|
|
for pagenum, page in enumerate(extract_pages(pdf_path)): |
|
print("Elaborating Page_" +str(pagenum)) |
|
|
|
pageObj = pdfReaded.pages[pagenum] |
|
page_text = [] |
|
line_format = [] |
|
text_from_images = [] |
|
text_from_tables = [] |
|
page_content = [] |
|
|
|
table_num = 0 |
|
first_element= True |
|
table_extraction_flag= False |
|
|
|
pdf = pdfplumber.open(pdf_path) |
|
|
|
page_tables = pdf.pages[pagenum] |
|
|
|
tables = page_tables.find_tables() |
|
|
|
|
|
|
|
page_elements = [(element.y1, element) for element in page._objs] |
|
|
|
page_elements.sort(key=lambda a: a[0], reverse=True) |
|
|
|
|
|
for i,component in enumerate(page_elements): |
|
|
|
pos= component[0] |
|
|
|
element = component[1] |
|
|
|
|
|
if isinstance(element, LTTextContainer): |
|
|
|
if table_extraction_flag == False: |
|
|
|
(line_text, format_per_line) = text_extraction(element) |
|
|
|
page_text.append(line_text) |
|
|
|
line_format.append(format_per_line) |
|
page_content.append(line_text) |
|
else: |
|
|
|
pass |
|
|
|
|
|
if isinstance(element, LTFigure): |
|
|
|
crop_image(element, pageObj) |
|
|
|
convert_to_images('cropped_image.pdf') |
|
|
|
image_text = image_to_text('PDF_image.png') |
|
text_from_images.append(image_text) |
|
page_content.append(image_text) |
|
|
|
page_text.append('image') |
|
line_format.append('image') |
|
|
|
|
|
if isinstance(element, LTRect): |
|
|
|
if first_element == True and (table_num+1) <= len(tables): |
|
|
|
lower_side = page.bbox[3] - tables[table_num].bbox[3] |
|
upper_side = element.y1 |
|
|
|
table = extract_table(pdf_path, pagenum, table_num) |
|
|
|
table_string = table_converter(table) |
|
|
|
text_from_tables.append(table_string) |
|
page_content.append(table_string) |
|
|
|
table_extraction_flag = True |
|
|
|
first_element = False |
|
|
|
page_text.append('table') |
|
line_format.append('table') |
|
|
|
|
|
if element.y0 >= lower_side and element.y1 <= upper_side: |
|
pass |
|
elif not isinstance(page_elements[i+1][1], LTRect): |
|
table_extraction_flag = False |
|
first_element = True |
|
table_num+=1 |
|
|
|
|
|
|
|
dctkey = 'Page_'+str(pagenum) |
|
|
|
text_per_page[dctkey]= [page_text, line_format, text_from_images,text_from_tables, page_content] |
|
|
|
|
|
pdfFileObj.close() |
|
|
|
return text_per_page |
|
|
|
pdf_path = pdf_file.name |
|
|
|
text_per_page = read_pdf(pdf_path) |
|
|
|
page_0 = text_per_page['Page_0'] |
|
|
|
page_0_clean = [item for sublist in page_0 for item in sublist if isinstance(item, str)] |
|
for i in range(len(page_0_clean)): |
|
page_0_clean[i] = page_0_clean[i].replace('\n', ' ').strip() |
|
|
|
|
|
|
|
abstract = 'abstract' |
|
found_abstract = False |
|
intro_string ='introduction' |
|
extracted_abstract ="" |
|
extracted_abstract = extracted_text_string.replace("Abstract", "") |
|
|
|
file = text.splitlines() |
|
for lines in file: |
|
lower_lines = lines.lower() |
|
if lower_lines.strip()== abstract: |
|
found_abstract = True |
|
elif "1" in lower_lines.strip() and intro_string in lower_lines.strip(): |
|
found_abstract = False |
|
|
|
|
|
|
|
from transformers import pipeline |
|
summarizer = pipeline("summarization", model="Falconsai/text_summarization") |
|
text1 = extracted_abstract |
|
print(summarizer(text1, max_length=20, min_length=10, do_sample=False)) |
|
|
|
|
|
|
|
sentence = summarized_text[0]['summary_text'] |
|
|
|
|
|
|
|
|
|
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech |
|
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts") |
|
model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts") |
|
|
|
|
|
text = sentence |
|
inputs = processor(text=sentence, return_tensors="pt") |
|
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation") |
|
speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0) |
|
spectrogram = model.generate_speech(inputs["input_ids"], speaker_embeddings) |
|
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan") |
|
with torch.no_grad(): |
|
speech = vocoder(spectrogram) |
|
|
|
speech = model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder) |
|
Audio(speech, rate=16000) |
|
|
|
|
|
|
|
input_component = gr.File(file_types=["pdf"]) |
|
output_component = gr.Audio() |
|
|
|
demo = gr.Interface( |
|
fn=read_pdf, |
|
inputs=input_component, |
|
outputs=output_component, |
|
title="Reading your abstract summary outloud", |
|
description="Upload a PDF that contains an Abstract. Get your abstract summarized in 1 sentence and read outloud. We only accept with PDfs that contains the section Abstract" |
|
) |
|
|
|
demo.launch() |