Update app.py
Browse files
app.py
CHANGED
@@ -1,50 +1,13 @@
|
|
1 |
-
|
2 |
-
# !pip install gradio
|
3 |
|
4 |
-
|
5 |
-
import PyPDF2
|
6 |
-
from transformers import pipeline
|
7 |
-
from gtts import gTTS
|
8 |
-
import io
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
for page_num in range(pdf_reader.numPages):
|
16 |
-
text += pdf_reader.getPage(page_num).extractText()
|
17 |
-
return text
|
18 |
-
|
19 |
-
# Function to summarize text
|
20 |
-
def summarize_text(text):
|
21 |
-
summarizer = pipeline("summarization")
|
22 |
-
summary = summarizer(text, max_length=150, min_length=50, length_penalty=2.0, num_beams=4, early_stopping=True)
|
23 |
-
return summary[0]['summary_text']
|
24 |
-
|
25 |
-
# Function to convert text to speech
|
26 |
-
def text_to_speech(text, output_path='output.mp3'):
|
27 |
-
tts = gTTS(text, lang='en')
|
28 |
-
tts.save(output_path)
|
29 |
-
|
30 |
-
# Custom function to handle file uploads and processing
|
31 |
-
def process_file(input_file):
|
32 |
-
file_contents = input_file.read()
|
33 |
-
text = extract_text_from_pdf(file_contents)
|
34 |
-
summary = summarize_text(text)
|
35 |
-
text_to_speech(summary, output_path='output.mp3')
|
36 |
-
return {"text": summary, "audio": "output.mp3"}
|
37 |
-
|
38 |
-
# Gradio Interface
|
39 |
-
iface = gr.Interface(
|
40 |
-
fn=process_file,
|
41 |
-
inputs=gr.File(),
|
42 |
-
outputs=["text", "audio"],
|
43 |
-
live=True,
|
44 |
-
interpretation="default",
|
45 |
-
title="PDF Abstract Summarizer",
|
46 |
-
description="Summarize the abstract of a PDF file and generate audio.",
|
47 |
)
|
48 |
|
49 |
-
#
|
50 |
-
|
|
|
1 |
+
from gradio import Interface, Input, Output, Text
|
|
|
2 |
|
3 |
+
from utils import summarize_and_speak_pdf_abstract
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
# Define the Gradio interface
|
6 |
+
interface = Interface(
|
7 |
+
fn=summarize_and_speak_pdf_abstract,
|
8 |
+
inputs=[Input(type="file", label="Upload PDF")],
|
9 |
+
outputs=[Output(type="text", label="One-sentence Summary")],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
)
|
11 |
|
12 |
+
# Launch the interface
|
13 |
+
interface.launch(title="PDF Abstract Summarizer", description="Summarize the abstract of your PDF in one sentence.")
|