Spaces:
Sleeping
Sleeping
File size: 7,913 Bytes
71bfdcb 7a3d92d 829f184 7a3d92d cc9698d 22b626b 7a3d92d 86f45ae 7a3d92d f4de003 22b626b 829f184 c4bc644 7a3d92d 829f184 123222c 9447f48 7fc494d 123222c 9447f48 ed449f1 7c5c1ee ed449f1 7c5c1ee ed449f1 123222c 9447f48 123222c 9447f48 123222c 22b626b 71bfdcb 7a3d92d 71bfdcb 2c8f446 71bfdcb 32170e5 71bfdcb 829f184 71bfdcb 7a3d92d 71bfdcb bca9727 7a3d92d 71bfdcb 22b626b d4ebf2c 22b626b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
import streamlit as st
import openai
import json
from annotated_text import annotated_text
import os
import pandas as pd
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-4o-mini",
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-4o-mini",
messages=[
{
"role": "system",
"content": f"""λ€μ κΈ°μ€μ λ°λΌ '{base_sentence}'μ μ μ¬νμ§λ§ λ
μ°½μ μΈ νμ νκ° λ¬Έκ΅¬ 30κ°λ₯Ό μμ±ν΄ μ£ΌμΈμ:
1. μ£Όμ΄λ μλ΅νκ³ μμ μ΄λ‘ μμν©λλ€.
2. λ¬Έμ₯ λμ '~μ', '~ν¨', '~μ' λ± λ€μν μ’
κ²°μ΄λ―Έλ₯Ό μ¬μ©ν©λλ€.
3. μλ λ¬Έμ₯μ ν΅μ¬ μλ―Έλ μ μ§νλ, λ€μ μμλ€μ λ³ννμ¬ λ€μμ±μ λμ
λλ€:
- μ μ¬ν μλ―Έμ λ€λ₯Έ λ¨μ΄λ νν μ¬μ©
- λ¬Έμ₯ ꡬ쑰 λ³κ²½ (μ: λ₯λν/μλν μ ν)
- ꡬ체μ μΈ μμλ μν© μΆκ°
- λΆμ¬μ΄κ΅¬λ₯Ό μΆκ°νμ¬ μλ―Έ νμ₯
4. νμμ μ±μ₯κ³Ό λ°μ μ κΈμ μ μΌλ‘ ννν©λλ€.
5. κ΅κ³Ό κ΄λ ¨ μλ, μ°½μμ±, νλμ¬, 리λμ λ± λ€μν μΈ‘λ©΄μ κ³ λ €ν©λλ€.
6. νΉμ νλ
μ΄λ κ³Όλͺ©μ κ΅νλμ§ μλλ‘ μΌλ°νν©λλ€.
7. κ° λ¬Έκ΅¬λ μ΅μ 20μ μ΄μ, μ΅λ 100μ μ΄λ΄λ‘ μμ±ν©λλ€.
μμ±λ 문ꡬλ€μ μλ λ¬Έμ₯κ³Ό μ μ¬νλ©΄μλ μΆ©λΆν λ€μνκ³ λ
μ°½μ μ΄μ΄μΌ ν©λλ€."""
},
{
"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 'generated_result' not in st.session_state:
st.session_state.generated_result = None
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 st.button("νκ° μμ±"):
with st.spinner('λ΅λ³ μμ±μ€...'):
result = generate_annotated_text(achievement_standard)
st.session_state.generated_result = result
st.session_state.selected_sentence = None # μλ‘μ΄ νκ° μμ±μ μ νλ λ¬Έμ₯ μ΄κΈ°ν
st.session_state.similar_sentences = [] # μ΄μ μ μ¬ν λ¬Έμ₯λ€ μ΄κΈ°ν
exec(result.replace('```', ''))
# μ
λ νΈ λ°μ€ λ° μ μ¬ν λ¬Έμ₯ μμ± λ²νΌ μΆκ°
if st.session_state.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 = st.selectbox("λ¬Έμ₯μ μ ννμΈμ:", sentences, key="sentence_select")
st.session_state.selected_sentence = selected_sentence
# μ μ¬ν λ¬Έμ₯ μμ± λ²νΌ
if st.button("μ μ¬ν 문ꡬ μμ±") and st.session_state.selected_sentence:
with st.spinner('λ¬Έμ₯ μμ±μ€...'):
st.session_state.similar_sentences = generate_similar_sentences(st.session_state.selected_sentence)
# μ μ¬ν λ¬Έμ₯λ€ μΆλ ₯
for sentence in st.session_state.similar_sentences:
st.write(sentence)
# CSV νμΌλ‘ μ μ₯νλ λ²νΌ μΆκ°
if st.session_state.similar_sentences:
# λ°μ΄ν°νλ μ μμ±
df = pd.DataFrame(st.session_state.similar_sentences, columns=["Similar Sentences"])
# UTF-8-SIG (BOM) μΈμ½λ©μΌλ‘ CSV νμΌ λ³ν
csv = df.to_csv(index=False, encoding='utf-8-sig').encode('utf-8-sig')
# λ€μ΄λ‘λ λ²νΌ μμ±
st.download_button(
label="CSVλ‘ μ μ₯",
data=csv,
file_name='similar_sentences.csv',
mime='text/csv'
)
|