Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,170 +1,16 @@
|
|
1 |
-
import env_set
|
2 |
import os
|
|
|
3 |
env_set.env_set()
|
4 |
|
5 |
import streamlit as st
|
6 |
-
import time
|
7 |
-
import numpy as np
|
8 |
-
import pandas as pd
|
9 |
import PyPDF2
|
10 |
-
from pptx import Presentation
|
11 |
import openai
|
12 |
import subprocess
|
13 |
from io import BytesIO
|
|
|
14 |
|
|
|
15 |
|
16 |
-
# text2ppt ν둬ννΈ μμ± ν¨μ
|
17 |
-
def generate_text2ppt_input_prompt(input_type, input_value, input_pages):
|
18 |
-
header = """
|
19 |
-
λκ° markdown λ¬Έλ²μΌλ‘ PPTλ₯Ό μ μνλ λμμ΄λλΌκ³ κ°μ νκ³ , %sμ₯μ PPTλ₯Ό μμ±ν΄.
|
20 |
-
+++ μλ λ΄μ© λλ λ§ν¬λ₯Ό μμ½ν΄μ markdown μΈμ΄λ‘ μμ±νλλ°, === μλ κ·μΉκ³Ό μ§ν€κ³ , ~~~ μλ μ¬λΌμ΄λ μμλ₯Ό μ°Έκ³ ν΄.
|
21 |
-
+++
|
22 |
-
""" % input_pages
|
23 |
-
|
24 |
-
summary_value = ""
|
25 |
-
|
26 |
-
if input_type == "λ§ν¬":
|
27 |
-
summary_value += input_value
|
28 |
-
summary_value += "ν
μ€νΈ"
|
29 |
-
elif input_type == "text":
|
30 |
-
summary_value += input_value
|
31 |
-
summary_value += "\n"
|
32 |
-
elif input_type == "PDF":
|
33 |
-
with open(input_value, 'rb') as pdf_file:
|
34 |
-
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
35 |
-
num_pages = len(pdf_reader.pages)
|
36 |
-
|
37 |
-
# κ° νμ΄μ§μ λ΄μ©μ λ¬Έμμ΄λ‘ λ³νν©λλ€.
|
38 |
-
text = ""
|
39 |
-
for page_num in range(num_pages):
|
40 |
-
page = pdf_reader.pages[page_num]
|
41 |
-
text += page.extract_text()
|
42 |
-
summary_value += text
|
43 |
-
summary_value += "\n"
|
44 |
-
else:
|
45 |
-
print("ERROR: μλͺ»λ μ
λ ₯")
|
46 |
-
|
47 |
-
rule_value = """
|
48 |
-
===
|
49 |
-
- μ μν λ΄μ© λλ λ§ν¬μ λ΄μ©μ λν΄μλ§ μ¬μ€μ μΌλ‘ μμ±ν΄μ€.
|
50 |
-
- μ¬λΌμ΄λ ꡬλΆμλ‘ ---λ₯Ό 무쑰건 μ¬μ©ν΄μ€.
|
51 |
-
- μ£Όμ μ μ μ ν λν, μ΄λ―Έμ§(![μ΄λ―Έμ§](μ΄λ―Έμ§λ§ν¬), https://unsplash.com/ko/images/stock/non-copyrighted μμ μ€μ λ‘ μ¬μ© κ°λ₯ν), ν(|-|), μΈμ©(>), κ°μ‘°(bold, ``), μ΄λͺ¨μ§(https://kr.piliapp.com/twitter-symbols/), μμ΄μ½ (https://kr.piliapp.com/symbol/#popular) λ±μ΄ λ€μνκ² μ¬λΌμ΄λλ₯Ό λμμΈνκ³ λ°°μΉν΄μ€.
|
52 |
-
- μ΄λͺ¨μ§λ μ΅λ 2 νμ΄μ§μ ν λ²λ§ μ¬μ©νκ³ , λ€λ₯Έ λμμΈμ λ€μνκ² μ¬μ©ν΄μ€.
|
53 |
-
- μ΄λ―Έμ§μ νλ₯Ό μ¬μ©ν λ, νμ΄μ§ ν¬κΈ°μ κ³ λ €ν΄μ κΈ λ΄μ©μ΄ λͺ¨λ λνλλλ‘ ν¬κΈ°λ₯Ό μ§μ ν΄μ€.
|
54 |
-
- Slide 1λ₯Ό μ λͺ©μΌλ‘ ν΄μ μ΄ %sμ₯μ΄μΌ.
|
55 |
-
- PPTμ λ΄μ©μ νλΆνκ² λ§ν¬λ€μ΄μΌλ‘ μμ±ν΄μ€.
|
56 |
-
- μ¬λΌμ΄λ λ³λ‘ μ€λͺ
νμ§λ§κ³ , μ½λλ§ μμ±ν΄μ€.
|
57 |
-
- μμμ λ΄μ©μ μ¬μ©ν΄μ μμ±νμ§λ§κ³ , νμλ§ μ°Έκ³ ν΄.
|
58 |
-
~~~
|
59 |
-
<!-- Slide 0. μ¬λΌμ΄λ μ£Όμ -->
|
60 |
-
# μ¬λΌμ΄λ μ λͺ©
|
61 |
-
![μ΄λ―Έμ§λ§ν¬](https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo-with-title.png)
|
62 |
-
- ChatGPTλ₯Ό νμ©ν π€**TEXT2PPT μλΉμ€ PA!**μ
λλ€.
|
63 |
-
- `λ§ν¬`,`ν
μ€νΈ`, `PDF`λ₯Ό μ
λ ₯ λλ μ
λ‘λνλ©΄, PPTλ‘ λ³νν©λλ€.
|
64 |
-
""" % input_pages
|
65 |
-
|
66 |
-
return header + summary_value + rule_value
|
67 |
-
|
68 |
-
|
69 |
-
# text2ppt μ€ν ν¨μ
|
70 |
-
def text2ppt(token_key, input_prompt, input_theme):
|
71 |
-
openai.api_key = token_key
|
72 |
-
|
73 |
-
messages = [
|
74 |
-
{"role": "system", "content": "You are a kind helpful PPT designer."},
|
75 |
-
]
|
76 |
-
|
77 |
-
message = input_prompt
|
78 |
-
|
79 |
-
if message:
|
80 |
-
messages.append(
|
81 |
-
{"role": "user", "content": message},
|
82 |
-
)
|
83 |
-
chat = openai.ChatCompletion.create(
|
84 |
-
model="gpt-3.5-turbo-0301", messages=messages
|
85 |
-
)
|
86 |
-
|
87 |
-
reply = chat.choices[0].message.content
|
88 |
-
messages.append({"role": "assistant", "content": reply})
|
89 |
-
|
90 |
-
md_text = reply[4:] if reply[:3] == "---" else reply
|
91 |
-
md_text_list = md_text.split('\n')
|
92 |
-
|
93 |
-
f = open("text2ppt_input.md", 'w')
|
94 |
-
for i in range(0, len(md_text_list)):
|
95 |
-
data = md_text_list[i] + "\n"
|
96 |
-
f.write(data)
|
97 |
-
f.close()
|
98 |
-
|
99 |
-
if input_theme == 'default':
|
100 |
-
subprocess.run(["./pandoc-2.14.2/bin/pandoc", "text2ppt_input.md", "-t", "pptx", "-o", "text2ppt_output.pptx"], capture_output=True)
|
101 |
-
else:
|
102 |
-
ppt_theme = "--reference-doc="+input_theme+".pptx"
|
103 |
-
subprocess.run(["./pandoc-2.14.2/bin/pandoc", "text2ppt_input.md", "-t", "pptx", ppt_theme, "-o", "text2ppt_output.pptx"], capture_output=True)
|
104 |
-
|
105 |
-
|
106 |
-
def ppt2script(token_key, input_file, input_type):
|
107 |
-
openai.api_key = token_key
|
108 |
-
|
109 |
-
if input_type=="PDF":
|
110 |
-
with open(input_file, 'rb') as pdf_file:
|
111 |
-
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
112 |
-
num_pages = len(pdf_reader.pages)
|
113 |
-
|
114 |
-
# κ° νμ΄μ§μ λ΄μ©μ λ¬Έμμ΄λ‘ λ³νν©λλ€.
|
115 |
-
text = ""
|
116 |
-
for page_num in range(num_pages):
|
117 |
-
page = pdf_reader.pages[page_num]
|
118 |
-
text += "[PAGE_NUM " + str(page_num + 1) + "]"
|
119 |
-
text += page.extract_text()
|
120 |
-
else:
|
121 |
-
prs = Presentation(input_file)
|
122 |
-
|
123 |
-
text = ""
|
124 |
-
page_num = 0
|
125 |
-
for slide in prs.slides:
|
126 |
-
text += "[PAGE_NUM " + str(page_num + 1) + "]"
|
127 |
-
page_num += 1
|
128 |
-
for shape in slide.shapes:
|
129 |
-
if not shape.has_text_frame:
|
130 |
-
continue
|
131 |
-
for paragraph in shape.text_frame.paragraphs:
|
132 |
-
for run in paragraph.runs:
|
133 |
-
text += run.text
|
134 |
-
|
135 |
-
header = """
|
136 |
-
λλλ PPT λ°νμ λμμ μ£Όλ μ‘°λ ₯μμΌ.
|
137 |
-
~~~μλ κ·μΉμ μ§ν€κ³ , --- μλ PPT λ΄μ©μ λν΄ λ°ν μ€ν¬λ¦½νΈλ₯Ό νκΈλ‘ μμ±ν΄.
|
138 |
-
~~~
|
139 |
-
- [PAGE_NUM 1] μΌ λ, μ¬κΈ°μ 1μ νμ΄μ§ λ²νΈμΈλ°, νμ΄μ§ λ²νΈλ§λ€ λ°ν μ€ν¬λ¦½νΈλ₯Ό μμ±ν΄.
|
140 |
-
- λ§ν¬λ€μ΄ μΈμ΄λ₯Ό μ°μ§μκ³ , ν
μ€νΈλ‘λ§ μμ±ν΄.
|
141 |
-
- PPT λ΄μ©μ μΆκ°μ μΈ μ€λͺ
μ΄λ μ¬λ‘λ₯Ό λ§λΆμ¬μ€.
|
142 |
-
---
|
143 |
-
"""
|
144 |
-
|
145 |
-
input_prompt = header + text
|
146 |
-
|
147 |
-
messages = [
|
148 |
-
{"role": "system", "content": "You are a kind helpful PPT Assistant."},
|
149 |
-
]
|
150 |
-
|
151 |
-
message = input_prompt
|
152 |
-
|
153 |
-
if message:
|
154 |
-
messages.append(
|
155 |
-
{"role": "user", "content": message},
|
156 |
-
)
|
157 |
-
chat = openai.ChatCompletion.create(
|
158 |
-
model="gpt-3.5-turbo-0301", messages=messages
|
159 |
-
)
|
160 |
-
|
161 |
-
reply = chat.choices[0].message.content
|
162 |
-
messages.append({"role": "assistant", "content": reply})
|
163 |
-
|
164 |
-
return reply
|
165 |
-
|
166 |
-
|
167 |
-
####### νλ©΄ μμ ########
|
168 |
tab1, tab2, tab3 = st.tabs(['PA!λ?', 'Text2PPT', 'PPT2Script'])
|
169 |
|
170 |
with tab1:
|
@@ -238,7 +84,7 @@ with tab2:
|
|
238 |
|
239 |
if input_text_check == True:
|
240 |
with st.spinner('Wait for it...'):
|
241 |
-
text2ppt(gpt_token, generate_text2ppt_input_prompt(status, input_text, page_choice), thema_select)
|
242 |
prs = Presentation("text2ppt_output.pptx")
|
243 |
binary_output = BytesIO()
|
244 |
prs.save(binary_output)
|
@@ -271,7 +117,7 @@ with tab3:
|
|
271 |
with open(uploaded_file.name, mode='wb') as w:
|
272 |
w.write(uploaded_file.getvalue())
|
273 |
|
274 |
-
script = ppt2script(gpt_token, uploaded_file.name, choose)
|
275 |
|
276 |
st.success('Done!')
|
277 |
st.download_button('Download Script',
|
|
|
|
|
1 |
import os
|
2 |
+
import env_set
|
3 |
env_set.env_set()
|
4 |
|
5 |
import streamlit as st
|
|
|
|
|
|
|
6 |
import PyPDF2
|
|
|
7 |
import openai
|
8 |
import subprocess
|
9 |
from io import BytesIO
|
10 |
+
from pptx import Presentation
|
11 |
|
12 |
+
import presentation_assistant as pa
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
tab1, tab2, tab3 = st.tabs(['PA!λ?', 'Text2PPT', 'PPT2Script'])
|
15 |
|
16 |
with tab1:
|
|
|
84 |
|
85 |
if input_text_check == True:
|
86 |
with st.spinner('Wait for it...'):
|
87 |
+
pa.text2ppt(gpt_token, generate_text2ppt_input_prompt(status, input_text, page_choice), thema_select)
|
88 |
prs = Presentation("text2ppt_output.pptx")
|
89 |
binary_output = BytesIO()
|
90 |
prs.save(binary_output)
|
|
|
117 |
with open(uploaded_file.name, mode='wb') as w:
|
118 |
w.write(uploaded_file.getvalue())
|
119 |
|
120 |
+
script = pa.ppt2script(gpt_token, uploaded_file.name, choose)
|
121 |
|
122 |
st.success('Done!')
|
123 |
st.download_button('Download Script',
|