aspendse commited on
Commit
b715724
·
verified ·
1 Parent(s): abb14a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -45
app.py CHANGED
@@ -1,63 +1,86 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
  from PyPDF2 import PdfReader
4
- import docx
5
- from io import BytesIO
 
6
 
7
- # Load summarization model
8
  summarizer = pipeline("summarization")
9
 
10
- # Extract text from PDF
11
  def extract_text_from_pdf(file):
12
  reader = PdfReader(file)
13
  text = ""
14
  for page in reader.pages:
15
- page_text = page.extract_text()
16
- if page_text:
17
- text += page_text
18
  return text
19
 
20
- # Extract text from Word document
21
  def extract_text_from_docx(file):
22
- doc = docx.Document(file)
23
  return "\n".join([para.text for para in doc.paragraphs])
24
 
25
- # Save summary as a Word file
26
- def save_summary_to_word(summary_text):
27
- doc = docx.Document()
28
- doc.add_heading("Summary", level=1)
29
- doc.add_paragraph(summary_text)
30
- output = BytesIO()
31
- doc.save(output)
32
- output.seek(0)
33
- return output
 
 
 
 
 
 
 
 
 
34
 
35
  # Main function
36
- def summarize_file(file):
37
- if file.name.endswith(".pdf"):
38
- text = extract_text_from_pdf(file)
39
- elif file.name.endswith(".docx"):
40
- text = extract_text_from_docx(file)
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  else:
42
- return "Unsupported file type", None
43
-
44
- # Limit length for large documents
45
- if len(text) > 3000:
46
- text = text[:3000]
47
-
48
- summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
49
- summary_text = summary[0]['summary_text']
50
- summary_doc = save_summary_to_word(summary_text)
51
- return summary_text, ("summary.docx", summary_doc)
52
-
53
- # Gradio interface
54
- gr.Interface(
55
- fn=summarize_file,
56
- inputs=gr.File(file_types=[".pdf", ".docx"], label="Upload PDF or Word Document"),
57
- outputs=[
58
- gr.Textbox(label="Summary"),
59
- gr.File(label="Download Summary as Word File")
60
- ],
61
- title="📄 Smart Document Summarizer",
62
- description="Upload a PDF or Word file. This app will summarize it using AI and give you a downloadable summary."
63
- ).launch()
 
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
  from PyPDF2 import PdfReader
4
+ from docx import Document
5
+ from fpdf import FPDF
6
+ import os
7
 
8
+ # Load the summarizer pipeline
9
  summarizer = pipeline("summarization")
10
 
11
+ # Helper to extract text from PDF
12
  def extract_text_from_pdf(file):
13
  reader = PdfReader(file)
14
  text = ""
15
  for page in reader.pages:
16
+ text += page.extract_text()
 
 
17
  return text
18
 
19
+ # Helper to extract text from DOCX
20
  def extract_text_from_docx(file):
21
+ doc = Document(file)
22
  return "\n".join([para.text for para in doc.paragraphs])
23
 
24
+ # Helper to save summary as DOCX
25
+ def save_as_docx(text):
26
+ doc = Document()
27
+ doc.add_heading("Summary", 0)
28
+ doc.add_paragraph(text)
29
+ path = "summary.docx"
30
+ doc.save(path)
31
+ return path
32
+
33
+ # Helper to save summary as PDF
34
+ def save_as_pdf(text):
35
+ pdf = FPDF()
36
+ pdf.add_page()
37
+ pdf.set_font("Arial", size=12)
38
+ pdf.multi_cell(0, 10, text)
39
+ path = "summary.pdf"
40
+ pdf.output(path)
41
+ return path
42
 
43
  # Main function
44
+ def summarize_input(text, file, export_type):
45
+ if file:
46
+ ext = os.path.splitext(file.name)[-1].lower()
47
+ if ext == ".pdf":
48
+ text = extract_text_from_pdf(file)
49
+ elif ext == ".docx":
50
+ text = extract_text_from_docx(file)
51
+ else:
52
+ return "Unsupported file type", None
53
+
54
+ if not text.strip():
55
+ return "No text to summarize.", None
56
+
57
+ summary = summarizer(text, max_length=130, min_length=30, do_sample=False)[0]['summary_text']
58
+
59
+ # Save based on export type
60
+ if export_type == "PDF":
61
+ file_path = save_as_pdf(summary)
62
  else:
63
+ file_path = save_as_docx(summary)
64
+
65
+ return summary, file_path
66
+
67
+ # UI
68
+ with gr.Blocks() as demo:
69
+ gr.Markdown("## 📝 Smart Text Summarizer\nUpload a file or enter text. Choose output format and get your summary!")
70
+
71
+ with gr.Row():
72
+ text_input = gr.Textbox(label="Enter Text", lines=8, placeholder="Or upload a file below instead...")
73
+ file_input = gr.File(label="Upload PDF or DOCX", file_types=[".pdf", ".docx"])
74
+
75
+ export_type = gr.Radio(["PDF", "Word (DOCX)"], label="Export As", value="PDF")
76
+ summarize_btn = gr.Button("Summarize")
77
+
78
+ output_text = gr.Textbox(label="Summary", lines=8)
79
+ download_link = gr.File(label="Download Summary")
80
+
81
+ summarize_btn.click(fn=summarize_input,
82
+ inputs=[text_input, file_input, export_type],
83
+ outputs=[output_text, download_link])
84
+
85
+ # Launch the app
86
+ demo.launch()