Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import libraries
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import PyPDF2
|
| 5 |
+
import torch
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
import scipy
|
| 8 |
+
import numpy
|
| 9 |
+
from gtts import gTTS
|
| 10 |
+
from io import BytesIO
|
| 11 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 12 |
+
|
| 13 |
+
# Function to extract text from PDF
|
| 14 |
+
# Defines a function to extract raw text from a PDF file
|
| 15 |
+
def extract_text(pdf_file):
|
| 16 |
+
pdfReader = PyPDF2.PdfReader(pdf_file)
|
| 17 |
+
pageObj = pdfReader.pages[0]
|
| 18 |
+
return pageObj.extract_text()
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# Function to summarize text
|
| 22 |
+
# Defines a function to summarize the extracted text using facebook/bart-large-cnn
|
| 23 |
+
def summarize_text(text):
|
| 24 |
+
sentences = text.split(". ")
|
| 25 |
+
for i, sentence in enumerate(sentences):
|
| 26 |
+
if "Abstract" in sentence:
|
| 27 |
+
start = i + 1
|
| 28 |
+
end = start + 6
|
| 29 |
+
break
|
| 30 |
+
abstract = ". ".join(sentences[start:end+1])
|
| 31 |
+
|
| 32 |
+
# Load BART model & tokenizer
|
| 33 |
+
tokenizer = AutoTokenizer.from_pretrained("pszemraj/led-base-book-summary")
|
| 34 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("pszemraj/led-base-book-summary")
|
| 35 |
+
|
| 36 |
+
# Tokenize abstract
|
| 37 |
+
inputs = tokenizer(abstract,
|
| 38 |
+
max_length=1024,
|
| 39 |
+
return_tensors="pt",
|
| 40 |
+
truncation=True)
|
| 41 |
+
|
| 42 |
+
# Generate summary
|
| 43 |
+
summary_ids = model.generate(inputs['input_ids'],
|
| 44 |
+
max_length=50,
|
| 45 |
+
min_length=30,
|
| 46 |
+
no_repeat_ngram_size=3,
|
| 47 |
+
encoder_no_repeat_ngram_size=3,
|
| 48 |
+
repetition_penalty=3.5,
|
| 49 |
+
num_beams=4,
|
| 50 |
+
do_sample=True,
|
| 51 |
+
early_stopping=False)
|
| 52 |
+
|
| 53 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 54 |
+
|
| 55 |
+
if '.' in summary:
|
| 56 |
+
index = summary.rindex('.')
|
| 57 |
+
if index != -1:
|
| 58 |
+
summary = summary[:index+1]
|
| 59 |
+
|
| 60 |
+
return summary
|
| 61 |
+
|
| 62 |
+
# Function to convert text to audio
|
| 63 |
+
# Defines a function to convert text to an audio file using Google Text-to-Speech
|
| 64 |
+
def text_to_audio(text):
|
| 65 |
+
tts = gTTS(text, lang='en')
|
| 66 |
+
buffer = BytesIO()
|
| 67 |
+
tts.write_to_fp(buffer)
|
| 68 |
+
buffer.seek(0)
|
| 69 |
+
return buffer.read()
|
| 70 |
+
|
| 71 |
+
### Main function
|
| 72 |
+
### The main function that ties everything together:
|
| 73 |
+
### extracts text, summarizes, and converts to audio.
|
| 74 |
+
def audio_pdf(pdf_file):
|
| 75 |
+
text = extract_text(pdf_file)
|
| 76 |
+
summary = summarize_text(text)
|
| 77 |
+
audio = text_to_audio(summary)
|
| 78 |
+
return summary, audio
|
| 79 |
+
|
| 80 |
+
# Define Gradio interface
|
| 81 |
+
# Gradio web interface with a file input, text output to display the summary
|
| 82 |
+
# and audio output to play the audio file. # Launches the interface
|
| 83 |
+
inputs = gr.File()
|
| 84 |
+
summary_text = gr.Text()
|
| 85 |
+
audio_summary = gr.Audio()
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
iface = gr.Interface(
|
| 89 |
+
fn=audio_pdf,
|
| 90 |
+
inputs=inputs,
|
| 91 |
+
outputs=[summary_text,audio_summary],
|
| 92 |
+
title="The Vocal PDF Summarizer",
|
| 93 |
+
description="I will summarize your pdf and transform it in to an audio",
|
| 94 |
+
examples=["Article 11 Hidden Technical Debt in Machine Learning Systems.pdf"
|
| 95 |
+
]
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
iface.launch() # Launch the interface
|