Spaces:
Runtime error
Runtime error
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 |