Hyeonseo commited on
Commit
4efe295
β€’
1 Parent(s): 80693e6

Create presentation_assistant.py

Browse files
Files changed (1) hide show
  1. presentation_assistant.py +156 -0
presentation_assistant.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import PyPDF2
3
+ from pptx import Presentation
4
+ import openai
5
+ import subprocess
6
+ from io import BytesIO
7
+
8
+ # text2ppt ν”„λ‘¬ν”„νŠΈ 생성 ν•¨μˆ˜
9
+ def generate_text2ppt_input_prompt(input_type, input_value, input_pages):
10
+ header = """
11
+ λ„ˆκ°€ markdown λ¬Έλ²•μœΌλ‘œ PPTλ₯Ό μ œμž‘ν•˜λŠ” λ””μžμ΄λ„ˆλΌκ³  κ°€μ •ν•˜κ³ , %sμž₯의 PPTλ₯Ό μž‘μ„±ν•΄.
12
+ +++ μ•„λž˜ λ‚΄μš© λ˜λŠ” 링크λ₯Ό μš”μ•½ν•΄μ„œ markdown μ–Έμ–΄λ‘œ μž‘μ„±ν•˜λŠ”λ°, === μ•„λž˜ κ·œμΉ™κ³Ό 지킀고, ~~~ μ•„λž˜ μŠ¬λΌμ΄λ“œ μ˜ˆμ‹œλ₯Ό μ°Έκ³ ν•΄.
13
+ +++
14
+ """ % input_pages
15
+
16
+ summary_value = ""
17
+
18
+ if input_type == "링크":
19
+ summary_value += input_value
20
+ summary_value += "ν…μŠ€νŠΈ"
21
+ elif input_type == "text":
22
+ summary_value += input_value
23
+ summary_value += "\n"
24
+ elif input_type == "PDF":
25
+ with open(input_value, 'rb') as pdf_file:
26
+ pdf_reader = PyPDF2.PdfReader(pdf_file)
27
+ num_pages = len(pdf_reader.pages)
28
+
29
+ # 각 νŽ˜μ΄μ§€μ˜ λ‚΄μš©μ„ λ¬Έμžμ—΄λ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€.
30
+ text = ""
31
+ for page_num in range(num_pages):
32
+ page = pdf_reader.pages[page_num]
33
+ text += page.extract_text()
34
+ summary_value += text
35
+ summary_value += "\n"
36
+ else:
37
+ print("ERROR: 잘λͺ»λœ μž…λ ₯")
38
+
39
+ rule_value = """
40
+ ===
41
+ - μ œμ‹œν•œ λ‚΄μš© λ˜λŠ” 링크의 λ‚΄μš©μ— λŒ€ν•΄μ„œλ§Œ μ‚¬μ‹€μ μœΌλ‘œ μž‘μ„±ν•΄μ€˜.
42
+ - μŠ¬λΌμ΄λ“œ κ΅¬λΆ„μžλ‘œ ---λ₯Ό 무쑰건 μ‚¬μš©ν•΄μ€˜.
43
+ - μ£Όμ œμ— μ μ ˆν•œ λ„ν˜•, 이미지(![이미지](이미지링크), https://unsplash.com/ko/images/stock/non-copyrighted μ—μ„œ μ‹€μ œλ‘œ μ‚¬μš© κ°€λŠ₯ν•œ), ν‘œ(|-|), 인용(>), κ°•μ‘°(bold, ``), 이λͺ¨μ§€(https://kr.piliapp.com/twitter-symbols/), μ•„μ΄μ½˜ (https://kr.piliapp.com/symbol/#popular) 등이 λ‹€μ–‘ν•˜κ²Œ μŠ¬λΌμ΄λ“œλ₯Ό λ””μžμΈν•˜κ³  λ°°μΉ˜ν•΄μ€˜.
44
+ - 이λͺ¨μ§€λŠ” μ΅œλŒ€ 2 νŽ˜μ΄μ§€μ— ν•œ 번만 μ‚¬μš©ν•˜κ³ , λ‹€λ₯Έ λ””μžμΈμ„ λ‹€μ–‘ν•˜κ²Œ μ‚¬μš©ν•΄μ€˜.
45
+ - 이미지와 ν‘œλ₯Ό μ‚¬μš©ν•  λ•Œ, νŽ˜μ΄μ§€ 크기와 κ³ λ €ν•΄μ„œ κΈ€ λ‚΄μš©μ΄ λͺ¨λ‘ λ‚˜νƒ€λ‚˜λ„λ‘ 크기λ₯Ό μ§€μ •ν•΄μ€˜.
46
+ - Slide 1λ₯Ό 제λͺ©μœΌλ‘œ ν•΄μ„œ 총 %sμž₯이야.
47
+ - PPT의 λ‚΄μš©μ„ ν’λΆ€ν•˜κ²Œ λ§ˆν¬λ‹€μš΄μœΌλ‘œ μž‘μ„±ν•΄μ€˜.
48
+ - μŠ¬λΌμ΄λ“œ λ³„λ‘œ μ„€λͺ…ν•˜μ§€λ§κ³ , μ½”λ“œλ§Œ μž‘μ„±ν•΄μ€˜.
49
+ - μ˜ˆμ‹œμ˜ λ‚΄μš©μ„ μ‚¬μš©ν•΄μ„œ μž‘μ„±ν•˜μ§€λ§κ³ , ν˜•μ‹λ§Œ μ°Έκ³ ν•΄.
50
+ ~~~
51
+ <!-- Slide 0. μŠ¬λΌμ΄λ“œ 주제 -->
52
+ # μŠ¬λΌμ΄λ“œ 제λͺ©
53
+ ![이미지링크](https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo-with-title.png)
54
+ - ChatGPTλ₯Ό ν™œμš©ν•œ πŸ€—**TEXT2PPT μ„œλΉ„μŠ€ PA!**μž…λ‹ˆλ‹€.
55
+ - `링크`,`ν…μŠ€νŠΈ`, `PDF`λ₯Ό μž…λ ₯ λ˜λŠ” μ—…λ‘œλ“œν•˜λ©΄, PPT둜 λ³€ν™˜ν•©λ‹ˆλ‹€.
56
+ """ % input_pages
57
+
58
+ return header + summary_value + rule_value
59
+
60
+
61
+ # text2ppt μ‹€ν–‰ ν•¨μˆ˜
62
+ def text2ppt(token_key, input_prompt, input_theme):
63
+ openai.api_key = token_key
64
+
65
+ messages = [
66
+ {"role": "system", "content": "You are a kind helpful PPT designer."},
67
+ ]
68
+
69
+ message = input_prompt
70
+
71
+ if message:
72
+ messages.append(
73
+ {"role": "user", "content": message},
74
+ )
75
+ chat = openai.ChatCompletion.create(
76
+ model="gpt-3.5-turbo-0301", messages=messages
77
+ )
78
+
79
+ reply = chat.choices[0].message.content
80
+ messages.append({"role": "assistant", "content": reply})
81
+
82
+ md_text = reply[4:] if reply[:3] == "---" else reply
83
+ md_text_list = md_text.split('\n')
84
+
85
+ f = open("text2ppt_input.md", 'w')
86
+ for i in range(0, len(md_text_list)):
87
+ data = md_text_list[i] + "\n"
88
+ f.write(data)
89
+ f.close()
90
+
91
+ if input_theme == 'default':
92
+ subprocess.run(["./pandoc-2.14.2/bin/pandoc", "text2ppt_input.md", "-t", "pptx", "-o", "text2ppt_output.pptx"], capture_output=True)
93
+ else:
94
+ ppt_theme = "--reference-doc="+input_theme+".pptx"
95
+ subprocess.run(["./pandoc-2.14.2/bin/pandoc", "text2ppt_input.md", "-t", "pptx", ppt_theme, "-o", "text2ppt_output.pptx"], capture_output=True)
96
+
97
+
98
+ def ppt2script(token_key, input_file, input_type):
99
+ openai.api_key = token_key
100
+
101
+ if input_type=="PDF":
102
+ with open(input_file, 'rb') as pdf_file:
103
+ pdf_reader = PyPDF2.PdfReader(pdf_file)
104
+ num_pages = len(pdf_reader.pages)
105
+
106
+ # 각 νŽ˜μ΄μ§€μ˜ λ‚΄μš©μ„ λ¬Έμžμ—΄λ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€.
107
+ text = ""
108
+ for page_num in range(num_pages):
109
+ page = pdf_reader.pages[page_num]
110
+ text += "[PAGE_NUM " + str(page_num + 1) + "]"
111
+ text += page.extract_text()
112
+ else:
113
+ prs = Presentation(input_file)
114
+
115
+ text = ""
116
+ page_num = 0
117
+ for slide in prs.slides:
118
+ text += "[PAGE_NUM " + str(page_num + 1) + "]"
119
+ page_num += 1
120
+ for shape in slide.shapes:
121
+ if not shape.has_text_frame:
122
+ continue
123
+ for paragraph in shape.text_frame.paragraphs:
124
+ for run in paragraph.runs:
125
+ text += run.text
126
+
127
+ header = """
128
+ λ„ˆλŠ”λŠ” PPT λ°œν‘œμ— 도움을 μ£ΌλŠ” μ‘°λ ₯μžμ•Ό.
129
+ ~~~μ•„λž˜ κ·œμΉ™μ„ 지킀고, --- μ•„λž˜ PPT λ‚΄μš©μ— λŒ€ν•΄ λ°œν‘œ 슀크립트λ₯Ό ν•œκΈ€λ‘œ μž‘μ„±ν•΄.
130
+ ~~~
131
+ - [PAGE_NUM 1] 일 λ•Œ, μ—¬κΈ°μ„œ 1은 νŽ˜μ΄μ§€ 번호인데, νŽ˜μ΄μ§€ λ²ˆν˜Έλ§ˆλ‹€ λ°œν‘œ 슀크립트λ₯Ό μž‘μ„±ν•΄.
132
+ - λ§ˆν¬λ‹€μš΄ μ–Έμ–΄λ₯Ό μ“°μ§€μ•Šκ³ , ν…μŠ€νŠΈλ‘œλ§Œ μž‘μ„±ν•΄.
133
+ - PPT λ‚΄μš©μ— 좔가적인 μ„€λͺ…μ΄λ‚˜ 사둀λ₯Ό λ§λΆ™μ—¬μ€˜.
134
+ ---
135
+ """
136
+
137
+ input_prompt = header + text
138
+
139
+ messages = [
140
+ {"role": "system", "content": "You are a kind helpful PPT Assistant."},
141
+ ]
142
+
143
+ message = input_prompt
144
+
145
+ if message:
146
+ messages.append(
147
+ {"role": "user", "content": message},
148
+ )
149
+ chat = openai.ChatCompletion.create(
150
+ model="gpt-3.5-turbo-0301", messages=messages
151
+ )
152
+
153
+ reply = chat.choices[0].message.content
154
+ messages.append({"role": "assistant", "content": reply})
155
+
156
+ return reply