Spaces:
Runtime error
Runtime error
import env_set | |
env_set.env_set() | |
import streamlit as st | |
import time | |
import numpy as np | |
import pandas as pd | |
import PyPDF2 | |
import openai | |
import subprocess | |
# text2ppt ν둬ννΈ μμ± ν¨μ | |
def generate_text2ppt_input_prompt(input_type, input_value, input_pages): | |
header = """ | |
λκ° Marp λ¬Έλ²μΌλ‘ PPTλ₯Ό μ μνλ λμμ΄λλΌκ³ κ°μ νκ³ , %sμ₯μ PPTλ₯Ό μμ±ν΄. | |
+++ μλ λ΄μ© λλ λ§ν¬λ₯Ό μμ½ν΄μ λ§ν¬λ€μ΄ μΈμ΄λ‘ μμ±νλλ°, === μλ κ·μΉκ³Ό μ§ν€κ³ , ~~~ μλ μ¬λΌμ΄λ μμλ₯Ό μ°Έκ³ ν΄. | |
+++ | |
""" % input_pages | |
summary_value = "" | |
if input_type == "λ§ν¬": | |
summary_value += input_value | |
summary_value += "ν μ€νΈ" | |
elif input_type == "text": | |
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}) | |
revised_reply = reply[4:] if reply[:3] == "---" else reply | |
ppt_theme = input_theme | |
marp_header = """--- | |
marp: true | |
theme: %s | |
footer: 'PA!(Presentation Assistant)' | |
paginate: true | |
--- | |
""" % input_theme | |
md_text = marp_header + revised_reply | |
md_text_list = md_text.split('\n') | |
# writedata.py | |
f = open("text2ppt_test.md", 'w') | |
for i in range(0, len(md_text_list)): | |
data = md_text_list[i] + "\n" | |
f.write(data) | |
f.close() | |
subprocess.run( | |
f"npx @marp-team/marp-cli@latest --pdf-fonts-dir=/usr/share/fonts/truetype/nanum --pdf-default-font=NanumGothic -o output.pdf text2ppt_test.md --chrome-path=/usr/bin/google-chrome-stable", | |
shell=True) | |
def ppt2script(token_key, input_file, input_type): | |
openai.api_key = token_key | |
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() | |
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 | |
####### νλ©΄ μμ ######## | |
tab1, tab2, tab3 = st.tabs(['PA!λ?', 'Text2PPT', 'PPT2Script']) | |
with tab1: | |
st.header('μκ°') | |
st.title('PA!(Presentation Assistant):sparkles:') | |
contents = """ | |
: μ¬μ©μκ° μ λ ₯ν λ΄μ©μ κΈ°λ°μΌλ‘ PPTλ₯Ό :blue[μλ μ μ]νκ³ , | |
νλ μ ν μ΄μ :red[μ€ν¬λ¦½νΈλ₯Ό μ 곡]νμ¬ νλ μ ν μ΄μ μλμ ν₯μμν΅λλ€!""" | |
st.markdown(contents) | |
st.markdown('-------------------------') | |
st.header('μ¬μ©λ²') | |
st.subheader('Text2PPT') | |
contents = """ | |
: μ¬μ©μμκ² λ§ν¬λ νμΌμ μ λ¬λ°μΌλ©΄ κ·Έ λ΄μ©μΌλ‘ :blue[λ°ν μλ£λ₯Ό μ μ]ν΄ λ립λλ€! | |
μ¬μ©μλ μνλ ν λ§(ν νλ¦Ώ) μ’ λ₯μ νμ΄μ§ μλ§ μ ννμΈμ!""" | |
st.markdown(contents) | |
st.subheader('PPT2Script') | |
contents = """ | |
: ppt λλ pdf λ°ν μλ£λ₯Ό μ¬μ©μλ‘λΆν° μ 곡λ°μΌλ©΄ μλμΌλ‘ :blue[λ°ν λλ³Έ]μ λ§λ€μ΄λ립λλ€!""" | |
st.markdown(contents) | |
with tab2: | |
st.header('Text2PPT') | |
gpt_token = st.text_input('μ± gptν ν°μ μ λ ₯ν΄ μ£ΌμΈμ.', key="<Text2PPT_token>") | |
st.markdown('-------------------------') | |
st.subheader(':computer:λ¬Έμ ppt μλ μμ±κΈ°:computer:') | |
thema_select = st.selectbox( | |
'μνλ ν λ§λ₯Ό μ ννμΈμ', | |
['default', 'gaia', 'uncover']) | |
st.markdown('-------------------------') | |
page_choice = st.slider('ppt νμ΄μ§ μ₯μ', min_value=2, max_value=10, step=1, value=5) | |
st.markdown('-------------------------') | |
my_order = ['ν μ€νΈ', 'λ§ν¬', 'PDF'] | |
status = st.radio('νμΌ μ’ λ₯λ₯Ό μ ννκ³ λ΄μ©μ μ λ ₯νμΈμ! :smile: ', my_order) | |
# 첫λ²μ§Έ λ°©λ² | |
if status == my_order[0]: | |
input_text = st.text_area('textλ₯Ό μ λ ₯νμΈμ', height=5) | |
elif status == my_order[1]: | |
input_text = st.text_area('urlλ₯Ό μ λ ₯νμΈμ', height=5) | |
elif status == my_order[2]: | |
input_text = st.file_uploader('νμΌμ μ λ‘λ νμΈμ', type=['pdf']) | |
input_text_check = st.button('νμΈ', key="<Text2PPT_start>") # μ΄ λ²νΌ λλ₯΄λ©΄ μ λ ₯ν μ€νΈκ° λμ΄κ°κ² ν΄μΌν¨ | |
st.markdown('-------------------------') | |
if input_text_check == True: | |
with st.spinner('Wait for it...'): | |
text2ppt(gpt_token, generate_text2ppt_input_prompt(status, input_text, page_choice), thema_select) | |
with open("output.pdf", "rb") as pdf_file: | |
PDFbyte = pdf_file.read() | |
st.success('Done!') | |
st.download_button(label="Download PPT", | |
data=PDFbyte, | |
file_name="export_output.pdf", | |
mime='application/octet-stream', key="<Text2PPT_download>") | |
with tab3: | |
st.header('PPT2Script') | |
gpt_token = st.text_input('μ±gptν ν°μ μ λ ₯ν΄μ£ΌμΈμ.', key="<PPT2Script_token>") | |
st.markdown('-------------------------') | |
st.subheader(':bookmark_tabs:λ°ν λλ³Έ μμ±κΈ°') | |
file_order = ['PDF', 'PPT'] | |
choose = st.radio('λ°ν μλ£μ νμΌ νμμ μ νν΄ μ£ΌμΈμ', file_order) | |
if choose == file_order[0]: | |
uploaded_file = st.file_uploader('Choose File!', type='pdf') | |
elif choose == file_order[1]: | |
uploaded_file = st.file_uploader('Choose File!', type='pptx') | |
input_file_check = st.button('νμΈ', key="<PPT2Script_start>") # μ΄ λ²νΌ λλ₯΄λ©΄ μ λ ₯ νμΌμ΄ λμ΄κ°κ² ν΄μΌν¨ | |
st.markdown('-------------------------') | |
if input_file_check == True: | |
with st.spinner('Wait for it...'): | |
with open(uploaded_file.name, mode='wb') as w: | |
w.write(uploaded_file.getvalue()) | |
script = ppt2script(gpt_token, uploaded_file.name, choose) | |
st.success('Done!') | |
st.download_button('Download Script', | |
data=script, file_name="script_output.txt", key="<PPT2Script_download>") | |