Spaces:
Running
Running
File size: 5,766 Bytes
2c4b32b 829f184 cc9698d 2c4b32b f4de003 829f184 2c4b32b 829f184 f4de003 829f184 123222c 9447f48 123222c 9447f48 c932ada 123222c 9447f48 123222c 9447f48 123222c 829f184 2c8f446 2c4b32b 2c8f446 2c4b32b 32170e5 2c4b32b 829f184 2c4b32b bca9727 2c4b32b bca9727 2c4b32b bca9727 2c4b32b bca9727 2c4b32b |
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 |
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()
|