Spaces:
Runtime error
Runtime error
File size: 10,490 Bytes
0259f08 32d94cc 0259f08 31ea449 0259f08 3866c77 0259f08 0213085 0259f08 0213085 0259f08 b94d0bd b2745d7 0259f08 31ea449 b94d0bd 0259f08 31ea449 0259f08 31ea449 0259f08 c5836b4 cca4833 c5836b4 cca4833 c5836b4 cca4833 b94d0bd c5836b4 0259f08 b94d0bd 0259f08 b94d0bd 0259f08 21fe8fd 0259f08 21fe8fd 0259f08 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
import env_set
import os
env_set.env_set()
import streamlit as st
import time
import numpy as np
import pandas as pd
import PyPDF2
from pptx import Presentation
import openai
import subprocess
from io import BytesIO
# 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})
md_text = reply[4:] if reply[:3] == "---" else reply
md_text_list = md_text.split('\n')
# writedata.py
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()
subprocess.run(["./pandoc-2.14.2/bin/pandoc", "text2ppt_input.md", "-t", "pptx", "-o", "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
####### νλ©΄ μμ ########
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)
# ν
μ€νΈ
subprocess.run(["./pandoc-2.14.2/bin/pandoc", "text2ppt_test.md", "-t", "pptx", "-o", "output.pptx"], capture_output=True)
print(os.listdir(os.getcwd()))
prs = Presentation("output.pptx")
binary_output = BytesIO()
prs.save(binary_output)
st.download_button(label="Download PPT",
data = binary_output.getvalue(),
file_name="export_output.pptx",
mime='application/octet-stream', key = "<Text2PPT_test_download>")
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)
prs = Presentation("text2ppt_output.pptx")
binary_output = BytesIO()
prs.save(binary_output)
st.success('Done!')
st.download_button(label="Download PPT",
data = binary_output.getvalue(),
file_name="export_output.pptx",
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', key="<PPT2Script_pdf_uploader>")
elif choose == file_order[1]:
uploaded_file = st.file_uploader('Choose File!', type='pptx', key="<PPT2Script_ppt_uploader>")
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>")
|