Spaces:
Runtime error
Runtime error
edit UI for small device & refactor major func
Browse files- app.py +138 -164
- gpt_based_function.py +112 -0
- text_annotator.py +45 -0
app.py
CHANGED
@@ -1,12 +1,38 @@
|
|
1 |
from annotated_text import annotated_text
|
|
|
2 |
import streamlit as st
|
3 |
import openai
|
4 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
# OpenAI API ์ค์ (ํ๊ฒฝ ๋ณ์์์ ์ฝ์ด์ด)
|
7 |
openai.api_key = os.getenv("OPENAI_API_KEY") # ์ค์ ์ฝ๋์์ ์ฃผ์ ํด์
|
8 |
st.set_page_config(layout="wide")
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
## ํฌ์ธํธ ๋ถ๋ถ ์ถ๊ฐ
|
12 |
if 'points' not in st.session_state:
|
@@ -37,148 +63,117 @@ def display_markdown_table():
|
|
37 |
##๊ธฐ๋ฅ์ ํจ์๋ก ๋๋์ด์ ๊ตฌํ
|
38 |
col, _ = st.columns(2)
|
39 |
|
40 |
-
def display_passage(
|
41 |
-
st.header("์ง๋ฌธ")
|
42 |
global text_container
|
43 |
text_container = st.container() # ์ถ๊ฐ๋ ์ฝ๋
|
44 |
# ๋ฏธ๋ฆฌ ๊ณต๊ฐ ํ๋ณด
|
45 |
global text_placeholder
|
46 |
text_placeholder = text_container.empty()
|
47 |
-
global user_text
|
48 |
-
user_text = """๋ฏผ์ฃผ์ฃผ์ ์ฌํ๋ ๊ตญ๋ฏผ์ด ์ ์น์ ์ฐธ์ฌํ ๊ถ๋ฆฌ๋ฅผ ๋ณด์ฅํ๋ค. ๊ทธ๋ฌํ ๊ถ๋ฆฌ๋ฅผ ์ฐธ์ ๊ถ์ด๋ผ ํ๋๋ฐ, ์ด๋ ๊ธฐ๋ณธ์ ์ผ๋ก โ์ ๊ฑฐโ๋ก ์คํ๋๋ค. ์ ๊ฑฐ๋ ์ฌํ ์ง๋จ์ ๋ํ์๋ ๊ณต์ง์๋ฅผ ์ ์ถํ์ฌ ๊ทธ๋ค์๊ฒ ๋ํ์ฑ์ ๋ถ์ฌํ๋ ํ์์ด๋ค. ๊ทธ๋ฌ๋ฏ๋ก ๋์ ํฌํ์จ์ ๋ฏผ์ฃผ์ฃผ์์ ์ ๋น์ฑ ํ๋ณด์ ๊น์ ๊ด๋ จ์ด ์๋ค.<br><br>
|
49 |
-
์ ๊ฑฐ ํฌํ ์ ๋์๋ ํฌํ๊ถ ํ์ฌ๋ฅผ ํฌํ์์ ์์ ์์ฌ์ ๋งก๊ธฐ๋ โ์์ ํฌํ์ โ์ ํฌํ๊ถ ํ์ฌ๋ฅผ ๊ตญ๋ฏผ์ ์๋ฌด๋ก ๊ฐ์ฃผํ๊ณ ์ ๋นํ ์ฌ์ ์์ด ๊ธฐ๊ถํ๋ฉด ๋ฒ์ ์ ์ฌ๋ฅผ ๊ฐํ๋ โ์๋ฌด ํฌํ์ โ๊ฐ ์๋ค. ์ฐ๋ฆฌ๋๋ผ๋ ์์ ํฌํ์ ๋ฅผ ์ฑํํ๊ณ ์๋๋ฐ, ์ต๊ทผ ์น๋ฅธ ์ ๊ฑฐ์ ํ๊ท ํฌํ์จ์ด 50ํผ์ผํธ๋๋ก ๋ํ๋ฌ๋ค. ๊ฒฝ์ ๊ฐ๋ฐ ํ๋ ฅ ๊ธฐ๊ตฌ(OECD) ํ์๊ตญ ํ๊ท ์ด 70ํผ์ผํธ๋์ธ ๊ฒ์ ์๊ฐํ๋ฉด ๋งค์ฐ ๋ฎ์ ์์น๋ผ ํ ์ ์๋ค. ์ด๋ฌํ ์๏ฟฝ๏ฟฝ๏ฟฝ์ด ์ง์๋์ ์๋ฌด ํฌํ์ ๋ฅผ ๋์
ํด์ผ ํ๋ค๋ ์๊ฒฌ์ด ์ ์๋์๊ณ , ์์ ํฌํ์ ๊ฐ ๋ฏผ์ฃผ์ฃผ์์ ์์น์ ๋ง์ผ๋ฏ๋ก ์ด๋ฅผ ์ ์งํด์ผ ํ๋ค๋ ์๊ฒฌ๊ณผ ๋๋ฆฝํ๊ณ ์๋ค.<br><br>
|
50 |
-
์๋ฌด ํฌํ์ ๋ฅผ ๋์
ํ์๋ ์ธก์ ๋ฎ์ ํฌํ์จ๋ก ํฌํ ๊ฒฐ๊ณผ์ ์ ๋น์ฑ์ ํ๋ณดํ์ง ๋ชปํ๋ ๋ฌธ์ ๊ฐ ๋งค์ฐ ์ฌ๊ฐํ๋ค๊ณ ์ฃผ์ฅํ๋ค. ๋ ์๋ฌด ํฌํ์ ์ ๊ฐ์ ์ฑ๊ณผ ๋ฒ์ ์ ์ฌ๊ฐ ํฌํ์จ์ ๋์ด๋ฏ๋ก ํฌํ์จ์ด ๋ฎ์์ ๋ฐ์ํ๋ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์๋ค๊ณ ๋ณธ๋ค. ๊ทธ๋ฆฌ๊ณ ๊ตญ๋ฏผ ๋๋ถ๋ถ์ด ํฌํ์ ์ฐธ์ฌํ๊ฒ ๋๋ฉด ์ ์น์ธ๋ค์ด ๋ชจ๋ ๊ณ์ธต์ ์ง์ง๋ฅผ ๋ฐ๊ธฐ ์ํด ์ ์ฑ
๊ฒฝ์๋ ฅ์ ๋์ด๋ ค ํ ๊ฒ์ด๋ฏ๋ก ์ ์น ์์ธ ๊ณ์ธต์ ๋์ฑ ๊ด์ฌ์ ์๋ ํจ๊ณผ๊ฐ ์์ ๊ฒ์ด๋ผ๊ณ ์ด์ผ๊ธฐํ๋ค.<br><br>
|
51 |
-
๋ฐ๋ฉด ์๋ฌด ํฌํ์ ์ ๋ฐ๋ํ๋ ์ธก์ ํ์ฌ ์ฐ๋ฆฌ๋๋ผ์ ํฌํ์จ์ด ์ ์น ์ง๋์๋ค์ ๋ํ์ฑ์ ํผ์ํ ๋งํผ ์ฌ๊ฐํ ์ํฉ์ ์๋๋ผ๊ณ ์ฃผ์ฅํ๋ค. ๋ ํฌํ์จ์ ๋์ด๋ ๊ฒ๋ณด๋ค ๊ตญ๋ฏผ์ ์ ๋ขฐ๋ฅผ ํ๋ณตํ๋ ๊ฒ์ด ๋ ์ค์ํ๊ณ , ์๋ฏผ ๊ต์ก์ด๋ ๋ชจ์ ํฌํ ๊ต์ก ํ๋ก๊ทธ๋จ์ผ๋ก๋ ํฌํ์จ ์์น์ ๊ธฐ๋ํ ์ ์๋ค๋ฉฐ ์๋ฌด ํฌํ์ ์ ๋์
๋ง์ด ํฌํ์จ์ด๋ ์ ์น์ ๊ด์ฌ์ ๋์ด๋ ํด๊ฒฐ ๋ฐฉ์์ ์๋๋ผ๊ณ ์ด์ผ๊ธฐํ๋ค. ๊ทธ๋ฆฌ๊ณ ์๋ฌด ํฌํ์ ๋ฅผ ๋์
ํ๋ฉด, ์ ์ถ๋ ์ ์น์ธ๋ค์ด ๋์ ํฌํ์จ์ ํ๊ณ๋ก ์ํ๋ฌด์ธ์ ํ๋๋ฅผ ๊ฐ๋ ๋ถ์์ฉ์ด ์๊ธด๋ค๋ ๊ฐ ํ๋ณด์๋ฅผ ์ ๋ชจ๋ฅด๋ ์ํ์์ ํฌํํ๋ ์ผ์ด ๋ฐ์ํ์ฌ ๊ตญ๋ฏผ์ ๋ป์ด ์คํ๋ ค ์๊ณก๋ ์ ์๋ค๋ฉฐ ์ฐ๋ ค์ ๋ชฉ์๋ฆฌ๋ฅผ ๋ด๊ณ ์๋ค.<br><br>
|
52 |
-
"""
|
53 |
text_placeholder.write(user_text, unsafe_allow_html=True)
|
54 |
#์ ์ธํ ๋ณ์ ๋ค๋ฅธ ํจ์์์ ์ฌ์ฉ๊ฐ๋ฅํ๊ฒ ํ๊ธฐ ์ํด return
|
55 |
return text_placeholder, text_container, user_text
|
56 |
|
|
|
|
|
57 |
|
58 |
-
|
59 |
-
st.header("์์ฝ ๊ฒฐ๊ณผ")
|
60 |
-
global user_summary
|
61 |
-
user_summary = st.text_area("์์ฝ๊ฒฐ๊ณผ๋ฅผ ์ ์ถํ์ธ์.")
|
62 |
-
|
63 |
cols = st.columns(2)
|
64 |
with cols[0]:
|
65 |
-
|
66 |
-
if
|
67 |
-
|
68 |
-
st.
|
69 |
-
pass
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
model="gpt-3.5-turbo-16k",
|
87 |
-
messages=messages,
|
88 |
-
temperature=0.1,
|
89 |
-
max_tokens=2500
|
90 |
-
)
|
91 |
-
|
92 |
-
explanation = response['choices'][0]['message']['content']
|
93 |
-
st.write(f"์ฑ์ ํ๊ธฐ: {explanation}")
|
94 |
-
pass
|
95 |
else:
|
96 |
if st.session_state.points == 0:
|
97 |
#ํฌ์ธํธ๊ฐ ์์ด์ ์ฌ์ฉํ ์ ์๋ค๋ ๋ฌธ๊ตฌ ์ถ๊ฐ
|
98 |
-
st.write("ํฌ์ธํธ๊ฐ ์์ด์
|
99 |
pass
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
# ๋ฒํผ row
|
107 |
-
cols = st.columns(4)
|
108 |
-
with cols[0]:
|
109 |
-
btn_keyword = st.button("ํค์๋ ์ฐพ๊ธฐ")
|
110 |
-
if btn_keyword and st.session_state.points > 0: # ํฌ์ธํธ ์ฒดํฌ
|
111 |
update_points(-1) # ํฌ์ธํธ ๊ฐ์
|
112 |
-
st.session_state.history.append(["
|
113 |
-
|
114 |
-
highlighted_text = """**๋ฏผ์ฃผ์ฃผ์ ์ฌํ**๋ ๊ตญ๋ฏผ์ด ์ ์น์ ์ฐธ์ฌํ ๊ถ๋ฆฌ๋ฅผ ๋ณด์ฅํ๋ค. ๊ทธ๋ฌํ ๊ถ๋ฆฌ๋ฅผ **์ฐธ์ ๊ถ**์ด๋ผ ํ๋๋ฐ, ์ด๋ ๊ธฐ๋ณธ์ ์ผ๋ก โ์ ๊ฑฐโ๋ก ์คํ๋๋ค. ์ ๊ฑฐ๋ ์ฌํ ์ง๋จ์ ๋ํ์๋ ๊ณต์ง์๋ฅผ ์ ์ถํ์ฌ ๊ทธ๋ค์๊ฒ ๋ํ์ฑ์ ๋ถ์ฌํ๋ ํ์์ด๋ค. ๊ทธ๋ฌ๋ฏ๋ก **๋์ ํฌํ์จ**์ ๋ฏผ์ฃผ์ฃผ์์ **์ ๋น์ฑ ํ๋ณด**์ ๊น์ ๊ด๋ จ์ด ์๋ค.
|
115 |
|
116 |
-
|
117 |
|
118 |
-
|
|
|
|
|
|
|
119 |
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
#
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
|
|
|
|
|
|
|
|
|
|
131 |
else:
|
132 |
if st.session_state.points == 0:
|
133 |
-
|
134 |
-
st.write("ํฌ์ธํธ๊ฐ ์์ด์
|
135 |
pass
|
136 |
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
|
|
|
|
143 |
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
st.session_state.history.append(["์ฌ์ด ํํ", -1, st.session_state.points])
|
150 |
-
|
151 |
-
|
152 |
-
with cols[3]:
|
153 |
-
global btn_rewrite
|
154 |
-
btn_rewrite = st.button("๋ค์ ์ฐ๊ธฐ")
|
155 |
-
if btn_rewrite and st.session_state.points > 0: # ํฌ์ธํธ ์ฒดํฌ
|
156 |
-
update_points(-1) # ํฌ์ธํธ ๊ฐ์
|
157 |
-
st.session_state.history.append(["๋ค์ ์ฐ๊ธฐ", -1, st.session_state.points])
|
158 |
-
|
159 |
-
|
160 |
-
return btn_keyword, btn_explanation, btn_simple, btn_rewrite
|
161 |
|
162 |
|
163 |
-
def
|
164 |
with st.container():
|
165 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
166 |
if btn_explanation and st.session_state.points > 0:
|
167 |
-
|
168 |
-
|
169 |
-
messages = [
|
170 |
-
{"role": "system", "content": "You are a helpful assistant that explains complex topics in a way that an elementary school student can understand. use only korean"},
|
171 |
-
{"role": "user", "content": explanation_task}
|
172 |
-
]
|
173 |
-
|
174 |
-
response = openai.ChatCompletion.create(
|
175 |
-
model="gpt-3.5-turbo-16k",
|
176 |
-
messages=messages,
|
177 |
-
temperature=0.1,
|
178 |
-
max_tokens=200
|
179 |
-
)
|
180 |
-
|
181 |
-
explanation = response['choices'][0]['message']['content']
|
182 |
|
183 |
# ์ถ๊ฐ ์ค๋ช
|
184 |
st.write(f"์ถ๊ฐ ์ค๋ช
: {explanation}")
|
@@ -190,70 +185,49 @@ def display_output():
|
|
190 |
|
191 |
pass
|
192 |
|
193 |
-
if
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
{"role": "user", "content": explanation_task}
|
199 |
-
]
|
200 |
-
|
201 |
-
response = openai.ChatCompletion.create(
|
202 |
-
model="gpt-3.5-turbo-16k",
|
203 |
-
messages=messages,
|
204 |
-
temperature=0.1,
|
205 |
-
max_tokens=2500
|
206 |
-
)
|
207 |
-
|
208 |
-
explanation = response['choices'][0]['message']['content']
|
209 |
-
|
210 |
-
# ์ฌ์ด ํํ์ผ๋ก ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
211 |
-
st.write(f"์ฌ์ด ๊ธ: {explanation}")
|
212 |
pass
|
213 |
else:
|
214 |
if st.session_state.points == 0:
|
215 |
-
st.write("ํฌ์ธํธ๊ฐ ์์ด์ ์ฌ์ด
|
216 |
pass
|
217 |
|
218 |
-
if btn_rewrite and st.session_state.points > 0:
|
219 |
-
explanation_task = f"Rewrite the contents of '{user_input}' so that it will pass the writing test."
|
220 |
-
|
221 |
-
messages = [
|
222 |
-
{"role": "system", "content": "You are a helpful assistant. use only korean"},
|
223 |
-
{"role": "user", "content": explanation_task}
|
224 |
-
]
|
225 |
-
|
226 |
-
response = openai.ChatCompletion.create(
|
227 |
-
model="gpt-3.5-turbo-16k",
|
228 |
-
messages=messages,
|
229 |
-
temperature=0.1,
|
230 |
-
max_tokens=2500
|
231 |
-
)
|
232 |
-
|
233 |
-
explanation = response['choices'][0]['message']['content']
|
234 |
-
|
235 |
-
st.write(f"๋ค์ ์ฐ๊ธฐ: {explanation}")
|
236 |
-
pass
|
237 |
-
else:
|
238 |
-
if st.session_state.points == 0:
|
239 |
-
st.write("ํฌ์ธํธ๊ฐ ์์ด์ ๋ค์ ์ฐ๊ธฐ ๊ธฐ๋ฅ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.")
|
240 |
|
241 |
-
pass
|
242 |
-
|
243 |
|
244 |
def main():
|
245 |
-
st.title("
|
246 |
#ํฌ์ธํธ ์์คํ
์ค๋ช
์ถ๊ฐ
|
247 |
-
st.sidebar.markdown("ํฌ์ธํธ๋ 3์ ๋ถํฐ ์์ํด์,
|
|
|
|
|
|
|
248 |
|
249 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
250 |
|
251 |
-
with col1:
|
252 |
-
display_passage(col1)
|
253 |
-
display_summary(col1)
|
254 |
-
with col2:
|
255 |
-
btn_keyword, btn_explanation, btn_simple, btn_rewrite = display_input_btns(col2)
|
256 |
-
display_output()
|
257 |
#ํฌ์ธํธ ํ์
|
258 |
display_points()
|
259 |
#ํฌ์ธํธ ๋ณํ ํ์
|
|
|
1 |
from annotated_text import annotated_text
|
2 |
+
import ast
|
3 |
import streamlit as st
|
4 |
import openai
|
5 |
import os
|
6 |
+
import sys
|
7 |
+
from gpt_based_function import gpt_keyword_highlighter, gpt_text_naturalizer, gpt_explanation_generator, gpt_easier_text_generator, gpt_rater
|
8 |
+
import concurrent.futures
|
9 |
+
from tqdm import tqdm
|
10 |
+
|
11 |
+
|
12 |
+
# current_path = '/Users/kintch/PycharmProjects/jungu_sgi/summary_with_gpt'
|
13 |
+
# os.chdir(current_path)
|
14 |
+
# sys.path.append(current_path)
|
15 |
|
16 |
# OpenAI API ์ค์ (ํ๊ฒฝ ๋ณ์์์ ์ฝ์ด์ด)
|
17 |
openai.api_key = os.getenv("OPENAI_API_KEY") # ์ค์ ์ฝ๋์์ ์ฃผ์ ํด์
|
18 |
st.set_page_config(layout="wide")
|
19 |
|
20 |
+
user_text = """๋ฏผ์ฃผ์ฃผ์ ์ฌํ๋ ๊ตญ๋ฏผ์ด ์ ์น์ ์ฐธ์ฌํ ๊ถ๋ฆฌ๋ฅผ ๋ณด์ฅํ๋ค. ๊ทธ๋ฌํ ๊ถ๋ฆฌ๋ฅผ ์ฐธ์ ๊ถ์ด๋ผ ํ๋๋ฐ, ์ด๋ ๊ธฐ๋ณธ์ ์ผ๋ก โ์ ๊ฑฐโ๋ก ์คํ๋๋ค. ์ ๊ฑฐ๋ ์ฌํ ์ง๋จ์ ๋ํ์๋ ๊ณต์ง์๋ฅผ ์ ์ถํ์ฌ ๊ทธ๋ค์๊ฒ ๋ํ์ฑ์ ๋ถ์ฌํ๋ ํ์์ด๋ค. ๊ทธ๋ฌ๋ฏ๋ก ๋์ ํฌํ์จ์ ๋ฏผ์ฃผ์ฃผ์์ ์ ๋น์ฑ ํ๋ณด์ ๊น์ ๊ด๋ จ์ด ์๋ค.
|
21 |
+
์ ๊ฑฐ ํฌํ ์ ๋์๋ ํฌํ๊ถ ํ์ฌ๋ฅผ ํฌํ์์ ์์ ์์ฌ์ ๋งก๊ธฐ๋ โ์์ ํฌํ์ โ์ ํฌํ๊ถ ํ์ฌ๋ฅผ ๊ตญ๋ฏผ์ ์๋ฌด๋ก ๊ฐ์ฃผํ๊ณ ์ ๋นํ ์ฌ์ ์์ด ๊ธฐ๊ถํ๋ฉด ๋ฒ์ ์ ์ฌ๋ฅผ ๊ฐํ๋ โ์๋ฌด ํฌํ์ โ๊ฐ ์๋ค. ์ฐ๋ฆฌ๋๋ผ๋ ์์ ํฌํ์ ๋ฅผ ์ฑํํ๊ณ ์๋๋ฐ, ์ต๊ทผ ์น๋ฅธ ์ ๊ฑฐ์ ํ๊ท ํฌํ์จ์ด 50ํผ์ผํธ๋๋ก ๋ํ๋ฌ๋ค. ๊ฒฝ์ ๊ฐ๋ฐ ํ๋ ฅ ๊ธฐ๊ตฌ(OECD) ํ์๊ตญ ํ๊ท ์ด 70ํผ์ผํธ๋์ธ ๊ฒ์ ์๊ฐํ๋ฉด ๋งค์ฐ ๋ฎ์ ์์น๋ผ ํ ์ ์๋ค. ์ด๋ฌํ ์ํฉ์ด ์ง์๋์ ์๋ฌด ํฌํ์ ๋ฅผ ๋์
ํด์ผ ํ๋ค๋ ์๊ฒฌ์ด ์ ์๋์๊ณ , ์์ ํฌํ์ ๊ฐ ๋ฏผ์ฃผ์ฃผ์์ ์์น์ ๋ง์ผ๋ฏ๋ก ์ด๋ฅผ ์ ์งํด์ผ ํ๋ค๋ ์๊ฒฌ๊ณผ ๋๋ฆฝํ๊ณ ์๋ค.
|
22 |
+
์๋ฌด ํฌํ์ ๋ฅผ ๋์
ํ์๋ ์ธก์ ๋ฎ์ ํฌํ์จ๋ก ํฌํ ๊ฒฐ๊ณผ์ ์ ๋น์ฑ์ ํ๋ณดํ์ง ๋ชปํ๋ ๋ฌธ์ ๊ฐ ๋งค์ฐ ์ฌ๊ฐํ๋ค๊ณ ์ฃผ์ฅํ๋ค. ๋ ์๋ฌด ํฌํ์ ์ ๊ฐ์ ์ฑ๊ณผ ๋ฒ์ ์ ์ฌ๊ฐ ํฌํ์จ์ ๋์ด๋ฏ๋ก ํฌํ์จ์ด ๋ฎ์์ ๋ฐ์ํ๋ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์๋ค๊ณ ๋ณธ๋ค. ๊ทธ๋ฆฌ๊ณ ๊ตญ๋ฏผ ๋๋ถ๋ถ์ด ํฌํ์ ์ฐธ์ฌํ๊ฒ ๋๋ฉด ์ ์น์ธ๋ค์ด ๋ชจ๋ ๊ณ์ธต์ ์ง์ง๋ฅผ ๋ฐ๊ธฐ ์ํด ์ ์ฑ
๊ฒฝ์๋ ฅ์ ๋์ด๋ ค ํ ๊ฒ์ด๋ฏ๋ก ์ ์น ์์ธ ๊ณ์ธต์ ๋์ฑ ๊ด์ฌ์ ์๋ ํจ๊ณผ๊ฐ ์์ ๊ฒ์ด๋ผ๊ณ ์ด์ผ๊ธฐํ๋ค.
|
23 |
+
๋ฐ๋ฉด ์๋ฌด ํฌํ์ ์ ๋ฐ๋ํ๋ ์ธก์ ํ์ฌ ์ฐ๋ฆฌ๋๋ผ์ ํฌํ์จ์ด ์ ์น ์ง๋์๋ค์ ๋ํ์ฑ์ ํผ์ํ ๋งํผ ์ฌ๊ฐํ ์ํฉ์ ์๋๋ผ๊ณ ์ฃผ์ฅํ๋ค. ๋ ํฌํ์จ์ ๋์ด๋ ๊ฒ๋ณด๋ค ๊ตญ๋ฏผ์ ์ ๋ขฐ๋ฅผ ํ๋ณตํ๋ ๊ฒ์ด ๋ ์ค์ํ๊ณ , ์๋ฏผ ๊ต์ก์ด๋ ๋ชจ์ ํฌํ ๊ต์ก ํ๋ก๊ทธ๋จ์ผ๋ก๋ ํฌํ์จ ์์น์ ๊ธฐ๋ํ ์ ์๋ค๋ฉฐ ์๋ฌด ํฌํ์ ์ ๋์
๋ง์ด ํฌํ์จ์ด๋ ์ ์น์ ๊ด์ฌ์ ๋์ด๋ ํด๊ฒฐ ๋ฐฉ์์ ์๋๋ผ๊ณ ์ด์ผ๊ธฐํ๋ค. ๊ทธ๋ฆฌ๊ณ ์๋ฌด ํฌํ์ ๋ฅผ ๋์
ํ๋ฉด, ์ ์ถ๋ ์ ์น์ธ๋ค์ด ๋์ ํฌํ์จ์ ํ๊ณ๋ก ์ํ๋ฌด์ธ์ ํ๋๋ฅผ ๊ฐ๋ ๋ถ์์ฉ์ด ์๊ธด๋ค๋ ๊ฐ ํ๋ณด์๋ฅผ ์ ๋ชจ๋ฅด๋ ์ํ์์ ํฌํํ๋ ์ผ์ด ๋ฐ์ํ์ฌ ๊ตญ๋ฏผ์ ๋ป์ด ์คํ๋ ค ์๊ณก๋ ์ ์๋ค๋ฉฐ ์ฐ๋ ค์ ๋ชฉ์๋ฆฌ๋ฅผ ๋ด๊ณ ์๋ค."""
|
24 |
+
|
25 |
+
user_text_paragraphs = user_text.split("\n") #๋ฌธ๋จ๋ณ๋ก ์ชผ๊ฐ๊ธฐ
|
26 |
+
|
27 |
+
is_easier_button_pressed = [False]*len(user_text_paragraphs)
|
28 |
+
|
29 |
+
def run(list_sum):
|
30 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
31 |
+
results = list(tqdm(executor.map(gpt_keyword_highlighter, list_sum), total=len(list_sum)))
|
32 |
+
return results
|
33 |
+
|
34 |
+
# SHOUD BE ํ์ฑํ
|
35 |
+
highlighted_texts_list = run(user_text_paragraphs)
|
36 |
|
37 |
## ํฌ์ธํธ ๋ถ๋ถ ์ถ๊ฐ
|
38 |
if 'points' not in st.session_state:
|
|
|
63 |
##๊ธฐ๋ฅ์ ํจ์๋ก ๋๋์ด์ ๊ตฌํ
|
64 |
col, _ = st.columns(2)
|
65 |
|
66 |
+
def display_passage(user_text):
|
67 |
+
# st.header("์ง๋ฌธ")
|
68 |
global text_container
|
69 |
text_container = st.container() # ์ถ๊ฐ๋ ์ฝ๋
|
70 |
# ๋ฏธ๋ฆฌ ๊ณต๊ฐ ํ๋ณด
|
71 |
global text_placeholder
|
72 |
text_placeholder = text_container.empty()
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
text_placeholder.write(user_text, unsafe_allow_html=True)
|
74 |
#์ ์ธํ ๋ณ์ ๋ค๋ฅธ ํจ์์์ ์ฌ์ฉ๊ฐ๋ฅํ๊ฒ ํ๊ธฐ ์ํด return
|
75 |
return text_placeholder, text_container, user_text
|
76 |
|
77 |
+
def display_preset_func_btns(col):
|
78 |
+
# st.header("์ธ๊ณต์ง๋ฅ ์ฌ์ฉํ๊ธฐ")
|
79 |
|
80 |
+
# ๋ฒํผ row
|
|
|
|
|
|
|
|
|
81 |
cols = st.columns(2)
|
82 |
with cols[0]:
|
83 |
+
btn_gpt_keyword_func = st.button("ํค์๋๋ฅผ ์ฐพ์์ค")
|
84 |
+
if btn_gpt_keyword_func and st.session_state.points > 0: # ํฌ์ธํธ ์ฒดํฌ
|
85 |
+
update_points(-1) # ํฌ์ธํธ ๊ฐ์
|
86 |
+
st.session_state.history.append(["ํค์๋๋ฅผ ์ฐพ์์ค", -1, st.session_state.points])
|
|
|
87 |
|
88 |
+
global is_easier_button_pressed
|
89 |
+
is_easier_button_pressed = [True]*len(user_text_paragraphs)
|
90 |
+
|
91 |
+
# ๋ค๋ฅธ ๋ก์ง ๊ณ ๋ ค
|
92 |
+
#๋ค๋ฅธ ์ง๋ฌธ ๋ฃ๊ธฐ
|
93 |
+
# global highlighted_texts_list
|
94 |
+
# highlighted_text = highlighted_texts_list[-1]
|
95 |
+
#
|
96 |
+
# # ๊ธฐ์กด ์ง๋ฌธ ์ง์ฐ๊ธฐ
|
97 |
+
# text_placeholder.empty()
|
98 |
+
#
|
99 |
+
# #text_container์ highlight๋ ํ
์คํธ๋ฅผ ์ถ๋ ฅ
|
100 |
+
# with text_container:
|
101 |
+
# exec(highlighted_text)
|
102 |
+
# st.markdown(highlighted_text, unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
else:
|
104 |
if st.session_state.points == 0:
|
105 |
#ํฌ์ธํธ๊ฐ ์์ด์ ์ฌ์ฉํ ์ ์๋ค๋ ๋ฌธ๊ตฌ ์ถ๊ฐ
|
106 |
+
st.write("ํฌ์ธํธ๊ฐ ์์ด์ ํค์๋๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.")
|
107 |
pass
|
108 |
|
109 |
+
with cols[1]:
|
110 |
+
global btn_gpt_easier_text_func
|
111 |
+
btn_gpt_easier_text_func = st.button("์ฌ์ด ํํ์ผ๋ก ๋ฐ๊ฟ์ค")
|
112 |
+
if btn_gpt_easier_text_func and st.session_state.points > 0: # ํฌ์ธํธ ์ฒดํฌ
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
update_points(-1) # ํฌ์ธํธ ๊ฐ์
|
114 |
+
st.session_state.history.append(["์ฌ์ด ํํ์ผ๋ก ๋ฐ๊ฟ์ค", -1, st.session_state.points])
|
115 |
+
|
|
|
116 |
|
117 |
+
# return btn_gpt_keyword_func, btn_gpt_easier_text_func
|
118 |
|
119 |
+
def display_users_summarization(col):
|
120 |
+
# st.header("์์ฝ ๊ฒฐ๊ณผ")
|
121 |
+
global user_summary
|
122 |
+
user_summary = st.text_area("ํ ๋ฌธ๋จ์ฉ ์ฝ๊ณ ์ ์ฒด ๊ธ์ ์์ฝํด๋ณด์ธ์.", key='user_summary')
|
123 |
|
124 |
+
cols = st.columns(2)
|
125 |
+
with cols[0]:
|
126 |
+
global btn_gpt_naturalize_func
|
127 |
+
btn_gpt_naturalize_func = st.button("๊ธ ๋ค๋ฌ๊ธฐ")
|
128 |
+
if btn_gpt_naturalize_func and st.session_state.points > 0: # ํฌ์ธํธ ์ฒดํฌ
|
129 |
+
update_points(-1) # ํฌ์ธํธ ๊ฐ์
|
130 |
+
st.session_state.history.append(["๊ธ ๋ค๋ฌ๊ธฐ", -1, st.session_state.points])
|
131 |
+
with cols[1]:
|
132 |
+
btn_gpt_rating_func = st.button("์ฑ์ ํ๊ธฐ")
|
133 |
+
if btn_gpt_rating_func and st.session_state.points > 0: # ํฌ์ธํธ ์ฒดํฌ
|
134 |
+
update_points(1) # ํฌ์ธํธ ์ฆ๊ฐ
|
135 |
+
st.session_state.history.append(["์ฑ์ ํ๊ธฐ", +1, st.session_state.points]) #fix from -1 to +1
|
136 |
+
gpt_rated_result = gpt_rater(user_summary)
|
137 |
+
st.write(f"์ฑ์ ํ๊ธฐ: {gpt_rated_result}")
|
138 |
+
# https://discuss.streamlit.io/t/streamlit-button-disable-enable/31293 # ๋ฒํผ ๋นํ์ฑํ ๊ด๋ จ ์ฐธ๊ณ (์์ง์ ์๋จ)
|
139 |
+
pass
|
140 |
else:
|
141 |
if st.session_state.points == 0:
|
142 |
+
# ํฌ์ธํธ๊ฐ ์์ด์ ์ฌ์ฉํ ์ ์๋ค๋ ๋ฌธ๊ตฌ ์ถ๊ฐ
|
143 |
+
st.write("ํฌ์ธํธ๊ฐ ์์ด์ ์ฑ์ ํ๊ธฐ๋ฅผ ํ ์ ์์ต๋๋ค.")
|
144 |
pass
|
145 |
|
146 |
+
if btn_gpt_naturalize_func and st.session_state.points > 0:
|
147 |
+
naturalized_text = gpt_text_naturalizer(user_summary) # ๋ค์์ฐ๊ธฐ ํจ์
|
148 |
+
st.write(f"๊ธ ๋ค๋ฌ๊ธฐ: {naturalized_text}")
|
149 |
+
pass
|
150 |
+
else:
|
151 |
+
if st.session_state.points == 0:
|
152 |
+
st.write("ํฌ์ธํธ๊ฐ ์์ด์ ๊ธ ๋ค๋ฌ๊ธฐ ๊ธฐ๋ฅ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.")
|
153 |
+
pass
|
154 |
|
155 |
+
btn_submit_summarized_text = st.button("์ ์ถํ๊ธฐ")
|
156 |
+
if btn_submit_summarized_text:
|
157 |
+
# ๊ตฌ๊ธ ๋๋ผ์ด๋ธ api ์ด์ฉํ์ฌ ์
๋ก๋
|
158 |
+
st.write("์ ์ถ ๋์์ต๋๋ค.")
|
159 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
|
161 |
|
162 |
+
def display_qna_with_gpt():
|
163 |
with st.container():
|
164 |
+
cols = st.columns(2)
|
165 |
+
# st.header("๊ฒฐ๊ณผ")
|
166 |
+
with cols[0]:
|
167 |
+
global btn_explanation
|
168 |
+
btn_explanation = st.button("์ถ๊ฐ ์ค๋ช
")
|
169 |
+
if btn_explanation and st.session_state.points > 0: # ํฌ์ธํธ ์ฒดํฌ
|
170 |
+
update_points(1) # ํฌ์ธํธ ์ฆ๊ฐ
|
171 |
+
st.session_state.history.append(["์ถ๊ฐ ์ค๋ช
", 1, st.session_state.points])
|
172 |
+
|
173 |
+
user_input = st.text_area("์ถ๊ฐ ์ค๋ช
์ด ํ์ํ๊ฑฐ๋ ๋ค์ ์ฐ๊ณ ์ถ์ ๋ด์ฉ์ ์
๋ ฅํ์ธ์.", key='user_input')
|
174 |
+
|
175 |
if btn_explanation and st.session_state.points > 0:
|
176 |
+
explanation = gpt_explanation_generator(user_input, user_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
|
178 |
# ์ถ๊ฐ ์ค๋ช
|
179 |
st.write(f"์ถ๊ฐ ์ค๋ช
: {explanation}")
|
|
|
185 |
|
186 |
pass
|
187 |
|
188 |
+
if btn_gpt_easier_text_func and st.session_state.points > 0:
|
189 |
+
easier_text = gpt_easier_text_generator(user_input)
|
190 |
+
|
191 |
+
# ์ฌ์ด ํํ์ผ๋ก ๋ฐ๊ฟ์ค์ผ๋ก ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
192 |
+
st.write(f"์ฌ์ด ๊ธ: {easier_text}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
193 |
pass
|
194 |
else:
|
195 |
if st.session_state.points == 0:
|
196 |
+
st.write("ํฌ์ธํธ๊ฐ ์์ด์ ์ฌ์ด ํํ์ผ๋ก ๋ฐ๊ฟ์ค ๊ธฐ๋ฅ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.")
|
197 |
pass
|
198 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
199 |
|
|
|
|
|
200 |
|
201 |
def main():
|
202 |
+
st.title("์ฑGPT์ ํจ๊ป ์์ฝํ๊ธฐ")
|
203 |
#ํฌ์ธํธ ์์คํ
์ค๋ช
์ถ๊ฐ
|
204 |
+
st.sidebar.markdown("ํฌ์ธํธ๋ 3์ ๋ถํฐ ์์ํด์, **ํค์๋๋ฅผ ์ฐพ์์ค**, **์ฌ์ด ํํ์ผ๋ก ๋ฐ๊ฟ์ค**, **๊ธ ๋ค๋ฌ๊ธฐ**๋ฅผ ํ ๋ 1์ ์ฉ ๊ฐ์ํฉ๋๋ค. **์ถ๊ฐ ์ค๋ช
**๊ณผ **์ฑ์ ํ๊ธฐ**๋ฅผ ํ ๋ 1์ ์ฉ ์ฆ๊ฐํฉ๋๋ค. ํฌ์ธํธ๊ฐ 0์ ์ด ๋๋ฉด ๋ ์ด์ ์์
์ ํ ์ ์์ต๋๋ค. ")
|
205 |
+
|
206 |
+
# col1, col2 = st.columns(2)
|
207 |
+
display_preset_func_btns(col)
|
208 |
|
209 |
+
# Paragraph Tabs
|
210 |
+
paragraph_tab1, paragraph_tab2, paragraph_tab3, paragraph_tab4 = st.tabs(["๋จ๋ฝ1", "๋จ๋ฝ2", "๋จ๋ฝ3", "๋จ๋ฝ4"])
|
211 |
+
with paragraph_tab1:
|
212 |
+
if is_easier_button_pressed[0]:
|
213 |
+
text = ast.literal_eval(highlighted_texts_list[0][15:-1])
|
214 |
+
annotated_text(*text) #exec ๋์ ์ธ์๋ก ๋ฐ๋ก ์ ๋ฌ
|
215 |
+
else:
|
216 |
+
display_passage(user_text_paragraphs[0])
|
217 |
+
with paragraph_tab2:
|
218 |
+
display_passage(user_text_paragraphs[1])
|
219 |
+
with paragraph_tab3:
|
220 |
+
display_passage(user_text_paragraphs[2])
|
221 |
+
with paragraph_tab4:
|
222 |
+
display_passage(user_text_paragraphs[3])
|
223 |
+
|
224 |
+
# User Input Tabs
|
225 |
+
user_input_tab1, user_input_tab2 = st.tabs(["์์ฝํ๊ธฐ", "์ฑGPT์๊ฒ ์ง๋ฌธํ๊ธฐ"])
|
226 |
+
with user_input_tab1:
|
227 |
+
display_users_summarization(col)
|
228 |
+
with user_input_tab2:
|
229 |
+
display_qna_with_gpt()
|
230 |
|
|
|
|
|
|
|
|
|
|
|
|
|
231 |
#ํฌ์ธํธ ํ์
|
232 |
display_points()
|
233 |
#ํฌ์ธํธ ๋ณํ ํ์
|
gpt_based_function.py
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import ast
|
2 |
+
import openai
|
3 |
+
from text_annotator import generate_annotated_text
|
4 |
+
|
5 |
+
|
6 |
+
def gpt_keyword_highlighter(user_text):
|
7 |
+
'''
|
8 |
+
:param user_text: str
|
9 |
+
:return: annotated_text: str
|
10 |
+
'''
|
11 |
+
task_description = "You are a Python function that extract 5 keywords from {input_text}. The output should be formatted as ['keyword1', 'keyword2', ...]. Return only the function's output, with no additional explanations."
|
12 |
+
|
13 |
+
user_prompt = r"{input_text}=" + f"{user_text}"
|
14 |
+
|
15 |
+
messages = [{"role": "system", "content": task_description}, {"role": "user", "content": user_prompt}]
|
16 |
+
|
17 |
+
response = openai.ChatCompletion.create(
|
18 |
+
model="gpt-3.5-turbo",
|
19 |
+
messages=messages,
|
20 |
+
temperature=0,
|
21 |
+
max_tokens=1019,
|
22 |
+
top_p=0,
|
23 |
+
frequency_penalty=0,
|
24 |
+
presence_penalty=0
|
25 |
+
)
|
26 |
+
|
27 |
+
extracted_keywords = response['choices'][0]['message']['content']
|
28 |
+
|
29 |
+
## literal_eval ํจ์๋ฅผ ์ฌ์ฉํ์ฌ string์ list๋ก ๋ณํ
|
30 |
+
extracted_keywords = ast.literal_eval(extracted_keywords)
|
31 |
+
|
32 |
+
## highlighted_text ํ์ฒ๋ฆฌ ํจ์ ์ถ๊ฐ
|
33 |
+
highlighted_text = generate_annotated_text(text=user_text, keyw_list=extracted_keywords)
|
34 |
+
|
35 |
+
return highlighted_text
|
36 |
+
|
37 |
+
|
38 |
+
def gpt_text_naturalizer(user_input):
|
39 |
+
paraphrasing_task = f"Rewrite the contents of '{user_input}' so that it will pass the writing test."
|
40 |
+
|
41 |
+
messages = [
|
42 |
+
{"role": "system", "content": "You are a helpful assistant. use only korean"},
|
43 |
+
{"role": "user", "content": paraphrasing_task}
|
44 |
+
]
|
45 |
+
|
46 |
+
response = openai.ChatCompletion.create(
|
47 |
+
model="gpt-3.5-turbo-16k",
|
48 |
+
messages=messages,
|
49 |
+
temperature=0.1,
|
50 |
+
max_tokens=2500
|
51 |
+
)
|
52 |
+
|
53 |
+
paraphrased_text = response['choices'][0]['message']['content']
|
54 |
+
return paraphrased_text
|
55 |
+
|
56 |
+
|
57 |
+
def gpt_explanation_generator(user_input, text_to_consider):
|
58 |
+
explanation_task = f"Explain the term '{user_input}' in a simple manner, based on the context of the following passage: {text_to_consider}"
|
59 |
+
|
60 |
+
messages = [
|
61 |
+
{"role": "system",
|
62 |
+
"content": "You are a helpful assistant that explains complex topics in a way that an elementary school student can understand. use only korean"},
|
63 |
+
{"role": "user", "content": explanation_task}
|
64 |
+
]
|
65 |
+
|
66 |
+
response = openai.ChatCompletion.create(
|
67 |
+
model="gpt-3.5-turbo-16k",
|
68 |
+
messages=messages,
|
69 |
+
temperature=0.1,
|
70 |
+
max_tokens=200
|
71 |
+
)
|
72 |
+
|
73 |
+
explanation = response['choices'][0]['message']['content']
|
74 |
+
return explanation
|
75 |
+
|
76 |
+
|
77 |
+
def gpt_easier_text_generator(user_input):
|
78 |
+
messages = [
|
79 |
+
{"role": "system",
|
80 |
+
"content": "You have a special talent for easily rewriting text. Make it easy to paraphrase, use simple words that an elementary school student can understand, but don't shorten or summarize. Don't forget to use only korean."},
|
81 |
+
{"role": "user", "content": user_input}
|
82 |
+
]
|
83 |
+
|
84 |
+
response = openai.ChatCompletion.create(
|
85 |
+
model="gpt-3.5-turbo",
|
86 |
+
messages=messages,
|
87 |
+
temperature=0,
|
88 |
+
max_tokens=2048
|
89 |
+
)
|
90 |
+
|
91 |
+
explanation = response['choices'][0]['message']['content']
|
92 |
+
|
93 |
+
|
94 |
+
def gpt_rater(user_input):
|
95 |
+
# ๋ฃจ๋ธ๋ฆญ์ ์ํ ์ฑ์
|
96 |
+
lubric = """์ฑ์ ๊ธฐ์ค ์: ๋ฌธ์ฅ์ ์ฃผ์ ๋ฅผ ํ์
ํ๊ณ , ์ฃผ์ ๋ด์ฉ์ ํ์
ํ ์ ์๋ค. ์ค: ๋ฌธ์ฅ์ ์ฃผ์ ๋ฅผ ํ์
ํ ์ ์๋ค. ํ: ๋ฌธ์ฅ์ ์ฃผ์ ๋ฅผ ํ์
ํ ์ ์๋ค."""
|
97 |
+
# ๋ฃจ๋ธ๋ฆญ ๊ธฐ์ค์ ์ด์ฉํด์ ์
๋ ฅ์นธ์ ์
๋ ฅํ ๋ด์ฉ์ ์ฑ์ ํ๋ ์์ด๋ก ํ๋กฌํํธ
|
98 |
+
explanation_task = f"{lubric}์ ๊ธฐ์ค์ผ๋ก {user_input}์ ๋ด์ฉ์ ์ฑ์ ํด์ฃผ์ธ์. ์ฑ์ ๊ธฐ์ค์ ๊ณต๊ฐํ์ง ๋ง๊ณ ์, ์ค,ํ๋ก ๋๋๊ณ ๊ฐ๋จํ ์ด์ ๋ฅผ ์๋ ค์ฃผ์ธ์."
|
99 |
+
messages = [
|
100 |
+
{"role": "system", "content": "You are a helpful assistant. use only korean"},
|
101 |
+
{"role": "user", "content": explanation_task}
|
102 |
+
]
|
103 |
+
|
104 |
+
response = openai.ChatCompletion.create(
|
105 |
+
model="gpt-3.5-turbo-16k",
|
106 |
+
messages=messages,
|
107 |
+
temperature=0.1,
|
108 |
+
max_tokens=2500
|
109 |
+
)
|
110 |
+
|
111 |
+
feedback = response['choices'][0]['message']['content']
|
112 |
+
return feedback
|
text_annotator.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
|
3 |
+
def generate_annotated_text(text, keyw_list):
|
4 |
+
'''
|
5 |
+
:param text: str
|
6 |
+
:param keyw_list: list of str
|
7 |
+
:return: str
|
8 |
+
|
9 |
+
e.g. input1 = 'I like an apple. Do you like apples too?'
|
10 |
+
input2 = ["like", "apple"]
|
11 |
+
output = annotated_text("I ", ("like", ""), " an ", ("apple", ""), ". Do you ", ("like", ""), " ", ("apple", ""), "s too?")
|
12 |
+
'''
|
13 |
+
def find_keyword_index(text, keyw_list):
|
14 |
+
indices = []
|
15 |
+
|
16 |
+
for keyword in keyw_list:
|
17 |
+
for match in re.finditer(keyword, text):
|
18 |
+
indices.append((keyword, match.start()))
|
19 |
+
|
20 |
+
return indices
|
21 |
+
|
22 |
+
indices = find_keyword_index(text, keyw_list)
|
23 |
+
|
24 |
+
# 1. indices๋ฅผ index ๊ฐ์ ๋ฐ๋ผ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ
|
25 |
+
sorted_indices = sorted(indices, key=lambda x: x[1])
|
26 |
+
|
27 |
+
output = 'annotated_text('
|
28 |
+
last_index = 0
|
29 |
+
|
30 |
+
# 2. input1์์ ๊ฐ ํค์๋์ ์์น๋ฅผ ํ์ฉํ์ฌ ๋ฌธ์์ด ๋ถํ ๋ฐ ์ฌ์ฒ๋ฆฌ
|
31 |
+
for word, start_idx in sorted_indices:
|
32 |
+
output += f'"{text[last_index:start_idx]}", ("{word}", ""), '
|
33 |
+
last_index = start_idx + len(word)
|
34 |
+
|
35 |
+
# 3. ๊ฒฐ๊ณผ ๋ฌธ์์ด ์์ฑ
|
36 |
+
output += f'"{text[last_index:]}"' + ')'
|
37 |
+
|
38 |
+
# 4. ์ฐ์ ๋ฌธ์์ด ๋์น
|
39 |
+
string_cont_type1 = re.compile(r'", ""\), " ", \("')
|
40 |
+
string_cont_type2 = re.compile(r'", ""\), "", \("')
|
41 |
+
output = string_cont_type1.sub(' ', output)
|
42 |
+
output = string_cont_type2.sub(' ', output)
|
43 |
+
|
44 |
+
return output
|
45 |
+
|