Presentation-Assistant / presentation_assistant /presentation_assistant.py
Hyeonseo's picture
Update presentation_assistant/presentation_assistant.py
ac4f85c
raw
history blame
6.16 kB
import os
import PyPDF2
from pptx import Presentation
import openai
import subprocess
from io import BytesIO
import sys
sys.path.append("/home/user/app")
# text2ppt ν”„λ‘¬ν”„νŠΈ 생성 ν•¨μˆ˜
def generate_text2ppt_input_prompt(input_type, input_value, input_pages):
header = """
λ„ˆκ°€ markdown λ¬Έλ²•μœΌλ‘œ PPTλ₯Ό μ œμž‘ν•˜λŠ” λ””μžμ΄λ„ˆλΌκ³  κ°€μ •ν•˜κ³ , %sμž₯의 PPTλ₯Ό μž‘μ„±ν•΄.
+++ μ•„λž˜ λ‚΄μš© λ˜λŠ” 링크λ₯Ό μš”μ•½ν•΄μ„œ markdown μ–Έμ–΄λ‘œ μž‘μ„±ν•˜λŠ”λ°, === μ•„λž˜ κ·œμΉ™κ³Ό 지킀고, ~~~ μ•„λž˜ μŠ¬λΌμ΄λ“œ μ˜ˆμ‹œλ₯Ό μ°Έκ³ ν•΄.
+++
""" % input_pages
summary_value = ""
if input_type == "링크":
summary_value += input_value
summary_value += "\n"
elif input_type == "ν…μŠ€νŠΈ":
summary_value += input_value
summary_value += "\n"
elif input_type == "PDF":
with open(input_value, 'rb') as pdf_file:
pdf_reader = PyPDF2.PdfReader(pdf_file)
num_pages = len(pdf_reader.pages)
# 각 νŽ˜μ΄μ§€μ˜ λ‚΄μš©μ„ λ¬Έμžμ—΄λ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€.
text = ""
for page_num in range(num_pages):
page = pdf_reader.pages[page_num]
text += page.extract_text()
summary_value += text
summary_value += "\n"
else:
print("ERROR: 잘λͺ»λœ μž…λ ₯")
rule_value = """
===
- μ œμ‹œν•œ λ‚΄μš© λ˜λŠ” 링크의 λ‚΄μš©μ— λŒ€ν•΄μ„œλ§Œ μ‚¬μ‹€μ μœΌλ‘œ μž‘μ„±ν•΄μ€˜.
- μŠ¬λΌμ΄λ“œ κ΅¬λΆ„μžλ‘œ ---λ₯Ό 무쑰건 μ‚¬μš©ν•΄μ€˜.
- μ£Όμ œμ— μ μ ˆν•œ λ„ν˜•, 이미지(![이미지](이미지링크), https://unsplash.com/ko/images/stock/non-copyrighted μ—μ„œ μ‹€μ œλ‘œ μ‚¬μš© κ°€λŠ₯ν•œ), ν‘œ(|-|), 인용(>), κ°•μ‘°(bold, ``), 이λͺ¨μ§€(https://kr.piliapp.com/twitter-symbols/), μ•„μ΄μ½˜ (https://kr.piliapp.com/symbol/#popular) 등이 λ‹€μ–‘ν•˜κ²Œ μŠ¬λΌμ΄λ“œλ₯Ό λ””μžμΈν•˜κ³  λ°°μΉ˜ν•΄μ€˜.
- 이λͺ¨μ§€λŠ” μ΅œλŒ€ 2 νŽ˜μ΄μ§€μ— ν•œ 번만 μ‚¬μš©ν•˜κ³ , λ‹€λ₯Έ λ””μžμΈμ„ λ‹€μ–‘ν•˜κ²Œ μ‚¬μš©ν•΄μ€˜.
- 이미지와 ν‘œλ₯Ό μ‚¬μš©ν•  λ•Œ, νŽ˜μ΄μ§€ 크기와 κ³ λ €ν•΄μ„œ κΈ€ λ‚΄μš©μ΄ λͺ¨λ‘ λ‚˜νƒ€λ‚˜λ„λ‘ 크기λ₯Ό μ§€μ •ν•΄μ€˜.
- Slide 1λ₯Ό 제λͺ©μœΌλ‘œ ν•΄μ„œ 총 %sμž₯이야.
- PPT의 λ‚΄μš©μ„ ν’λΆ€ν•˜κ²Œ λ§ˆν¬λ‹€μš΄μœΌλ‘œ μž‘μ„±ν•΄μ€˜.
- μŠ¬λΌμ΄λ“œ λ³„λ‘œ μ„€λͺ…ν•˜μ§€λ§κ³ , μ½”λ“œλ§Œ μž‘μ„±ν•΄μ€˜.
- μ˜ˆμ‹œμ˜ λ‚΄μš©μ„ μ‚¬μš©ν•΄μ„œ μž‘μ„±ν•˜μ§€λ§κ³ , ν˜•μ‹λ§Œ μ°Έκ³ ν•΄.
~~~
<!-- Slide 0. μŠ¬λΌμ΄λ“œ 주제 -->
# μŠ¬λΌμ΄λ“œ 제λͺ©
![이미지링크](https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo-with-title.png)
- ChatGPTλ₯Ό ν™œμš©ν•œ πŸ€—**TEXT2PPT μ„œλΉ„μŠ€ PA!**μž…λ‹ˆλ‹€.
- `링크`,`ν…μŠ€νŠΈ`, `PDF`λ₯Ό μž…λ ₯ λ˜λŠ” μ—…λ‘œλ“œν•˜λ©΄, PPT둜 λ³€ν™˜ν•©λ‹ˆλ‹€.
""" % input_pages
return header + summary_value + rule_value
# text2ppt μ‹€ν–‰ ν•¨μˆ˜
def text2ppt(token_key, input_prompt, input_theme):
openai.api_key = token_key
messages = [
{"role": "system", "content": "You are a kind helpful PPT designer."},
]
message = input_prompt
if message:
messages.append(
{"role": "user", "content": message},
)
chat = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0301", messages=messages
)
reply = chat.choices[0].message.content
messages.append({"role": "assistant", "content": reply})
md_text = reply[4:] if reply[:3] == "---" else reply
md_text_list = md_text.split('\n')
f = open("text2ppt_input.md", 'w')
for i in range(0, len(md_text_list)):
data = md_text_list[i] + "\n"
f.write(data)
f.close()
if input_theme == 'default':
subprocess.run(["/home/user/app/pandoc-2.14.2/bin/pandoc", "/home/user/app/text2ppt_input.md", "-t", "pptx", "-o", "/home/user/app/text2ppt_output.pptx"], capture_output=True)
else:
ppt_theme = "--reference-doc=/home/user/app/template/"+input_theme+".pptx"
subprocess.run(["/home/user/app/pandoc-2.14.2/bin/pandoc", "/home/user/app/text2ppt_input.md", "-t", "pptx", ppt_theme, "-o", "/home/user/app/text2ppt_output.pptx"], capture_output=True)
def ppt2script(token_key, input_file, input_type):
openai.api_key = token_key
if input_type=="PDF":
with open(input_file, 'rb') as pdf_file:
pdf_reader = PyPDF2.PdfReader(pdf_file)
num_pages = len(pdf_reader.pages)
# 각 νŽ˜μ΄μ§€μ˜ λ‚΄μš©μ„ λ¬Έμžμ—΄λ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€.
text = ""
for page_num in range(num_pages):
page = pdf_reader.pages[page_num]
text += "[PAGE_NUM " + str(page_num + 1) + "]"
text += page.extract_text()
else:
prs = Presentation(input_file)
text = ""
page_num = 0
for slide in prs.slides:
text += "[PAGE_NUM " + str(page_num + 1) + "]"
page_num += 1
for shape in slide.shapes:
if not shape.has_text_frame:
continue
for paragraph in shape.text_frame.paragraphs:
for run in paragraph.runs:
text += run.text
header = """
λ„ˆλŠ” PPT λ°œν‘œμ— 도움을 μ£ΌλŠ” μ‘°λ ₯μžμ•Ό.
~~~μ•„λž˜ κ·œμΉ™μ„ 지킀고, --- μ•„λž˜ PPT λ‚΄μš©μ— λŒ€ν•΄ λ°œν‘œ 슀크립트λ₯Ό ν•œκΈ€λ‘œ μž‘μ„±ν•΄.
~~~
- [PAGE_NUM 1] 일 λ•Œ, μ—¬κΈ°μ„œ 1은 νŽ˜μ΄μ§€ 번호인데, νŽ˜μ΄μ§€ λ²ˆν˜Έλ§ˆλ‹€ λ°œν‘œ 슀크립트λ₯Ό μž‘μ„±ν•΄.
- λ§ˆν¬λ‹€μš΄ μ–Έμ–΄λ₯Ό μ“°μ§€μ•Šκ³ , ν…μŠ€νŠΈλ‘œλ§Œ μž‘μ„±ν•΄.
- PPT λ‚΄μš©μ— 좔가적인 μ„€λͺ…μ΄λ‚˜ 사둀λ₯Ό λ§λΆ™μ—¬μ€˜.
---
"""
input_prompt = header + text
messages = [
{"role": "system", "content": "You are a kind helpful PPT Assistant."},
]
message = input_prompt
if message:
messages.append(
{"role": "user", "content": message},
)
chat = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0301", messages=messages
)
reply = chat.choices[0].message.content
messages.append({"role": "assistant", "content": reply})
return reply