Update app.py
Browse files
app.py
CHANGED
@@ -5,83 +5,104 @@ import os
|
|
5 |
|
6 |
|
7 |
# OpenAI API ์ค์ (ํ๊ฒฝ ๋ณ์์์ ์ฝ์ด์ด)
|
8 |
-
openai.api_key = os.getenv("OPENAI_API_KEY")
|
9 |
|
10 |
def main():
|
11 |
st.title("GPT-3๋ฅผ ํ์ฉํ ๊ตญ์ด ๋ฌธ์ ์์ฑ๊ธฐ")
|
12 |
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
15 |
์ ๊ฑฐ ํฌํ ์ ๋์๋ ํฌํ๊ถ ํ์ฌ๋ฅผ ํฌํ์์ ์์ ์์ฌ์ ๋งก๊ธฐ๋ โ์์ ํฌํ์ โ์ ํฌํ๊ถ ํ์ฌ๋ฅผ ๊ตญ๋ฏผ์ ์๋ฌด๋ก ๊ฐ์ฃผํ๊ณ ์ ๋นํ ์ฌ์ ์์ด ๊ธฐ๊ถํ๋ฉด ๋ฒ์ ์ ์ฌ๋ฅผ ๊ฐํ๋ โ์๋ฌด ํฌํ์ โ๊ฐ ์๋ค. ์ฐ๋ฆฌ๋๋ผ๋ ์์ ํฌํ์ ๋ฅผ ์ฑํํ๊ณ ์๋๋ฐ, ์ต๊ทผ ์น๋ฅธ ์ ๊ฑฐ์ ํ๊ท ํฌํ์จ์ด 50ํผ์ผํธ๋๋ก ๋ํ๋ฌ๋ค. ๊ฒฝ์ ๊ฐ๋ฐ ํ๋ ฅ ๊ธฐ๊ตฌ(OECD) ํ์๊ตญ ํ๊ท ์ด 70ํผ์ผํธ๋์ธ ๊ฒ์ ์๊ฐํ๋ฉด ๋งค์ฐ ๋ฎ์ ์์น๋ผ ํ ์ ์๋ค. ์ด๋ฌํ ์ํฉ์ด ์ง์๋์ ์๋ฌด ํฌํ์ ๋ฅผ ๋์
ํด์ผ ํ๋ค๋ ์๊ฒฌ์ด ์ ์๋์๊ณ , ์์ ํฌํ์ ๊ฐ ๋ฏผ์ฃผ์ฃผ์์ ์์น์ ๋ง์ผ๋ฏ๋ก ์ด๋ฅผ ์ ์งํด์ผ ํ๋ค๋ ์๊ฒฌ๊ณผ ๋๋ฆฝํ๊ณ ์๋ค.
|
16 |
์๋ฌด ํฌํ์ ๋ฅผ ๋์
ํ์๋ ์ธก์ ๋ฎ์ ํฌํ์จ๋ก ํฌํ ๊ฒฐ๊ณผ์ ์ ๋น์ฑ์ ํ๋ณดํ์ง ๋ชปํ๋ ๋ฌธ์ ๊ฐ ๋งค์ฐ ์ฌ๊ฐํ๋ค๊ณ ์ฃผ์ฅํ๋ค. ๋ ์๋ฌด ํฌํ์ ์ ๊ฐ์ ์ฑ๊ณผ ๋ฒ์ ์ ์ฌ๊ฐ ํฌํ์จ์ ๋์ด๋ฏ๋ก ํฌํ์จ์ด ๋ฎ์์ ๋ฐ์ํ๋ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์๋ค๊ณ ๋ณธ๋ค. ๊ทธ๋ฆฌ๊ณ ๊ตญ๋ฏผ ๋๋ถ๋ถ์ด ํฌํ์ ์ฐธ์ฌํ๊ฒ ๋๋ฉด ์ ์น์ธ๋ค์ด ๋ชจ๋ ๊ณ์ธต์ ์ง์ง๋ฅผ ๋ฐ๊ธฐ ์ํด ์ ์ฑ
๊ฒฝ์๋ ฅ์ ๋์ด๋ ค ํ ๊ฒ์ด๋ฏ๋ก ์ ์น ์์ธ ๊ณ์ธต์ ๋์ฑ ๊ด์ฌ์ ์๋ ํจ๊ณผ๊ฐ ์์ ๊ฒ์ด๋ผ๊ณ ์ด์ผ๊ธฐํ๋ค.
|
17 |
๋ฐ๋ฉด ์๋ฌด ํฌํ์ ์ ๋ฐ๋ํ๋ ์ธก์ ํ์ฌ ์ฐ๋ฆฌ๋๋ผ์ ํฌํ์จ์ด ์ ์น ์ง๋์๋ค์ ๋ํ์ฑ์ ํผ์ํ ๋งํผ ์ฌ๊ฐํ ์ํฉ์ ์๋๋ผ๊ณ ์ฃผ์ฅํ๋ค. ๋ ํฌํ์จ์ ๋์ด๋ ๊ฒ๋ณด๋ค ๊ตญ๋ฏผ์ ์ ๋ขฐ๋ฅผ ํ๋ณตํ๋ ๊ฒ์ด ๋ ์ค์ํ๊ณ , ์๋ฏผ ๊ต์ก์ด๋ ๋ชจ์ ํฌํ ๊ต์ก ํ๋ก๊ทธ๋จ์ผ๋ก๋ ํฌํ์จ ์์น์ ๊ธฐ๋ํ ์ ์๋ค๋ฉฐ ์๋ฌด ํฌํ์ ์ ๋์
๋ง์ด ํฌํ์จ์ด๋ ์ ์น์ ๊ด์ฌ์ ๋์ด๋ ํด๊ฒฐ ๋ฐฉ์์ ์๋๋ผ๊ณ ์ด์ผ๊ธฐํ๋ค. ๊ทธ๋ฆฌ๊ณ ์๋ฌด ํฌํ์ ๋ฅผ ๋์
ํ๋ฉด, ์ ์ถ๋ ์ ์น์ธ๋ค์ด ๋์ ํฌํ์จ์ ํ๊ณ๋ก ์ํ๋ฌด์ธ์ ํ๋๋ฅผ ๊ฐ๋ ๋ถ์์ฉ์ด ์๊ธด๋ค๋ ๊ฐ ํ๋ณด์๋ฅผ ์ ๋ชจ๋ฅด๋ ์ํ์์ ํฌํํ๋ ์ผ์ด ๋ฐ์ํ์ฌ ๊ตญ๋ฏผ์ ๋ป์ด ์คํ๋ ค ์๊ณก๋ ์ ์๋ค๋ฉฐ ์ฐ๋ ค์ ๋ชฉ์๋ฆฌ๋ฅผ ๋ด๊ณ ์๋ค.
|
18 |
"""
|
|
|
|
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
st.write(user_text)
|
23 |
-
# ํ
์คํธ ์
๋ ฅ ๋ฐ์ค ์ถ๊ฐ
|
24 |
-
user_input = st.text_input("๋ชจ๋ฅด๋ ๋ฌธ์ฅ์ด๋ ๋จ์ด๋ฅผ ์
๋ ฅํ์ธ์:", "")
|
25 |
-
|
26 |
-
if st.button("ํค์๋ ์ฐพ๊ธฐ"):
|
27 |
-
|
28 |
-
task_description ="""You are a useful helper that generates annotated text for Python's st-annotated-text library. Your task is to identify the topic of the passage and highlight the key words needed to convey the meaning. You should be able to identify the main points. Also, please mark keywords based on the different paragraphs and headings provided in the text. The output should be formatted in the following way:
|
29 |
-
annotated_text(
|
30 |
-
"This ",
|
31 |
-
("is", ""),
|
32 |
-
" some ",
|
33 |
-
("annotated", ""),
|
34 |
-
("text", ""),
|
35 |
-
" for those of ",
|
36 |
-
("you", ""),
|
37 |
-
" who ",
|
38 |
-
("like", ""),
|
39 |
-
" this sort of ",
|
40 |
-
("thing", ""),
|
41 |
-
". "
|
42 |
-
)"""
|
43 |
-
|
44 |
-
user_prompt = f"First, extract key words for the topic st-annotated-text format.: {user_text}"
|
45 |
-
|
46 |
-
messages = [{"role": "system", "content": task_description}, {"role": "user", "content": user_prompt}]
|
47 |
-
|
48 |
-
response = openai.ChatCompletion.create(
|
49 |
-
model="gpt-3.5-turbo-16k",
|
50 |
-
messages=messages,
|
51 |
-
temperature=0.1,
|
52 |
-
max_tokens=2500,
|
53 |
-
top_p=0.2,
|
54 |
-
frequency_penalty=0,
|
55 |
-
presence_penalty=0
|
56 |
-
)
|
57 |
-
|
58 |
-
highlighted_text = response['choices'][0]['message']['content']
|
59 |
-
|
60 |
-
exec(highlighted_text)
|
61 |
-
|
62 |
-
# ํ
์คํธ ๋ฒํผ ์ถ๊ฐ
|
63 |
-
if st.button("์ถ๊ฐ ์ค๋ช
"):
|
64 |
-
# ์
๋ ฅ๋ ํ
์คํธ ์ค๋ช
|
65 |
-
explanation_task = f"Explain the term '{user_input}' in a simple manner, based on the context of the following passage: {user_text}"
|
66 |
|
67 |
-
messages = [
|
68 |
-
{"role": "system", "content": "You are a helpful assistant that explains complex topics in a way that an elementary school student can understand."},
|
69 |
-
{"role": "user", "content": explanation_task}
|
70 |
-
]
|
71 |
|
72 |
response = openai.ChatCompletion.create(
|
73 |
model="gpt-3.5-turbo-16k",
|
74 |
messages=messages,
|
75 |
temperature=0.1,
|
76 |
-
max_tokens=
|
|
|
|
|
|
|
77 |
)
|
78 |
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
|
87 |
if __name__ == "__main__":
|
|
|
5 |
|
6 |
|
7 |
# OpenAI API ์ค์ (ํ๊ฒฝ ๋ณ์์์ ์ฝ์ด์ด)
|
8 |
+
openai.api_key = os.getenv("OPENAI_API_KEY") # ์ค์ ์ฝ๋์์ ์ฃผ์ ํด์
|
9 |
|
10 |
def main():
|
11 |
st.title("GPT-3๋ฅผ ํ์ฉํ ๊ตญ์ด ๋ฌธ์ ์์ฑ๊ธฐ")
|
12 |
|
13 |
+
col1, clo2 = st.beta_columns(2)
|
14 |
+
|
15 |
+
with col1:
|
16 |
+
st.header("์ง๋ฌธ")
|
17 |
+
st.write(user_text)
|
18 |
+
user_text = """๋ฏผ์ฃผ์ฃผ์ ์ฌํ๋ ๊ตญ๋ฏผ์ด ์ ์น์ ์ฐธ์ฌํ ๊ถ๋ฆฌ๋ฅผ ๋ณด์ฅํ๋ค. ๊ทธ๋ฌํ ๊ถ๋ฆฌ๋ฅผ ์ฐธ์ ๊ถ์ด๋ผ ํ๋๋ฐ, ์ด๋ ๊ธฐ๋ณธ์ ์ผ๋ก โ์ ๊ฑฐโ๋ก ์คํ๋๋ค. ์ ๊ฑฐ๋ ์ฌํ ์ง๋จ์ ๋ํ์๋ ๊ณต์ง์๋ฅผ ์ ์ถํ์ฌ ๊ทธ๋ค์๊ฒ ๋ํ์ฑ์ ๋ถ์ฌํ๋ ํ์์ด๋ค. ๊ทธ๋ฌ๋ฏ๋ก ๋์ ํฌํ์จ์ ๋ฏผ์ฃผ์ฃผ์์ ์ ๋น์ฑ ํ๋ณด์ ๊น์ ๊ด๋ จ์ด ์๋ค.
|
19 |
์ ๊ฑฐ ํฌํ ์ ๋์๋ ํฌํ๊ถ ํ์ฌ๋ฅผ ํฌํ์์ ์์ ์์ฌ์ ๋งก๊ธฐ๋ โ์์ ํฌํ์ โ์ ํฌํ๊ถ ํ์ฌ๋ฅผ ๊ตญ๋ฏผ์ ์๋ฌด๋ก ๊ฐ์ฃผํ๊ณ ์ ๋นํ ์ฌ์ ์์ด ๊ธฐ๊ถํ๋ฉด ๋ฒ์ ์ ์ฌ๋ฅผ ๊ฐํ๋ โ์๋ฌด ํฌํ์ โ๊ฐ ์๋ค. ์ฐ๋ฆฌ๋๋ผ๋ ์์ ํฌํ์ ๋ฅผ ์ฑํํ๊ณ ์๋๋ฐ, ์ต๊ทผ ์น๋ฅธ ์ ๊ฑฐ์ ํ๊ท ํฌํ์จ์ด 50ํผ์ผํธ๋๋ก ๋ํ๋ฌ๋ค. ๊ฒฝ์ ๊ฐ๋ฐ ํ๋ ฅ ๊ธฐ๊ตฌ(OECD) ํ์๊ตญ ํ๊ท ์ด 70ํผ์ผํธ๋์ธ ๊ฒ์ ์๊ฐํ๋ฉด ๋งค์ฐ ๋ฎ์ ์์น๋ผ ํ ์ ์๋ค. ์ด๋ฌํ ์ํฉ์ด ์ง์๋์ ์๋ฌด ํฌํ์ ๋ฅผ ๋์
ํด์ผ ํ๋ค๋ ์๊ฒฌ์ด ์ ์๋์๊ณ , ์์ ํฌํ์ ๊ฐ ๋ฏผ์ฃผ์ฃผ์์ ์์น์ ๋ง์ผ๋ฏ๋ก ์ด๋ฅผ ์ ์งํด์ผ ํ๋ค๋ ์๊ฒฌ๊ณผ ๋๋ฆฝํ๊ณ ์๋ค.
|
20 |
์๋ฌด ํฌํ์ ๋ฅผ ๋์
ํ์๋ ์ธก์ ๋ฎ์ ํฌํ์จ๋ก ํฌํ ๊ฒฐ๊ณผ์ ์ ๋น์ฑ์ ํ๋ณดํ์ง ๋ชปํ๋ ๋ฌธ์ ๊ฐ ๋งค์ฐ ์ฌ๊ฐํ๋ค๊ณ ์ฃผ์ฅํ๋ค. ๋ ์๋ฌด ํฌํ์ ์ ๊ฐ์ ์ฑ๊ณผ ๋ฒ์ ์ ์ฌ๊ฐ ํฌํ์จ์ ๋์ด๋ฏ๋ก ํฌํ์จ์ด ๋ฎ์์ ๋ฐ์ํ๋ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์๋ค๊ณ ๋ณธ๋ค. ๊ทธ๋ฆฌ๊ณ ๊ตญ๋ฏผ ๋๋ถ๋ถ์ด ํฌํ์ ์ฐธ์ฌํ๊ฒ ๋๋ฉด ์ ์น์ธ๋ค์ด ๋ชจ๋ ๊ณ์ธต์ ์ง์ง๋ฅผ ๋ฐ๊ธฐ ์ํด ์ ์ฑ
๊ฒฝ์๋ ฅ์ ๋์ด๋ ค ํ ๊ฒ์ด๋ฏ๋ก ์ ์น ์์ธ ๊ณ์ธต์ ๋์ฑ ๊ด์ฌ์ ์๋ ํจ๊ณผ๊ฐ ์์ ๊ฒ์ด๋ผ๊ณ ์ด์ผ๊ธฐํ๋ค.
|
21 |
๋ฐ๋ฉด ์๋ฌด ํฌํ์ ์ ๋ฐ๋ํ๋ ์ธก์ ํ์ฌ ์ฐ๋ฆฌ๋๋ผ์ ํฌํ์จ์ด ์ ์น ์ง๋์๋ค์ ๋ํ์ฑ์ ํผ์ํ ๋งํผ ์ฌ๊ฐํ ์ํฉ์ ์๋๋ผ๊ณ ์ฃผ์ฅํ๋ค. ๋ ํฌํ์จ์ ๋์ด๋ ๊ฒ๋ณด๋ค ๊ตญ๋ฏผ์ ์ ๋ขฐ๋ฅผ ํ๋ณตํ๋ ๊ฒ์ด ๋ ์ค์ํ๊ณ , ์๋ฏผ ๊ต์ก์ด๋ ๋ชจ์ ํฌํ ๊ต์ก ํ๋ก๊ทธ๋จ์ผ๋ก๋ ํฌํ์จ ์์น์ ๊ธฐ๋ํ ์ ์๋ค๋ฉฐ ์๋ฌด ํฌํ์ ์ ๋์
๋ง์ด ํฌํ์จ์ด๋ ์ ์น์ ๊ด์ฌ์ ๋์ด๋ ํด๊ฒฐ ๋ฐฉ์์ ์๋๋ผ๊ณ ์ด์ผ๊ธฐํ๋ค. ๊ทธ๋ฆฌ๊ณ ์๋ฌด ํฌํ์ ๋ฅผ ๋์
ํ๋ฉด, ์ ์ถ๋ ์ ์น์ธ๋ค์ด ๋์ ํฌํ์จ์ ํ๊ณ๋ก ์ํ๋ฌด์ธ์ ํ๋๋ฅผ ๊ฐ๋ ๋ถ์์ฉ์ด ์๊ธด๋ค๋ ๊ฐ ํ๋ณด์๋ฅผ ์ ๋ชจ๋ฅด๋ ์ํ์์ ํฌํํ๋ ์ผ์ด ๋ฐ์ํ์ฌ ๊ตญ๋ฏผ์ ๋ป์ด ์คํ๋ ค ์๊ณก๋ ์ ์๋ค๋ฉฐ ์ฐ๋ ค์ ๋ชฉ์๋ฆฌ๋ฅผ ๋ด๊ณ ์๋ค.
|
22 |
"""
|
23 |
+
with clo2:
|
24 |
+
user_input = st.text_input("๋ชจ๋ฅด๋ ๋ฌธ์ฅ์ด๋ ๋จ์ด๋ฅผ ์
๋ ฅํ์ธ์:", "")
|
25 |
|
26 |
+
if st.button("ํค์๋ ์ฐพ๊ธฐ"):
|
27 |
+
task_description ="""You are a useful helper that generates annotated text for Python's st-annotated-text library. Your task is to identify the topic of the passage and highlight the key words needed to convey the meaning. You should be able to identify the main points. Also, please mark keywords based on the different paragraphs and headings provided in the text. The output should be formatted in the following way:
|
28 |
+
annotated_text(
|
29 |
+
"This ",
|
30 |
+
("is", ""),
|
31 |
+
" some ",
|
32 |
+
("annotated", ""),
|
33 |
+
("text", ""),
|
34 |
+
" for those of ",
|
35 |
+
("you", ""),
|
36 |
+
" who ",
|
37 |
+
("like", ""),
|
38 |
+
" this sort of ",
|
39 |
+
("thing", ""),
|
40 |
+
". "
|
41 |
+
)"""
|
42 |
|
43 |
+
user_prompt = f"First, extract key words for the topic st-annotated-text format.: {user_text}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
messages = [{"role": "system", "content": task_description}, {"role": "user", "content": user_prompt}]
|
|
|
|
|
|
|
46 |
|
47 |
response = openai.ChatCompletion.create(
|
48 |
model="gpt-3.5-turbo-16k",
|
49 |
messages=messages,
|
50 |
temperature=0.1,
|
51 |
+
max_tokens=2500,
|
52 |
+
top_p=0.2,
|
53 |
+
frequency_penalty=0,
|
54 |
+
presence_penalty=0
|
55 |
)
|
56 |
|
57 |
+
highlighted_text = response['choices'][0]['message']['content']
|
58 |
+
|
59 |
+
exec(highlighted_text)
|
60 |
+
|
61 |
+
# ํ
์คํธ ๋ฒํผ ์ถ๊ฐ
|
62 |
+
if st.button("์ถ๊ฐ ์ค๋ช
"):
|
63 |
+
# ์
๋ ฅ๋ ํ
์คํธ ์ค๋ช
|
64 |
+
explanation_task = f"Explain the term '{user_input}' in a simple manner, based on the context of the following passage: {user_text}"
|
65 |
+
|
66 |
+
messages = [
|
67 |
+
{"role": "system", "content": "You are a helpful assistant that explains complex topics in a way that an elementary school student can understand."},
|
68 |
+
{"role": "user", "content": explanation_task}
|
69 |
+
]
|
70 |
+
|
71 |
+
response = openai.ChatCompletion.create(
|
72 |
+
model="gpt-3.5-turbo-16k",
|
73 |
+
messages=messages,
|
74 |
+
temperature=0.1,
|
75 |
+
max_tokens=150
|
76 |
+
)
|
77 |
+
|
78 |
+
explanation = response['choices'][0]['message']['content']
|
79 |
|
80 |
+
# ์ฌ์ด ํํ์ผ๋ก ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
81 |
+
st.text("์ค๋ช
:")
|
82 |
+
st.write(explanation)
|
83 |
|
84 |
+
if st.button("์ถ๊ฐ ์ค๋ช
"):
|
85 |
+
explanation_task = f"Explain the term '{user_input}' in a simple manner, based on the context of the following passage: {user_text}"
|
86 |
+
|
87 |
+
messages = [
|
88 |
+
{"role": "system", "content": "You are a helpful assistant that explains complex topics in a way that an elementary school student can understand."},
|
89 |
+
{"role": "user", "content": explanation_task}
|
90 |
+
]
|
91 |
+
|
92 |
+
response = openai.ChatCompletion.create(
|
93 |
+
model="gpt-3.5-turbo-16k",
|
94 |
+
messages=messages,
|
95 |
+
temperature=0.1,
|
96 |
+
max_tokens=150
|
97 |
+
)
|
98 |
+
|
99 |
+
explanation = response['choices'][0]['message']['content']
|
100 |
+
|
101 |
+
# ์ฌ์ด ํํ์ผ๋ก ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
102 |
+
st.text("์ค๋ช
:")
|
103 |
+
st.write(explanation)
|
104 |
+
|
105 |
+
|
106 |
|
107 |
|
108 |
if __name__ == "__main__":
|