JUNGU's picture
Update app.py
2c4b32b verified
raw
history blame
5.77 kB
import gradio as gr
import openai
import json
import os
import pandas as pd
# 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()]
# μ„±μ·¨κΈ°μ€€ 데이터 κ°€μ Έμ˜€κΈ°
import achievement_standards as data
achievement_standards = data.achievement_standards
def get_subjects(grade_group):
return list(achievement_standards[grade_group].keys())
def get_standards(grade_group, subject):
return achievement_standards[grade_group][subject]
def generate_and_save_evaluation(grade_group, subject, standard):
result = generate_annotated_text(standard)
result_lines = result.split('\n')
sentences = [line[start_idx + 2:line.find('",', start_idx)].strip() for line in result_lines if (start_idx := line.find('("')) != -1]
df = pd.DataFrame(sentences, columns=["Evaluation"])
file_path = "/mnt/data/evaluations.csv"
df.to_csv(file_path, index=False)
return result, file_path
# Gradio μΈν„°νŽ˜μ΄μŠ€ μ •μ˜
with gr.Blocks() as demo:
gr.Markdown("### μ„±μ·¨κΈ°μ€€ 기반 ν•™μƒμ˜ νŠΉμ„± 및 ν™œλ™ 평가 생성")
grade_group = gr.Dropdown(label="학년ꡰ을 μ„ νƒν•˜μ„Έμš”:", choices=list(achievement_standards.keys()))
subject = gr.Dropdown(label="κ³Όλͺ©μ„ μ„ νƒν•˜μ„Έμš”:")
standard = gr.Dropdown(label="성취기쀀을 μ„ νƒν•˜μ„Έμš”:")
grade_group.change(fn=get_subjects, inputs=grade_group, outputs=subject)
subject.change(fn=get_standards, inputs=[grade_group, subject], outputs=standard)
with gr.Row():
generate_button = gr.Button("평가 생성")
result_output = gr.HTML()
similar_sentence_button = gr.Button("μœ μ‚¬ν•œ 문ꡬ 생성")
similar_sentences_output = gr.HTML()
save_button = gr.Button("CSV둜 μ €μž₯")
download_link = gr.File()
def update_similar_sentences(selected_sentence):
similar_sentences = generate_similar_sentences(selected_sentence)
return '<br>'.join(similar_sentences)
def save_evaluations_to_csv(evaluations):
df = pd.DataFrame(evaluations, columns=["Evaluation"])
file_path = "/mnt/data/evaluations.csv"
df.to_csv(file_path, index=False)
return file_path
selected_sentence = gr.Textbox(visible=False)
generate_button.click(fn=generate_and_save_evaluation, inputs=[grade_group, subject, standard], outputs=[result_output, download_link])
similar_sentence_button.click(fn=update_similar_sentences, inputs=selected_sentence, outputs=similar_sentences_output)
save_button.click(fn=save_evaluations_to_csv, inputs=result_output, outputs=download_link)
demo.launch()