JUNGU's picture
Update app.py
123222c
raw
history blame
5.8 kB
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}'와 μœ μ‚¬ν•œ 30개의 λ¬Έμž₯을 μƒμ„±ν•©λ‹ˆλ‹€. 각 λ¬Έμž₯은 독립적이고 μœ μ‚¬ν•œ λ‚΄μš©μ„ λ‹΄κ³  μžˆμ–΄μ•Ό ν•©λ‹ˆλ‹€:"
},
{
"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 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
# μœ μ‚¬ν•œ λ¬Έμž₯ 생성 λ²„νŠΌ μΆ”κ°€
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 similar_sentences:
st.write(sentence)