Spaces:
Sleeping
Sleeping
diogenemudenge
commited on
Commit
•
d98c3e0
1
Parent(s):
d536ccf
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pdfplumber
|
2 |
+
from transformers import pipeline
|
3 |
+
from gtts import gTTS
|
4 |
+
import speech_recognition as sr
|
5 |
+
import os
|
6 |
+
|
7 |
+
|
8 |
+
def extract_abstract(text):
|
9 |
+
# Simple logic to extract text between 'Abstract' and 'Introduction'
|
10 |
+
abstract_start = text.find('Abstract')
|
11 |
+
abstract_end = text.find('Introduction', abstract_start)
|
12 |
+
return text[abstract_start:abstract_end]
|
13 |
+
|
14 |
+
def summarize_abstract(abstract):
|
15 |
+
summarizer = pipeline('summarization', model='facebook/bart-large-cnn')
|
16 |
+
summary = summarizer(abstract, max_length=130, min_length=30, do_sample=False)
|
17 |
+
return summary[0]['summary_text']
|
18 |
+
|
19 |
+
def convert_to_speech(text):
|
20 |
+
tts = gTTS(text)
|
21 |
+
audio_file = 'summary.mp3'
|
22 |
+
tts.save(audio_file)
|
23 |
+
return audio_file
|
24 |
+
|
25 |
+
def my_app_function(pdf_file):
|
26 |
+
with pdfplumber.open(pdf_file) as pdf:
|
27 |
+
first_page = pdf.pages[0]
|
28 |
+
text = first_page.extract_text()
|
29 |
+
abstract = extract_abstract(text)
|
30 |
+
summary = summarize_abstract(abstract)
|
31 |
+
audio_file = convert_to_speech(summary)
|
32 |
+
return summary, audio_file
|
33 |
+
|
34 |
+
iface = gr.Interface(
|
35 |
+
fn=my_app_function,
|
36 |
+
inputs=gr.File(), # Updated line
|
37 |
+
outputs=["text", "audio"],
|
38 |
+
title="PDF Abstract Summarizer",
|
39 |
+
description="This app reads PDFs, summarizes the abstract, and converts the summary to speech. Please upload PDFs with abstracts."
|
40 |
+
)
|
41 |
+
|
42 |
+
|
43 |
+
iface.launch()
|