rahulpitt commited on
Commit
402e1e6
·
verified ·
1 Parent(s): b9c93eb

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +56 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ from PyPDF2 import PdfReader
4
+ import os
5
+
6
+ # Load a summarization model
7
+ summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
8
+
9
+ # Function to summarize text
10
+ def summarize_text(text, max_length, min_length):
11
+ summary = summarizer(text, max_length=int(max_length), min_length=int(min_length), do_sample=False)
12
+ return summary[0]['summary_text']
13
+
14
+ # Function to summarize uploaded documents
15
+ def summarize_document(file, max_length, min_length):
16
+ # Read the content of the file
17
+ if file.name.endswith(".pdf"):
18
+ reader = PdfReader(file.name)
19
+ text = ""
20
+ for page in reader.pages:
21
+ text += page.extract_text()
22
+ else:
23
+ text = file.read().decode("utf-8")
24
+
25
+ # Summarize the extracted text
26
+ if len(text) > 1024: # Summarizer has input size limitations
27
+ text = text[:1024]
28
+
29
+ summary = summarize_text(text, max_length, min_length)
30
+ return summary
31
+
32
+ # Gradio Interface
33
+ with gr.Blocks() as interface:
34
+ gr.Markdown("# Document Summarizer")
35
+
36
+ with gr.Tab("Text Summarization"):
37
+ input_text = gr.Textbox(lines=10, placeholder="Enter the text to summarize...", label="Input Text")
38
+ max_len = gr.Slider(50, 300, value=130, label="Max Summary Length")
39
+ min_len = gr.Slider(10, 50, value=30, label="Min Summary Length")
40
+ output_text = gr.Textbox(lines=5, label="Summary")
41
+ summarize_button = gr.Button("Summarize")
42
+
43
+ summarize_button.click(summarize_text, [input_text, max_len, min_len], output_text)
44
+
45
+ with gr.Tab("Document Summarization"):
46
+ input_file = gr.File(label="Upload a Document (PDF or TXT)")
47
+ max_len_doc = gr.Slider(50, 300, value=130, label="Max Summary Length")
48
+ min_len_doc = gr.Slider(10, 50, value=30, label="Min Summary Length")
49
+ output_doc = gr.Textbox(lines=5, label="Summary")
50
+ summarize_doc_button = gr.Button("Summarize Document")
51
+
52
+ summarize_doc_button.click(summarize_document, [input_file, max_len_doc, min_len_doc], output_doc)
53
+
54
+ # Launch the app
55
+ if __name__ == "__main__":
56
+ interface.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio
2
+ transformers
3
+ torch
4
+ PyPDF2