Update app.py
Browse files
app.py
CHANGED
@@ -3,143 +3,98 @@ import streamlit as st
|
|
3 |
import openai
|
4 |
import os
|
5 |
|
6 |
-
|
7 |
-
#์ง๋ฌธ์ ์
๋ ฅ ๋ฐ์์ ํค์๋๋ง ์ถ์ถํ๋ ํจ์
|
8 |
-
#๋ฌธ๋จ์์ ๋ชจ๋ฅด๋ ๋ด์ฉ์ ์ถ๊ฐ์ ์ผ๋ก ์ค๋ช
ํด์ค
|
9 |
-
#์
๋ ฅ์ฐฝ์ ์
๋ ฅํ ์ง๋ฌธ์์ ํํ์ ๋ค๋ฌ์ ๊ธ์ ์์ฑํด์ค gpt3-5-turbo-16k ์ฌ์ฉ
|
10 |
-
#์ถ๊ฐ ์ค๋ช
๋ฒํผ์ ๋๋ฅด๋ฉด ์ถ๊ฐ์ ์ธ ์ค๋ช
์ ํด์ค
|
11 |
-
|
12 |
# OpenAI API ์ค์ (ํ๊ฒฝ ๋ณ์์์ ์ฝ์ด์ด)
|
13 |
openai.api_key = os.getenv("OPENAI_API_KEY") # ์ค์ ์ฝ๋์์ ์ฃผ์ ํด์
|
14 |
|
15 |
def main():
|
16 |
st.title("ํ๊ตญ์ด ํ์ต์๋ฅผ ์ํ HCI tools")
|
17 |
|
18 |
-
col1, col2= st.columns(2)
|
19 |
|
20 |
with col1:
|
21 |
st.header("์ง๋ฌธ")
|
22 |
|
23 |
-
user_text = """๋ฏผ์ฃผ์ฃผ์ ์ฌํ๋ ๊ตญ๋ฏผ์ด ์ ์น์ ์ฐธ์ฌํ ๊ถ๋ฆฌ๋ฅผ ๋ณด์ฅํ๋ค. ๊ทธ๋ฌํ ๊ถ๋ฆฌ๋ฅผ ์ฐธ์ ๊ถ์ด๋ผ ํ๋๋ฐ, ์ด๋ ๊ธฐ๋ณธ์ ์ผ๋ก โ์ ๊ฑฐโ๋ก ์คํ๋๋ค. ์ ๊ฑฐ๋ ์ฌํ ์ง๋จ์ ๋ํ์๋ ๊ณต์ง์๋ฅผ ์ ์ถํ์ฌ ๊ทธ๋ค์๊ฒ ๋ํ์ฑ์ ๋ถ์ฌํ๋ ํ์์ด๋ค. ๊ทธ๋ฌ๋ฏ๋ก ๋์ ํฌํ์จ์ ๋ฏผ์ฃผ์ฃผ์์ ์ ๋น์ฑ ํ๋ณด์ ๊น์ ๊ด๋ จ์ด ์๋ค.
|
24 |
-
์ ๊ฑฐ ํฌํ ์ ๋์๋ ํฌํ๊ถ ํ์ฌ๋ฅผ ํฌํ์์ ์์ ์์ฌ์ ๋งก๊ธฐ๋ โ์์ ํฌํ์ โ์ ํฌํ๊ถ ํ์ฌ๋ฅผ ๊ตญ๋ฏผ์ ์๋ฌด๋ก ๊ฐ์ฃผํ๊ณ ์ ๋นํ ์ฌ์ ์์ด ๊ธฐ๊ถํ๋ฉด ๋ฒ์ ์ ์ฌ๋ฅผ ๊ฐํ๋ โ์๋ฌด ํฌํ์ โ๊ฐ ์๋ค. ์ฐ๋ฆฌ๋๋ผ๋ ์์ ํฌํ์ ๋ฅผ ์ฑํํ๊ณ ์๋๋ฐ, ์ต๊ทผ ์น๋ฅธ ์ ๊ฑฐ์ ํ๊ท ํฌํ์จ์ด 50ํผ์ผํธ๋๋ก ๋ํ๋ฌ๋ค. ๊ฒฝ์ ๊ฐ๋ฐ ํ๋ ฅ ๊ธฐ๊ตฌ(OECD) ํ์๊ตญ ํ๊ท ์ด 70ํผ์ผํธ๋์ธ ๊ฒ์ ์๊ฐํ๋ฉด ๋งค์ฐ ๋ฎ์ ์์น๋ผ ํ ์ ์๋ค. ์ด๋ฌํ ์ํฉ์ด ์ง์๋์ ์๋ฌด ํฌํ์ ๋ฅผ ๋์
ํด์ผ ํ๋ค๋ ์๊ฒฌ์ด ์ ์๋์๊ณ , ์์ ํฌํ์ ๊ฐ ๋ฏผ์ฃผ์ฃผ์์ ์์น์ ๋ง์ผ๋ฏ๋ก ์ด๋ฅผ ์ ์งํด์ผ ํ๋ค๋ ์๊ฒฌ๊ณผ ๋๋ฆฝํ๊ณ ์๋ค.
|
25 |
-
์๋ฌด ํฌํ์ ๋ฅผ ๋์
ํ์๋ ์ธก์ ๋ฎ์ ํฌํ์จ๋ก ํฌํ ๊ฒฐ๊ณผ์ ์ ๋น์ฑ์ ํ๋ณดํ์ง ๋ชปํ๋ ๋ฌธ์ ๊ฐ ๋งค์ฐ ์ฌ๊ฐํ๋ค๊ณ ์ฃผ์ฅํ๋ค. ๋ ์๋ฌด ํฌํ์ ์ ๊ฐ์ ์ฑ๊ณผ ๋ฒ์ ์ ์ฌ๊ฐ ํฌํ์จ์ ๋์ด๋ฏ๋ก ํฌํ์จ์ด ๋ฎ์์ ๋ฐ์ํ๋ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์๋ค๊ณ ๋ณธ๋ค. ๊ทธ๋ฆฌ๊ณ ๊ตญ๋ฏผ ๋๋ถ๋ถ์ด ํฌํ์ ์ฐธ์ฌํ๊ฒ ๋๋ฉด ์ ์น์ธ๋ค์ด ๋ชจ๋ ๊ณ์ธต์ ์ง์ง๋ฅผ ๋ฐ๊ธฐ ์ํด ์ ์ฑ
๊ฒฝ์๋ ฅ์ ๋์ด๋ ค ํ ๊ฒ์ด๋ฏ๋ก ์ ์น ์์ธ ๊ณ์ธต์ ๋์ฑ ๊ด์ฌ์ ์๋ ํจ๊ณผ๊ฐ ์์ ๊ฒ์ด๋ผ๊ณ ์ด์ผ๊ธฐํ๋ค.
|
26 |
-
๋ฐ๋ฉด ์๋ฌด ํฌํ์ ์ ๋ฐ๋ํ๋ ์ธก์ ํ์ฌ ์ฐ๋ฆฌ๋๋ผ์ ํฌํ์จ์ด ์ ์น ์ง๋์๋ค์ ๋ํ์ฑ์ ํผ์ํ ๋งํผ ์ฌ๊ฐํ ์ํฉ์ ์๋๋ผ๊ณ ์ฃผ์ฅํ๋ค. ๋ ํฌํ์จ์ ๋์ด๋ ๊ฒ๋ณด๋ค ๊ตญ๋ฏผ์ ์ ๋ขฐ๋ฅผ ํ๋ณตํ๋ ๊ฒ์ด ๋ ์ค์ํ๊ณ , ์๋ฏผ ๊ต์ก์ด๋ ๋ชจ์ ํฌํ ๊ต์ก ํ๋ก๊ทธ๋จ์ผ๋ก๋ ํฌํ์จ ์์น์ ๊ธฐ๋ํ ์ ์๋ค๋ฉฐ ์๋ฌด ํฌํ์ ์ ๋์
๋ง์ด ํฌํ์จ์ด๋ ์ ์น์ ๊ด์ฌ์ ๋์ด๋ ํด๊ฒฐ ๋ฐฉ์์ ์๋๋ผ๊ณ ์ด์ผ๊ธฐํ๋ค. ๊ทธ๋ฆฌ๊ณ ์๋ฌด ํฌํ์ ๋ฅผ ๋์
ํ๋ฉด, ์ ์ถ๋ ์ ์น์ธ๋ค์ด ๋์ ํฌํ์จ์ ํ๊ณ๋ก ์ํ๋ฌด์ธ์ ํ๋๋ฅผ ๊ฐ๋ ๋ถ์์ฉ์ด ์๊ธด๋ค๋ ๊ฐ ํ๋ณด์๋ฅผ ์ ๋ชจ๋ฅด๋ ์ํ์์ ํฌํํ๋ ์ผ์ด ๋ฐ์ํ์ฌ ๊ตญ๋ฏผ์ ๋ป์ด ์คํ๋ ค ์๊ณก๋ ์ ์๋ค๋ฉฐ ์ฐ๋ ค์ ๋ชฉ์๋ฆฌ๋ฅผ ๋ด๊ณ ์๋ค.
|
27 |
-
"""
|
28 |
st.write(user_text)
|
29 |
|
|
|
30 |
with col2:
|
|
|
31 |
user_input = st.text_input("๋ชจ๋ฅด๋ ๋ฌธ์ฅ์ด๋ ๋จ์ด๋ฅผ ์
๋ ฅํ์ธ์:", "")
|
32 |
-
|
|
|
33 |
cols = st.columns(4)
|
34 |
-
with cols[0]:
|
35 |
-
|
36 |
-
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:
|
37 |
-
annotated_text(
|
38 |
-
"This ",
|
39 |
-
("is", ""),
|
40 |
-
" some ",
|
41 |
-
("annotated", ""),
|
42 |
-
("text", ""),
|
43 |
-
" for those of ",
|
44 |
-
("you", ""),
|
45 |
-
" who ",
|
46 |
-
("like", ""),
|
47 |
-
" this sort of ",
|
48 |
-
("thing", ""),
|
49 |
-
". "
|
50 |
-
)"""
|
51 |
-
|
52 |
-
user_prompt = f"First, extract key words for the topic st-annotated-text format.: {user_text}"
|
53 |
-
|
54 |
-
messages = [{"role": "system", "content": task_description}, {"role": "user", "content": user_prompt}]
|
55 |
-
|
56 |
-
response = openai.ChatCompletion.create(
|
57 |
-
model="gpt-3.5-turbo-16k",
|
58 |
-
messages=messages,
|
59 |
-
temperature=0.1,
|
60 |
-
max_tokens=2500,
|
61 |
-
top_p=0.2,
|
62 |
-
frequency_penalty=0,
|
63 |
-
presence_penalty=0
|
64 |
-
)
|
65 |
-
|
66 |
-
highlighted_text = response['choices'][0]['message']['content']
|
67 |
-
|
68 |
-
exec(highlighted_text)
|
69 |
-
pass
|
70 |
-
|
71 |
with cols[1]:
|
72 |
-
|
73 |
-
# ์
๋ ฅ๋ ํ
์คํธ ์ค๋ช
|
74 |
-
explanation_task = f"Explain the term '{user_input}' in a simple manner, based on the context of the following passage: {user_text}"
|
75 |
-
|
76 |
-
messages = [
|
77 |
-
{"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"},
|
78 |
-
{"role": "user", "content": explanation_task}
|
79 |
-
]
|
80 |
-
|
81 |
-
response = openai.ChatCompletion.create(
|
82 |
-
model="gpt-3.5-turbo-16k",
|
83 |
-
messages=messages,
|
84 |
-
temperature=0.1,
|
85 |
-
max_tokens=200
|
86 |
-
)
|
87 |
-
|
88 |
-
explanation = response['choices'][0]['message']['content']
|
89 |
-
|
90 |
-
# ์ฌ์ด ํํ์ผ๋ก ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
91 |
-
st.text("์ค๋ช
:")
|
92 |
-
st.write(explanation)
|
93 |
-
pass
|
94 |
-
|
95 |
-
#๋ฒํผ์ ๋๋ฅด๋ฉด user_text์ ๋ด์ฉ์ ์ฝ๊ฒ ๋ค์ ์ค๋ช
ํด์ค
|
96 |
with cols[2]:
|
97 |
-
|
98 |
-
# ์
๋ ฅ๋ ํ
์คํธ ์ค๋ช
|
99 |
-
#user_text๋ฅผ ์ฝ๊ฒ ์ค๋ช
ํด์ค
|
100 |
-
explanation_task = f"Describe the fingerprint of '{user_text}' in a way that an elementary school student could understand."
|
101 |
-
|
102 |
-
messages = [
|
103 |
-
{"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"},
|
104 |
-
{"role": "user", "content": explanation_task}
|
105 |
-
]
|
106 |
-
|
107 |
-
response = openai.ChatCompletion.create(
|
108 |
-
model="gpt-3.5-turbo-16k",
|
109 |
-
messages=messages,
|
110 |
-
temperature=0.1,
|
111 |
-
max_tokens=500
|
112 |
-
)
|
113 |
-
|
114 |
-
explanation = response['choices'][0]['message']['content']
|
115 |
-
|
116 |
-
# ์ฌ์ด ํํ์ผ๋ก ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
117 |
-
st.text("์ค๋ช
:")
|
118 |
-
st.write(explanation)
|
119 |
-
pass
|
120 |
-
#๋ฒํผ์ ๋๋ฅด๋ฉด ์
๋ ฅ์ค์ธ ๊ธ์ ๋ค๋ฌ์ด์ ๋ค์ ์จ์ค
|
121 |
with cols[3]:
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
|
139 |
-
# ์ฌ์ด ํํ์ผ๋ก ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
140 |
-
st.text("์ค๋ช
:")
|
141 |
-
st.write(explanation)
|
142 |
-
pass
|
143 |
-
|
144 |
if __name__ == "__main__":
|
145 |
-
main()
|
|
|
3 |
import openai
|
4 |
import os
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
# OpenAI API ์ค์ (ํ๊ฒฝ ๋ณ์์์ ์ฝ์ด์ด)
|
7 |
openai.api_key = os.getenv("OPENAI_API_KEY") # ์ค์ ์ฝ๋์์ ์ฃผ์ ํด์
|
8 |
|
9 |
def main():
|
10 |
st.title("ํ๊ตญ์ด ํ์ต์๋ฅผ ์ํ HCI tools")
|
11 |
|
12 |
+
col1, col2 = st.columns(2)
|
13 |
|
14 |
with col1:
|
15 |
st.header("์ง๋ฌธ")
|
16 |
|
17 |
+
user_text = """๋ฏผ์ฃผ์ฃผ์ ์ฌํ๋ ๊ตญ๋ฏผ์ด ์ ์น์ ์ฐธ์ฌํ ๊ถ๋ฆฌ๋ฅผ ๋ณด์ฅํ๋ค. ๊ทธ๋ฌํ ๊ถ๋ฆฌ๋ฅผ ์ฐธ์ ๊ถ์ด๋ผ ํ๋๋ฐ, ์ด๋ ๊ธฐ๋ณธ์ ์ผ๋ก โ์ ๊ฑฐโ๋ก ์คํ๋๋ค. ์ ๊ฑฐ๋ ์ฌํ ์ง๋จ์ ๋ํ์๋ ๊ณต์ง์๋ฅผ ์ ์ถํ์ฌ ๊ทธ๋ค์๊ฒ ๋ํ์ฑ์ ๋ถ์ฌํ๋ ํ์์ด๋ค. ๊ทธ๋ฌ๋ฏ๋ก ๋์ ํฌํ์จ์ ๋ฏผ์ฃผ์ฃผ์์ ์ ๋น์ฑ ํ๋ณด์ ๊น์ ๊ด๋ จ์ด ์๋ค."""
|
|
|
|
|
|
|
|
|
18 |
st.write(user_text)
|
19 |
|
20 |
+
# ์๋จ ์ปจํ
์ด๋
|
21 |
with col2:
|
22 |
+
st.header("์กฐ์ ํจ๋")
|
23 |
user_input = st.text_input("๋ชจ๋ฅด๋ ๋ฌธ์ฅ์ด๋ ๋จ์ด๋ฅผ ์
๋ ฅํ์ธ์:", "")
|
24 |
+
|
25 |
+
# ๋ฒํผ row
|
26 |
cols = st.columns(4)
|
27 |
+
with cols[0]:
|
28 |
+
btn_keyword = st.button("ํค์๋ ์ฐพ๊ธฐ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
with cols[1]:
|
30 |
+
btn_explanation = st.button("์ถ๊ฐ ์ค๋ช
")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
with cols[2]:
|
32 |
+
btn_simple = st.button("์ฌ์ด ํํ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
with cols[3]:
|
34 |
+
btn_rewrite = st.button("๋ค์ ์ฐ๊ธฐ")
|
35 |
+
|
36 |
+
# ํ๋จ ์ปจํ
์ด๋
|
37 |
+
with st.container():
|
38 |
+
st.header("๊ฒฐ๊ณผ")
|
39 |
+
|
40 |
+
if btn_keyword:
|
41 |
+
# ํค์๋ ์ฐพ๊ธฐ ๋ก์ง
|
42 |
+
# OpenAI API ํธ์ถ์ ์ค์ ๋ก ์คํํ์ค ๋ ์ฃผ์์ ํด์ ํด์ฃผ์ธ์.
|
43 |
+
|
44 |
+
response = openai.ChatCompletion.create(
|
45 |
+
model="gpt-3.5-turbo-16k",
|
46 |
+
prompt=f"Extract key words from the text: {user_text}",
|
47 |
+
temperature=0.1,
|
48 |
+
max_tokens=200
|
49 |
+
)
|
50 |
+
keywords = response['choices'][0]['text']
|
51 |
+
|
52 |
+
keywords = "๋ฏผ์ฃผ์ฃผ์, ๊ตญ๋ฏผ, ์ ์น, ์ฐธ์ฌ, ๊ถ๋ฆฌ, ์ฐธ์ ๊ถ, ์ ๊ฑฐ, ์ฌํ ์ง๋จ, ๋ํ์, ๊ณต์ง์, ๋ํ์ฑ, ํฌํ์จ, ์ ๋น์ฑ" # ์์
|
53 |
+
st.write(f"ํค์๋: {keywords}")
|
54 |
+
|
55 |
+
if btn_explanation:
|
56 |
+
# ์ถ๊ฐ ์ค๋ช
๋ก์ง
|
57 |
+
# OpenAI API ํธ์ถ์ ์ค์ ๋ก ์คํํ์ค ๋ ์ฃผ์์ ํด์ ํด์ฃผ์ธ์.
|
58 |
+
|
59 |
+
response = openai.ChatCompletion.create(
|
60 |
+
model="gpt-3.5-turbo-16k",
|
61 |
+
prompt=f"Explain the term '{user_input}' in the context of the text: {user_text}",
|
62 |
+
temperature=0.1,
|
63 |
+
max_tokens=200
|
64 |
+
)
|
65 |
+
explanation = response['choices'][0]['text']
|
66 |
+
|
67 |
+
explanation = "์ฌ๊ธฐ์ ์ถ๊ฐ ์ค๋ช
์ด ๋ค์ด๊ฐ๋๋ค." # ์์
|
68 |
+
st.write(f"์ค๋ช
: {explanation}")
|
69 |
+
|
70 |
+
if btn_simple:
|
71 |
+
# ์ฌ์ด ํํ ๋ก์ง
|
72 |
+
# OpenAI API ํธ์ถ์ ์ค์ ๋ก ์คํํ์ค ๋ ์ฃผ์์ ํด์ ํด์ฃผ์ธ์.
|
73 |
+
|
74 |
+
response = openai.ChatCompletion.create(
|
75 |
+
model="gpt-3.5-turbo-16k",
|
76 |
+
prompt=f"Rewrite the text in simpler terms: {user_text}",
|
77 |
+
temperature=0.1,
|
78 |
+
max_tokens=200
|
79 |
+
)
|
80 |
+
simple_text = response['choices'][0]['text']
|
81 |
+
simple_text = "์ฌ๊ธฐ์ ์ฌ์ด ํํ์ด ๋ค์ด๊ฐ๋๋ค." # ์์
|
82 |
+
st.write(f"์ฌ์ด ํํ: {simple_text}")
|
83 |
+
|
84 |
+
if btn_rewrite:
|
85 |
+
# ๋ค์ ์ฐ๊ธฐ ๋ก์ง
|
86 |
+
# OpenAI API ํธ์ถ์ ์ค์ ๋ก ์คํํ์ค ๋ ์ฃผ์์ ํด์ ํด์ฃผ์ธ์.
|
87 |
+
|
88 |
+
response = openai.ChatCompletion.create(
|
89 |
+
model="gpt-3.5-turbo-16k",
|
90 |
+
prompt=f"Rewrite the text: {user_text}",
|
91 |
+
temperature=0.1,
|
92 |
+
max_tokens=200
|
93 |
+
)
|
94 |
+
rewritten_text = response['choices'][0]['text']
|
95 |
+
|
96 |
+
rewritten_text = "์ฌ๊ธฐ์ ๋ค์ ์ด ํ
์คํธ๊ฐ ๋ค์ด๊ฐ๋๋ค." # ์์
|
97 |
+
st.write(f"๋ค์ ์ฐ๊ธฐ: {rewritten_text}")
|
98 |
|
|
|
|
|
|
|
|
|
|
|
99 |
if __name__ == "__main__":
|
100 |
+
main()
|