Hyeonseo commited on
Commit
de2125a
ยท
1 Parent(s): 4efe295

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -159
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',