Spaces:
Sleeping
Sleeping
import streamlit as st | |
import openai | |
import json | |
from annotated_text import annotated_text | |
import os | |
import achivenment_standards as data | |
# OpenAI API ์ค์ (ํ๊ฒฝ ๋ณ์์์ ์ฝ์ด์ด) | |
openai.api_key = os.getenv("OPENAI_API_KEY") | |
#gpt์ด์ฉํด์ ์ถ๋ก ํจ์ ๋ง๋ค๊ธฐ | |
def generate_annotated_text(text): | |
response = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo-16k", | |
messages=[ | |
{ | |
"role": "system", | |
"content": "์ฑ์ทจ๊ธฐ์ค ๊ธฐ๋ฐ ํ์์ ํน์ฑ ๋ฐ ํ๋ ํ๊ฐ ์์ฑ\n์ฑ์ทจ๊ธฐ์ค์ ์ ๋ ฅํ์๋ฉด, ํด๋น ์ฑ์ทจ๊ธฐ์ค์ ๊ธฐ๋ฐํ ํ์์ ํน์ฑ ๋ฐ ํ๋์ ๋ํ ํ๊ฐ๋ฅผ annotated_text ํ์์ผ๋ก ์ ๊ณตํฉ๋๋ค. ์ฑ์ทจ๊ธฐ์ค์ ๋ณด๋ฉฐ ํ์์ ํน์ ํ๋, ์ฑ์ทจ ์์ค, ๊ต์ฌ์ ์ดํ, ๊ทธ๋ฆฌ๊ณ ํ์์ ์ญ๋์ ๊ณ ๋ คํ์ฌ ์ฒด๊ณ์ ์ผ๋ก ๊ตฌ์ฑ๋ ์ถ๋ ฅ์ ์ ๊ณตํฉ๋๋ค. ์ฃผ์ด๋ ๋ฐ๋์ ์๋ตํฉ๋๋ค. \n\n์์ :\n์ ๋ ฅ: ```์ฑ์ทจ๊ธฐ์ค: [6๊ตญ01-07]์๋๊ฐ ์ฒํ ์ํฉ์ ์ดํดํ๊ณ ๊ณต๊ฐํ๋ฉฐ ๋ฃ๋ ํ๋๋ฅผ ์ง๋๋ค, [6๊ตญ01-02] ์๊ฒฌ์ ์ ์ํ๊ณ ํจ๊ป ์กฐ์ ํ๋ฉฐ ํ ์ํ๋ค.```\n์ถ๋ ฅ: ```annotated_text(\n (\"ํ์ ์์ ์ ์๊ฐ์ ์ผ๋ชฉ์์ฐํ๊ฒ ์ ๋ฆฌํ๋ ์ต๊ด์ด ์์.\", \"์ญ๋\", \"rgba(255, 0, 0, 0.3)\"),\n (\"์ฌํ ํ์์ ๊ดํ ์ฃผ์ฅํ๋ ๊ธ์ฐ๊ธฐ๋ฅผ ์ํจ.\", \"์ฑ์ทจ์์ค\", \"rgba(0, 0, 255, 0.3)\"),\n (\"์น๊ตฌ์ ๊ณ ๋ฏผ์ ํด๊ฒฐํด์ฃผ๋ ์ญํ ๊ทน์์ ์๋๋ฐฉ์ ๋ฐฐ๋ คํ์ฌ ํด๊ฒฐ ๊ฐ๋ฅํ ๋ฐฉ์์ ์ ์ํจ.\", \"์ํ\", \"rgba(0, 128, 0, 0.3)\"),\n (\"์๋๊ฐ ์ฒํ ์ํฉ์ ์ดํดํ๊ณ ๊ณต๊ฐํ๋ ํ๋๋ฅผ ๊ฐ์ง๊ณ ์น๊ตฌ๋ค๊ณผ ์๋งํ ๊ด๊ณ๋ฅผ ๋งบ๊ณ ๊ฐ๋ฑ์ ์กฐ์ ํจ.\", \"๊ต์ฌ์ดํ\", \"rgba(128, 128, 128, 0.3)\"),\n (\"์ค๊ฐ ๋์ด ์๊ฐ์ ์ด๋์ฅ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ ์ ํ๊ธฐ๋ฅผ ์ฃผ์ ๋ก ํ ํ ์์์ ์๋ง์ ๊ทผ๊ฑฐ์ ๋ท๋ฐ์นจํ ์ ์๋ ์๋ฃ๋ฅผ ํ ๋๋ก ์์ ์ ์๊ฒฌ์ ํ๋นํ๊ฒ ์ ์ํ๋ฉด์ ๋ค๋ฅธ ์ฌ๋์ ์๊ฒฌ์ ๋ฅ๋์ ์ผ๋ก ์์ฉํ๊ณ ํจ๊ณผ์ ์ผ๋ก ์๊ฒฌ์ ์กฐ์ ํ๋ ๋ฅ๋ ฅ์ ๋ณด์.\", \"์ํ\", \"rgba(0, 128, 0, 0.3)\"),\n (\"์๋์ ์๊ฒฌ์ ์กด์คํ๊ณ ํ๋ ฅํ๋ ํ๋๋ฅผ ๋ณด์.\", \"์ญ๋\", \"rgba(255, 0, 0, 0.3)\")\n)\n```" | |
}, | |
{ | |
"role": "user", | |
"content": text | |
} | |
], | |
temperature=1, | |
max_tokens=10000, | |
top_p=1, | |
frequency_penalty=0, | |
presence_penalty=0 | |
) | |
return response['choices'][0]['message']['content'] | |
# ์ ์ฌํ ๋ฌธ์ฅ ์์ฑ ํจ์ | |
def generate_similar_sentences(base_sentence): | |
response = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo-16k", | |
messages=[ | |
{ | |
"role": "system", | |
"content": f"๋ค์์ '{base_sentence}'์ ์ ์ฌํ ํ์์ ํน์ฑ ๋ฐ ํ๋์ ๋ํ ํ๊ฐ ์์ ๋ฌธํญ 10๊ฐ๋ฅผ ๋ง๋ค์ด๋ผ. ๋ฌธ์ฅ์ ๋์ '~์,~ํจ,~์'์ผ๋ก ๋๋๋๋ก ํด์ค" | |
}, | |
{ | |
"role": "user", | |
"content": base_sentence | |
} | |
], | |
temperature=0.7, | |
max_tokens=10000, | |
top_p=1, | |
frequency_penalty=0, | |
presence_penalty=0 | |
) | |
generated_sentences = response.choices[0].message['content'].split('\n') | |
return [sentence.strip() for sentence in generated_sentences if sentence.strip()] | |
# Streamlit ์ฑ์ ์ ๋ชฉ ๋ฐ ์ค๋ช | |
st.title("์ฑ์ทจ๊ธฐ์ค ๊ธฐ๋ฐ ํ์์ ํน์ฑ ๋ฐ ํ๋ ํ๊ฐ ์์ฑ") | |
st.write("์ฑ์ทจ๊ธฐ์ค์ ์ ๋ ฅํ์๋ฉด, ํด๋น ์ฑ์ทจ๊ธฐ์ค์ ๊ธฐ๋ฐํ ํ์์ ํน์ฑ ๋ฐ ํ๋์ ๋ํ ํ๊ฐ๋ฅผ \n\n [ํ์ ํ๋, ์ฑ์ทจ ์์ค, ๊ต์ฌ์ ์ดํ, ํ์ ์ญ๋] 4๊ฐ์ง ์์๋ฅผ ์กฐํฉํ์ฌ ์ ๊ณตํฉ๋๋ค.") | |
# ์ฑ์ทจ๊ธฐ์ค ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ | |
achievement_standards = data.achievement_standards | |
# ํ๋ ๊ตฐ ์ ํ ๋๋กญ๋ค์ด | |
grade_group = st.selectbox("ํ๋ ๊ตฐ์ ์ ํํ์ธ์:", list(achievement_standards.keys())) | |
# ์ ํ๋ ํ๋ ๊ตฐ์ ๋ฐ๋ฅธ ๊ณผ๋ชฉ ๋ชฉ๋ก | |
subject_list = list(achievement_standards[grade_group].keys()) | |
subject = st.selectbox("๊ณผ๋ชฉ์ ์ ํํ์ธ์:", subject_list) | |
# ์ ํ๋ ๊ณผ๋ชฉ์ ๋ฐ๋ฅธ ์ฑ์ทจ๊ธฐ์ค ๋ชฉ๋ก | |
selected_standards = achievement_standards[grade_group][subject] | |
selected_standard = st.selectbox("์ฑ์ทจ๊ธฐ์ค์ ์ ํํ์ธ์:", selected_standards) | |
# ์ ํ๋ ์ฑ์ทจ๊ธฐ์ค์ ํ ์คํธ ์ ๋ ฅ์ฐฝ์ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ฌ์ฉ | |
achievement_standard = st.text_input("์ฑ์ทจ๊ธฐ์ค ์ ๋ ฅ:", value=selected_standard) | |
# ์ธ์ ์ํ ์ด๊ธฐํ | |
if 'selected_sentence' not in st.session_state: | |
st.session_state.selected_sentence = None | |
if 'similar_sentences' not in st.session_state: | |
st.session_state.similar_sentences = [] | |
if 'selected_index' not in st.session_state: | |
st.session_state.selected_index = 0 | |
# # "ํ๊ฐ ์์ฑ" ๋ฒํผ ํด๋ฆญ ์์ ๋์ | |
# if st.button("ํ๊ฐ ์์ฑ"): | |
# with st.spinner('๋ต๋ณ ์์ฑ์ค...'): | |
# result = generate_annotated_text(achievement_standard) | |
# # ๊ฒฐ๊ณผ ์ถ๋ ฅ | |
# exec(result.replace('```', '')) | |
# # annotated_text ๊ฒฐ๊ณผ์์ ๋ฌธ์ฅ๋ง ์ถ์ถ | |
# result_lines = result.split('\n') | |
# sentences = [] | |
# for line in result_lines: | |
# # "(" ๋ฌธ์์ด์ด ํฌํจ๋ ์์น๋ฅผ ์ฐพ์์ ๊ทธ ์ดํ์ ๋ฌธ์์ด๋ง ์ถ์ถ | |
# start_idx = line.find('("') | |
# if start_idx != -1: | |
# end_idx = line.find('",', start_idx) | |
# sentence = line[start_idx + 2:end_idx].strip() # "(" ๋ค์๋ถํฐ "," ์ ๊น์ง์ ๋ฌธ์์ด์ ์ถ์ถ | |
# sentences.append(sentence) | |
# # # ๋ฌธ์ฅ์ ๋ผ๋์ค ๋ฒํผ์ผ๋ก ํ์ ๋ฐ ์ ํ๋ ๋ฌธ์ฅ ์ ์ฅ | |
# # selected_index = st.radio("๋ฌธ์ฅ์ ์ ํํ์ธ์:", range(len(sentences)), format_func=lambda x: sentences[x]) | |
# # st.session_state.selected_sentence = sentences[selected_index] if sentences else None | |
# #๋๋ฒ๊น | |
# st.session_state.selected_index = st.radio("๋ฌธ์ฅ์ ์ ํํ์ธ์:", range(len(sentences)), format_func=lambda x: sentences[x], key="selected_index") | |
# # # ์ ์ฌํ ๋ฌธ์ฅ ์์ฑ ๋ฒํผ ์ถ๊ฐ | |
# # if st.button("์ ์ฌํ ๋ฌธ๊ตฌ ์์ฑ") and st.session_state.get('selected_sentence'): | |
# # with st.spinner('๋ฌธ์ฅ ์์ฑ์ค...'): | |
# # similar_sentences = generate_similar_sentences(st.session_state.selected_sentence) | |
# # for sentence in st.session_state.similar_sentences: | |
# # st.write(sentence) | |
# # ์ ์ฌํ ๋ฌธ์ฅ ์์ฑ ๋ฒํผ ์ถ๊ฐ | |
# if st.button("์ ์ฌํ ๋ฌธ๊ตฌ ์์ฑ"): | |
# if st.session_state.get('generated_result'): | |
# # annotated_text ๊ฒฐ๊ณผ์์ ๋ค์ ๋ฌธ์ฅ ์ถ์ถ | |
# result_lines = st.session_state.generated_result.split('\n') | |
# sentences = [line[start_idx + 2:line.find('",', start_idx)].strip() for line in result_lines if (start_idx := line.find('("')) != -1] | |
# if sentences: | |
# selected_sentence = sentences[st.session_state.selected_index] | |
# with st.spinner('๋ฌธ์ฅ ์์ฑ์ค...'): | |
# st.session_state.similar_sentences = generate_similar_sentences(selected_sentence) | |
# # ์์ฑ๋ ๊ฒฐ๊ณผ ๋ฐ ์ ์ฌํ ๋ฌธ์ฅ๋ค ์ถ๋ ฅ | |
# if st.session_state.get('generated_result'): | |
# exec(st.session_state.generated_result.replace('```', '')) | |
# for sentence in st.session_state.get('similar_sentences', []): | |
# st.write(sentence) | |
# "ํ๊ฐ ์์ฑ" ๋ฒํผ ํด๋ฆญ ์์ ๋์ | |
if st.button("ํ๊ฐ ์์ฑ"): | |
with st.spinner('๋ต๋ณ ์์ฑ์ค...'): | |
result = generate_annotated_text(achievement_standard) | |
st.session_state.generated_result = result | |
exec(result.replace('```', '')) | |
# annotated_text ๊ฒฐ๊ณผ์์ ๋ฌธ์ฅ๋ง ์ถ์ถ ๋ฐ ๋ฒํผ ์ถ๊ฐ | |
result_lines = result.split('\n') | |
for line in result_lines: | |
start_idx = line.find('("') | |
if start_idx != -1: | |
end_idx = line.find('",', start_idx) | |
sentence = line[start_idx + 2:end_idx].strip() | |
# ๊ฐ ๋ฌธ์ฅ ์์ ๋ฒํผ ์ถ๊ฐ | |
if st.button(f"์ ์ฌํ ๋ฌธ๊ตฌ ์์ฑ - '{sentence[:30]}...'", key=sentence): | |
with st.spinner('๋ฌธ์ฅ ์์ฑ์ค...'): | |
similar_sentences = generate_similar_sentences(sentence) | |
for generated_sentence in similar_sentences: | |
st.write(generated_sentence) | |
# ์์ฑ๋ ๊ฒฐ๊ณผ ๋ฐ ์ ์ฌํ ๋ฌธ์ฅ๋ค ์ถ๋ ฅ | |
if st.session_state.get('generated_result'): | |
exec(st.session_state.generated_result.replace('```', '')) |