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
|
| 1 |
+
import os
|
| 2 |
+
import PyPDF2
|
| 3 |
+
from pptx import Presentation
|
| 4 |
+
import openai
|
| 5 |
+
import subprocess
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
|
| 8 |
+
# text2ppt ν둬ννΈ μμ± ν¨μ
|
| 9 |
+
def generate_text2ppt_input_prompt(input_type, input_value, input_pages):
|
| 10 |
+
header = """
|
| 11 |
+
λκ° markdown λ¬Έλ²μΌλ‘ PPTλ₯Ό μ μνλ λμμ΄λλΌκ³ κ°μ νκ³ , %sμ₯μ PPTλ₯Ό μμ±ν΄.
|
| 12 |
+
+++ μλ λ΄μ© λλ λ§ν¬λ₯Ό μμ½ν΄μ markdown μΈμ΄λ‘ μμ±νλλ°, === μλ κ·μΉκ³Ό μ§ν€κ³ , ~~~ μλ μ¬λΌμ΄λ μμλ₯Ό μ°Έκ³ ν΄.
|
| 13 |
+
+++
|
| 14 |
+
""" % input_pages
|
| 15 |
+
|
| 16 |
+
summary_value = ""
|
| 17 |
+
|
| 18 |
+
if input_type == "λ§ν¬":
|
| 19 |
+
summary_value += input_value
|
| 20 |
+
summary_value += "ν
μ€νΈ"
|
| 21 |
+
elif input_type == "text":
|
| 22 |
+
summary_value += input_value
|
| 23 |
+
summary_value += "\n"
|
| 24 |
+
elif input_type == "PDF":
|
| 25 |
+
with open(input_value, 'rb') as pdf_file:
|
| 26 |
+
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
| 27 |
+
num_pages = len(pdf_reader.pages)
|
| 28 |
+
|
| 29 |
+
# κ° νμ΄μ§μ λ΄μ©μ λ¬Έμμ΄λ‘ λ³νν©λλ€.
|
| 30 |
+
text = ""
|
| 31 |
+
for page_num in range(num_pages):
|
| 32 |
+
page = pdf_reader.pages[page_num]
|
| 33 |
+
text += page.extract_text()
|
| 34 |
+
summary_value += text
|
| 35 |
+
summary_value += "\n"
|
| 36 |
+
else:
|
| 37 |
+
print("ERROR: μλͺ»λ μ
λ ₯")
|
| 38 |
+
|
| 39 |
+
rule_value = """
|
| 40 |
+
===
|
| 41 |
+
- μ μν λ΄μ© λλ λ§ν¬μ λ΄μ©μ λν΄μλ§ μ¬μ€μ μΌλ‘ μμ±ν΄μ€.
|
| 42 |
+
- μ¬λΌμ΄λ ꡬλΆμλ‘ ---λ₯Ό 무쑰건 μ¬μ©ν΄μ€.
|
| 43 |
+
- μ£Όμ μ μ μ ν λν, μ΄λ―Έμ§(, https://unsplash.com/ko/images/stock/non-copyrighted μμ μ€μ λ‘ μ¬μ© κ°λ₯ν), ν(|-|), μΈμ©(>), κ°μ‘°(bold, ``), μ΄λͺ¨μ§(https://kr.piliapp.com/twitter-symbols/), μμ΄μ½ (https://kr.piliapp.com/symbol/#popular) λ±μ΄ λ€μνκ² μ¬λΌμ΄λλ₯Ό λμμΈνκ³ λ°°μΉν΄μ€.
|
| 44 |
+
- μ΄λͺ¨μ§λ μ΅λ 2 νμ΄μ§μ ν λ²λ§ μ¬μ©νκ³ , λ€λ₯Έ λμμΈμ λ€μνκ² μ¬μ©ν΄μ€.
|
| 45 |
+
- μ΄λ―Έμ§μ νλ₯Ό μ¬μ©ν λ, νμ΄μ§ ν¬κΈ°μ κ³ λ €ν΄μ κΈ λ΄μ©μ΄ λͺ¨λ λνλλλ‘ ν¬κΈ°λ₯Ό μ§μ ν΄μ€.
|
| 46 |
+
- Slide 1λ₯Ό μ λͺ©μΌλ‘ ν΄μ μ΄ %sμ₯μ΄μΌ.
|
| 47 |
+
- PPTμ λ΄μ©μ νλΆνκ² λ§ν¬λ€μ΄μΌλ‘ μμ±ν΄μ€.
|
| 48 |
+
- μ¬λΌμ΄λ λ³λ‘ μ€λͺ
νμ§λ§κ³ , μ½λλ§ μμ±ν΄μ€.
|
| 49 |
+
- μμμ λ΄μ©μ μ¬μ©ν΄μ μμ±νμ§λ§κ³ , νμλ§ μ°Έκ³ ν΄.
|
| 50 |
+
~~~
|
| 51 |
+
<!-- Slide 0. μ¬λΌμ΄λ μ£Όμ -->
|
| 52 |
+
# μ¬λΌμ΄λ μ λͺ©
|
| 53 |
+

|
| 54 |
+
- ChatGPTλ₯Ό νμ©ν π€**TEXT2PPT μλΉμ€ PA!**μ
λλ€.
|
| 55 |
+
- `λ§ν¬`,`ν
μ€νΈ`, `PDF`λ₯Ό μ
λ ₯ λλ μ
λ‘λνλ©΄, PPTλ‘ λ³νν©λλ€.
|
| 56 |
+
""" % input_pages
|
| 57 |
+
|
| 58 |
+
return header + summary_value + rule_value
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
# text2ppt μ€ν ν¨μ
|
| 62 |
+
def text2ppt(token_key, input_prompt, input_theme):
|
| 63 |
+
openai.api_key = token_key
|
| 64 |
+
|
| 65 |
+
messages = [
|
| 66 |
+
{"role": "system", "content": "You are a kind helpful PPT designer."},
|
| 67 |
+
]
|
| 68 |
+
|
| 69 |
+
message = input_prompt
|
| 70 |
+
|
| 71 |
+
if message:
|
| 72 |
+
messages.append(
|
| 73 |
+
{"role": "user", "content": message},
|
| 74 |
+
)
|
| 75 |
+
chat = openai.ChatCompletion.create(
|
| 76 |
+
model="gpt-3.5-turbo-0301", messages=messages
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
reply = chat.choices[0].message.content
|
| 80 |
+
messages.append({"role": "assistant", "content": reply})
|
| 81 |
+
|
| 82 |
+
md_text = reply[4:] if reply[:3] == "---" else reply
|
| 83 |
+
md_text_list = md_text.split('\n')
|
| 84 |
+
|
| 85 |
+
f = open("text2ppt_input.md", 'w')
|
| 86 |
+
for i in range(0, len(md_text_list)):
|
| 87 |
+
data = md_text_list[i] + "\n"
|
| 88 |
+
f.write(data)
|
| 89 |
+
f.close()
|
| 90 |
+
|
| 91 |
+
if input_theme == 'default':
|
| 92 |
+
subprocess.run(["./pandoc-2.14.2/bin/pandoc", "text2ppt_input.md", "-t", "pptx", "-o", "text2ppt_output.pptx"], capture_output=True)
|
| 93 |
+
else:
|
| 94 |
+
ppt_theme = "--reference-doc="+input_theme+".pptx"
|
| 95 |
+
subprocess.run(["./pandoc-2.14.2/bin/pandoc", "text2ppt_input.md", "-t", "pptx", ppt_theme, "-o", "text2ppt_output.pptx"], capture_output=True)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def ppt2script(token_key, input_file, input_type):
|
| 99 |
+
openai.api_key = token_key
|
| 100 |
+
|
| 101 |
+
if input_type=="PDF":
|
| 102 |
+
with open(input_file, 'rb') as pdf_file:
|
| 103 |
+
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
| 104 |
+
num_pages = len(pdf_reader.pages)
|
| 105 |
+
|
| 106 |
+
# κ° νμ΄μ§μ λ΄μ©μ λ¬Έμμ΄λ‘ λ³νν©λλ€.
|
| 107 |
+
text = ""
|
| 108 |
+
for page_num in range(num_pages):
|
| 109 |
+
page = pdf_reader.pages[page_num]
|
| 110 |
+
text += "[PAGE_NUM " + str(page_num + 1) + "]"
|
| 111 |
+
text += page.extract_text()
|
| 112 |
+
else:
|
| 113 |
+
prs = Presentation(input_file)
|
| 114 |
+
|
| 115 |
+
text = ""
|
| 116 |
+
page_num = 0
|
| 117 |
+
for slide in prs.slides:
|
| 118 |
+
text += "[PAGE_NUM " + str(page_num + 1) + "]"
|
| 119 |
+
page_num += 1
|
| 120 |
+
for shape in slide.shapes:
|
| 121 |
+
if not shape.has_text_frame:
|
| 122 |
+
continue
|
| 123 |
+
for paragraph in shape.text_frame.paragraphs:
|
| 124 |
+
for run in paragraph.runs:
|
| 125 |
+
text += run.text
|
| 126 |
+
|
| 127 |
+
header = """
|
| 128 |
+
λλλ PPT λ°νμ λμμ μ£Όλ μ‘°λ ₯μμΌ.
|
| 129 |
+
~~~μλ κ·μΉμ μ§ν€κ³ , --- μλ PPT λ΄μ©μ λν΄ λ°ν μ€ν¬λ¦½νΈλ₯Ό νκΈλ‘ μμ±ν΄.
|
| 130 |
+
~~~
|
| 131 |
+
- [PAGE_NUM 1] μΌ λ, μ¬κΈ°μ 1μ νμ΄μ§ λ²νΈμΈλ°, νμ΄μ§ λ²νΈλ§λ€ λ°ν μ€ν¬λ¦½νΈλ₯Ό μμ±ν΄.
|
| 132 |
+
- λ§ν¬λ€μ΄ μΈμ΄λ₯Ό μ°μ§μκ³ , ν
μ€νΈλ‘λ§ μμ±ν΄.
|
| 133 |
+
- PPT λ΄μ©μ μΆκ°μ μΈ μ€λͺ
μ΄λ μ¬λ‘λ₯Ό λ§λΆμ¬μ€.
|
| 134 |
+
---
|
| 135 |
+
"""
|
| 136 |
+
|
| 137 |
+
input_prompt = header + text
|
| 138 |
+
|
| 139 |
+
messages = [
|
| 140 |
+
{"role": "system", "content": "You are a kind helpful PPT Assistant."},
|
| 141 |
+
]
|
| 142 |
+
|
| 143 |
+
message = input_prompt
|
| 144 |
+
|
| 145 |
+
if message:
|
| 146 |
+
messages.append(
|
| 147 |
+
{"role": "user", "content": message},
|
| 148 |
+
)
|
| 149 |
+
chat = openai.ChatCompletion.create(
|
| 150 |
+
model="gpt-3.5-turbo-0301", messages=messages
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
reply = chat.choices[0].message.content
|
| 154 |
+
messages.append({"role": "assistant", "content": reply})
|
| 155 |
+
|
| 156 |
+
return reply
|