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