Razzaqi3143 commited on
Commit
726da04
·
verified ·
1 Parent(s): ca5faaa

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from pptx import Presentation
3
+ from pptx.util import Inches
4
+ import pdfplumber
5
+ from transformers import pipeline
6
+ import tempfile
7
+
8
+ # Initialize the summarization model
9
+ summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
10
+
11
+ # Function to process PDF and create PowerPoint
12
+ def pdf_to_pptx(pdf_file):
13
+ # Step 1: Extract text from the PDF
14
+ pdf_text = ""
15
+ with pdfplumber.open(pdf_file) as pdf:
16
+ for page in pdf.pages:
17
+ pdf_text += page.extract_text()
18
+
19
+ # Step 2: Summarize text
20
+ def chunk_text(text, max_len=1024):
21
+ return [text[i:i+max_len] for i in range(0, len(text), max_len)]
22
+
23
+ chunked_text = chunk_text(pdf_text)
24
+ summarized_text = " ".join([summarizer(chunk)[0]['summary_text'] for chunk in chunked_text])
25
+
26
+ # Step 3: Create PowerPoint presentation
27
+ presentation = Presentation()
28
+ slide_content = summarized_text.split(". ")
29
+
30
+ for i, content in enumerate(slide_content):
31
+ slide = presentation.slides.add_slide(presentation.slide_layouts[1]) # Title and Content layout
32
+ title = slide.shapes.title
33
+ body = slide.placeholders[1]
34
+ title.text = f"Slide {i+1}" # Slide title
35
+ body.text = content # Slide content
36
+
37
+ # Step 4: Save presentation to a temporary file and return path
38
+ temp_pptx = tempfile.NamedTemporaryFile(delete=False, suffix=".pptx")
39
+ presentation.save(temp_pptx.name)
40
+ return temp_pptx.name
41
+
42
+ # Gradio app interface
43
+ def generate_pptx(pdf_file):
44
+ pptx_file_path = pdf_to_pptx(pdf_file.name)
45
+ return pptx_file_path
46
+
47
+ # Define Gradio interface
48
+ interface = gr.Interface(
49
+ fn=generate_pptx,
50
+ inputs=gr.File(label="Upload PDF File"),
51
+ outputs=gr.File(label="Download PowerPoint Presentation"),
52
+ title="PDF to PowerPoint Converter",
53
+ description="Upload a PDF file to convert it into a professional PowerPoint presentation."
54
+ )
55
+
56
+ # Launch the app
57
+ interface.launch()