Spaces:
Sleeping
Sleeping
Antoniskaraolis
commited on
Commit
·
c306bb9
1
Parent(s):
299603c
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PyPDF2 import PdfReader
|
3 |
+
from transformers import pipeline
|
4 |
+
from gtts import gTTS
|
5 |
+
import tempfile
|
6 |
+
import os
|
7 |
+
|
8 |
+
def extract_abstract_from_pdf(file):
|
9 |
+
reader = PdfReader(file.name)
|
10 |
+
first_page = reader.pages[0]
|
11 |
+
text = first_page.extract_text()
|
12 |
+
return text
|
13 |
+
|
14 |
+
def summarize_text(text, model_name='sshleifer/distilbart-cnn-12-6'):
|
15 |
+
summarizer = pipeline("summarization", model=model_name)
|
16 |
+
summary = summarizer(text, max_length=75, min_length=20, do_sample=False)
|
17 |
+
first_sentence = summary[0]['summary_text'].split('.')[0] + '.'
|
18 |
+
return first_sentence
|
19 |
+
|
20 |
+
def process_pdf_to_speech(file):
|
21 |
+
abstract = extract_abstract_from_pdf(file)
|
22 |
+
summary = summarize_text(abstract)
|
23 |
+
tts = gTTS(text=summary, lang='en')
|
24 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as fp:
|
25 |
+
tts.save(fp.name)
|
26 |
+
return fp.name
|
27 |
+
|
28 |
+
iface = gr.Interface(
|
29 |
+
fn=process_pdf_to_speech,
|
30 |
+
inputs=gr.inputs.File(type="file", label="Upload PDF"),
|
31 |
+
outputs=gr.outputs.Audio(type="file", label="Play Summary"),
|
32 |
+
title="PDF Abstract to Speech",
|
33 |
+
description="This app summarizes the abstract from a PDF and converts it to speech. Please upload a PDF with an abstract."
|
34 |
+
)
|
35 |
+
|
36 |
+
iface.launch()
|