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',