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