from annotated_text import annotated_text | |
import streamlit as st | |
import openai | |
import os | |
# OpenAI API ์ค์ (ํ๊ฒฝ ๋ณ์์์ ์ฝ์ด์ด) | |
openai.api_key = os.getenv("OPENAI_API_KEY") # ์ค์ ์ฝ๋์์ ์ฃผ์ ํด์ | |
def main(): | |
st.title("Keyword Highlighter") | |
user_text = st.text_area("Please enter your text here:", "") | |
if st.button("Find Keywords"): | |
# # few-shot learning์ ์ด์ฉํ task_description | |
# task_description = """You are a useful helper that generates annotated text for Python's st-annotated-text library. Your job is to identify the topic of the fingerprint and highlight the key words needed to convey meaning. The output should be formatted in the following way: | |
# annotated_text( | |
# "This ", | |
# ("is", ""), | |
# " some ", | |
# ("annotated", ""), | |
# ("text", ""), | |
# " for those of ", | |
# ("you", ""), | |
# " who ", | |
# ("like", ""), | |
# " this sort of ", | |
# ("thing", ""), | |
# ". " | |
# )""" | |
# 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: | |
# annotated_text( | |
# "This ", | |
# ("is", ""), | |
# " some ", | |
# ("annotated", ""), | |
# ("text", ""), | |
# " for those of ", | |
# ("you", ""), | |
# " who ", | |
# ("like", ""), | |
# " this sort of ", | |
# ("thing", ""), | |
# ". " | |
# )""" | |
# ํ๊ตญ์ด ํ๋กฌํํธ | |
task_description ="""๋น์ ์ Python์ st-annotated-text ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ํ ์ฃผ์์ด ๋ฌ๋ฆฐ ํ ์คํธ๋ฅผ ์์ฑํ๋ ์ ์ฉํ ๋์ฐ๋ฏธ์ ๋๋ค. ๋ณธ๋ฌธ์ ์ฃผ์ ๋ฅผ ํ์ ํ๊ณ ์๋ฏธ๋ฅผ ์ ๋ฌํ๋ ๋ฐ ํ์ํ ํต์ฌ ๋จ์ด๋ฅผ ๊ฐ์กฐํด์ผ ํฉ๋๋ค. ์ฃผ์ ํฌ์ธํธ๋ฅผ ํ์ ํ ์ ์์ด์ผ ํฉ๋๋ค. ๋ํ ์ ๊ณต๋ ํ ์คํธ์ ๋ฌธ๋จ์ ์ค์ฌ ๋ด์ฉ์ ๊ธฐ๋ฐ์ผ๋ก ๋จ์ด๋ฅผ ํ์ํ์ธ์. ์ถ๋ ฅ์ ๋ค์๊ณผ ๊ฐ์ ํ์์ผ๋ก ํฌ๋งท๋์ด์ผ ํฉ๋๋ค: | |
annotated_text( | |
("๋ฏผ์ฃผ์ฃผ์ ์ฌํ", ""), | |
" ๋ ๊ตญ๋ฏผ์ด ์ ์น์ ์ฐธ์ฌํ ๊ถ๋ฆฌ๋ฅผ ๋ณด์ฅํ๋ค. ๊ทธ๋ฌํ ๊ถ๋ฆฌ๋ฅผ", | |
("์ฐธ์ ๊ถ", ""), | |
"์ด๋ผ ํ๋๋ฐ, ์ด๋ ๊ธฐ๋ณธ์ ์ผ๋ก " | |
("์ ๊ฑฐ", ""), | |
"๋ก ์คํ๋๋ค. ", | |
("์ ๊ฑฐ", ""), | |
" ๋ ์ฌํ ์ง๋จ์ ๋ํ์๋ ๊ณต์ง์๋ค์ ์ ์ถํ์ฌ ๊ทธ๋ค์๊ฒ ", | |
("๋ํ์ฑ", ""), | |
" ์ ๋ถ์ฌํ๋ ํ์์ด๋ค. ๊ทธ๋ฌ๋ฏ๋ก ", | |
("๋์ ํฌํ์จ", ""), | |
" ๋ฏผ์ฃผ์ฃผ์์ ", | |
("์ ๋น์ฑ", ""), | |
" ํ๋ณด์ ๊น์ ๊ด๋ จ์ด ์๋ค ", | |
". " | |
) | |
""" | |
# user_prompt = f"Now, please annotate this text: {user_text}" | |
# #์ถ๋ ฅ ๊ด์ฐฎ๊ฒ ๋์ด1 | |
# task_description = """You are a helpful assistant that generates annotated text for the st-annotated-text library in Python. Your task is to highlight the key terms that are central to the overall topic or theme of the sentence. Your output should be formatted in the following way: | |
# annotated_text( | |
# "This ", | |
# ("is", ""), | |
# " some ", | |
# ("annotated", ""), | |
# ("text", ""), | |
# " for those of ", | |
# ("you", ""), | |
# " who ", | |
# ("like", ""), | |
# " this sort of ", | |
# ("thing", ""), | |
# ". " | |
# )""" | |
user_prompt = f"Based on the task description, please annotate the following text by highlighting the key terms central to its topic : {user_text}" | |
messages = [{"role": "system", "content": task_description}, {"role": "user", "content": user_prompt}] | |
response = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo-16k", | |
messages=messages, | |
temperature=1, | |
max_tokens=2500, | |
top_p=1, | |
frequency_penalty=0, | |
presence_penalty=0 | |
) | |
highlighted_text = response['choices'][0]['message']['content'] | |
# ์ฌ๊ธฐ์๋ ๊ฐ๋จํ๊ฒ exec ํจ์๋ฅผ ์ด์ฉํด GPT-3.5-turbo๊ฐ ์์ฑํ ์ฝ๋๋ฅผ ์คํํฉ๋๋ค. | |
# ์ค์ ํ๋ก๋์ ํ๊ฒฝ์์๋ ๋ณด์ ์ด์๋ฅผ ๊ณ ๋ คํด์ผ ํฉ๋๋ค. | |
exec(highlighted_text) | |
if __name__ == "__main__": | |
main() |