mrsk1883 commited on
Commit
1636cb6
1 Parent(s): cbaf28c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -46
app.py CHANGED
@@ -1,50 +1,13 @@
1
- # Install required libraries
2
- # !pip install gradio
3
 
4
- import gradio as gr
5
- import PyPDF2
6
- from transformers import pipeline
7
- from gtts import gTTS
8
- import io
9
 
10
- # Function to extract text from PDF using PyPDF2
11
- def extract_text_from_pdf(file_contents):
12
- with io.BytesIO(file_contents) as file:
13
- pdf_reader = PyPDF2.PdfFileReader(file)
14
- text = ''
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
- # Deploy the interface to Hugging Face Spaces
50
- iface.share(huggingface_spaces=True)
 
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.")