aspendse commited on
Commit
0c5099f
·
verified ·
1 Parent(s): 14db8b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -10
app.py CHANGED
@@ -1,19 +1,63 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
 
 
3
 
4
- # Load the summarization pipeline
5
  summarizer = pipeline("summarization")
6
 
7
- # Function to summarize text
8
- def summarize_text(text):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
10
- return summary[0]['summary_text']
 
 
11
 
12
- # Create Gradio Interface
13
  gr.Interface(
14
- fn=summarize_text,
15
- inputs=gr.Textbox(lines=10, label="Enter Text to Summarize"),
16
- outputs=gr.Textbox(label="Summary"),
17
- title="📝 Text Summarizer",
18
- description="Paste a paragraph and get a quick summary using a Hugging Face transformer model."
 
 
 
19
  ).launch()
 
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()