Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import PyPDF2
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
from pptx import Presentation
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Fungsi untuk mengekstrak teks dari PDF
|
| 8 |
+
def extract_text_from_pdf(pdf_path):
|
| 9 |
+
with open(pdf_path, 'rb') as file:
|
| 10 |
+
reader = PyPDF2.PdfReader(file)
|
| 11 |
+
text = ""
|
| 12 |
+
for page in reader.pages:
|
| 13 |
+
text += page.extract_text()
|
| 14 |
+
return text
|
| 15 |
+
|
| 16 |
+
# Fungsi untuk menghasilkan poin utama dan prompt
|
| 17 |
+
def generate_prompts(text):
|
| 18 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 19 |
+
summary = summarizer(text, max_length=200, min_length=50, do_sample=False)[0]['summary_text']
|
| 20 |
+
points = summary.split(". ")[:10]
|
| 21 |
+
prompts = [f"Buat slide dengan judul 'Bagian {i+1}' dan isi: {point}" for i, point in enumerate(points)]
|
| 22 |
+
return prompts
|
| 23 |
+
|
| 24 |
+
# Fungsi untuk membuat presentasi
|
| 25 |
+
def create_presentation(prompts):
|
| 26 |
+
prs = Presentation()
|
| 27 |
+
for prompt in prompts:
|
| 28 |
+
slide_layout = prs.slide_layouts[1]
|
| 29 |
+
slide = prs.slides.add_slide(slide_layout)
|
| 30 |
+
title = slide.shapes.title
|
| 31 |
+
content = slide.placeholders[1]
|
| 32 |
+
title.text = prompt.split("dan isi:")[0].replace("Buat slide dengan judul ", "").strip("'")
|
| 33 |
+
content.text = prompt.split("dan isi:")[1].strip()
|
| 34 |
+
output_path = "output_presentation.pptx"
|
| 35 |
+
prs.save(output_path)
|
| 36 |
+
return output_path
|
| 37 |
+
|
| 38 |
+
# Fungsi untuk Gradio
|
| 39 |
+
def process_pdf(pdf_file):
|
| 40 |
+
text = extract_text_from_pdf(pdf_file)
|
| 41 |
+
prompts = generate_prompts(text)
|
| 42 |
+
output_path = create_presentation(prompts)
|
| 43 |
+
return output_path
|
| 44 |
+
|
| 45 |
+
# Antarmuka Gradio
|
| 46 |
+
iface = gr.Interface(
|
| 47 |
+
fn=process_pdf,
|
| 48 |
+
inputs=gr.File(label="Unggah PDF"),
|
| 49 |
+
outputs=gr.File(label="Unduh Presentasi"),
|
| 50 |
+
title="PDF to Presentation",
|
| 51 |
+
description="Unggah PDF untuk menghasilkan presentasi slide."
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
iface.launch()
|