|
|
|
|
|
import gradio as gr |
|
import torch |
|
import PyPDF2 |
|
from transformers import pipeline |
|
import numpy |
|
import scipy |
|
from gtts import gTTS |
|
from io import BytesIO |
|
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM |
|
from bark import SAMPLE_RATE, generate_audio, preload_models |
|
|
|
|
|
|
|
|
|
def extract_text(pdf_file): |
|
pdfReader = PyPDF2.PdfReader(pdf_file) |
|
pageObj = pdfReader.pages[0] |
|
return pageObj.extract_text() |
|
|
|
|
|
|
|
|
|
def summarize_text(text): |
|
sentences = text.split(". ") |
|
for i, sentence in enumerate(sentences): |
|
if "Abstract" in sentence: |
|
start = i + 1 |
|
end = start + 6 |
|
break |
|
|
|
if start is not None and end is not None: |
|
abstract = ". ".join(sentences[start:end+1]) |
|
|
|
else: |
|
return("Abstract section not found") |
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained("pszemraj/led-base-book-summary") |
|
model = AutoModelForSeq2SeqLM.from_pretrained("pszemraj/led-base-book-summary") |
|
|
|
|
|
inputs = tokenizer(abstract, |
|
max_length=1024, |
|
return_tensors="pt", |
|
truncation=True) |
|
|
|
|
|
summary_ids = model.generate(inputs['input_ids'], |
|
max_length=50, |
|
min_length=30, |
|
no_repeat_ngram_size=3, |
|
encoder_no_repeat_ngram_size=3, |
|
repetition_penalty=3.5, |
|
num_beams=4, |
|
do_sample=True, |
|
early_stopping=False) |
|
|
|
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True) |
|
|
|
if '.' in summary: |
|
index = summary.rindex('.') |
|
if index != -1: |
|
summary = summary[:index+1] |
|
|
|
return summary |
|
|
|
|
|
|
|
|
|
def text_to_audio(text): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
speech_array = generate_audio(text) |
|
return (SAMPLE_RATE, speech_array) |
|
|
|
|
|
|
|
|
|
|
|
|
|
def audio_pdf(pdf_file): |
|
text = extract_text(pdf_file) |
|
summary = summarize_text(text) |
|
audio = text_to_audio(summary) |
|
return summary, audio |
|
|
|
|
|
|
|
|
|
inputs = gr.File() |
|
summary_text = gr.Text() |
|
audio_summary = gr.Audio() |
|
|
|
|
|
iface = gr.Interface( |
|
fn=audio_pdf, |
|
inputs=inputs, |
|
outputs=[summary_text,audio_summary], |
|
title="PDF Audio Summarizer 📻", |
|
description="App that converts an abstract into audio", |
|
examples=["Attention_is_all_you_need.pdf", |
|
"ImageNet_Classification.pdf" |
|
] |
|
) |
|
|
|
iface.launch() |