orftxd / app.py
openfree's picture
Update app.py
df12f45 verified
import gradio as gr
import random
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import io
def generate_math_content(education_level, math_field, specific_topic, difficulty):
# ๋ฌธ์ œ ์€ํ–‰ (์‹ค์ œ ๊ตฌํ˜„์‹œ ๋” ๋งŽ์€ ๋ฌธ์ œ ์ถ”๊ฐ€ ํ•„์š”)
problems = {
"์ค‘ํ•™๊ต": {
"๋Œ€์ˆ˜": {
"์ผ์ฐจ๋ฐฉ์ •์‹": [
{
"basic": "x + 5 = 12 ๋ฅผ ํ’€์–ด๋ณด์„ธ์š”.",
"medium": "2x + 7 = 15 ๋ฅผ ํ’€์–ด๋ณด์„ธ์š”.",
"advanced": "3x + 4 = 2x - 8 ์„ ํ’€์–ด๋ณด์„ธ์š”."
}
]
},
"๊ธฐํ•˜": {
"์‚ผ๊ฐํ˜•": [
{
"basic": "๋ฐ‘๋ณ€์ด 6cm, ๋†’์ด๊ฐ€ 4cm์ธ ์‚ผ๊ฐํ˜•์˜ ๋„“์ด๋ฅผ ๊ตฌํ•˜์„ธ์š”.",
"medium": "์„ธ ๋ณ€์˜ ๊ธธ์ด๊ฐ€ 3, 4, 5์ธ ์‚ผ๊ฐํ˜•์˜ ๋„“์ด๋ฅผ ๊ตฌํ•˜์„ธ์š”.",
"advanced": "๋‘ ๋ณ€์˜ ๊ธธ์ด๊ฐ€ 5cm, 8cm์ด๊ณ  ๊ทธ ์‚ฌ์ด๊ฐ์ด 60ยฐ์ธ ์‚ผ๊ฐํ˜•์˜ ๋„“์ด๋ฅผ ๊ตฌํ•˜์„ธ์š”."
}
]
}
}
}
# ํ•ด์„ค ์ƒ์„ฑ
def create_explanation():
explanations = {
"basic": "๊ธฐ๋ณธ ๊ฐœ๋…์„ ์ด์šฉํ•œ ๋‹จ๊ณ„๋ณ„ ํ’€์ด:\n1. ์ฃผ์–ด์ง„ ์‹์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.\n2. ๊ธฐ๋ณธ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.\n3. ๊ฒฐ๊ณผ๋ฅผ ๊ฒ€์ฆํ•ฉ๋‹ˆ๋‹ค.",
"medium": "์ค‘๊ธ‰ ๋‚œ์ด๋„ ํ’€์ด ์ „๋žต:\n1. ๋ฌธ์ œ ๋ถ„์„\n2. ์ ์šฉํ•  ๊ณต์‹ ์„ ํƒ\n3. ๊ณ„์‚ฐ ์ˆ˜ํ–‰\n4. ๊ฒฐ๊ณผ ๊ฒ€์ฆ",
"advanced": "๊ณ ๊ธ‰ ๋ฌธ์ œ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•:\n1. ๋ฌธ์ œ์˜ ์กฐ๊ฑด ๋ถ„์„\n2. ํ•ด๊ฒฐ ์ „๋žต ์ˆ˜๋ฆฝ\n3. ๋‹จ๊ณ„๋ณ„ ๊ณ„์‚ฐ\n4. ๊ฒฐ๊ณผ์˜ ํƒ€๋‹น์„ฑ ๊ฒ€ํ† "
}
return explanations[difficulty]
# ์‹œ๊ฐํ™” ์ƒ์„ฑ
def create_visualization():
fig, ax = plt.subplots(figsize=(8, 6))
if math_field == "๋Œ€์ˆ˜":
x = np.linspace(-10, 10, 100)
y = x + 5
ax.plot(x, y)
ax.grid(True)
ax.axhline(y=0, color='k')
ax.axvline(x=0, color='k')
plt.title("์ผ์ฐจ๋ฐฉ์ •์‹์˜ ๊ทธ๋ž˜ํ”„")
elif math_field == "๊ธฐํ•˜":
points = np.array([[0, 0], [6, 0], [3, 4]])
triangle = plt.Polygon(points, fill=False)
ax.add_patch(triangle)
ax.set_xlim(-2, 8)
ax.set_ylim(-2, 6)
plt.title("์‚ผ๊ฐํ˜•")
buf = io.BytesIO()
plt.savefig(buf, format='png')
buf.seek(0)
return Image.open(buf)
# ์—ฐ์Šต๋ฌธ์ œ ์ƒ์„ฑ
practice_problems = [
"์—ฐ์Šต๋ฌธ์ œ 1: ๊ธฐ๋ณธ ๊ฐœ๋… ํ™•์ธ",
"์—ฐ์Šต๋ฌธ์ œ 2: ์‘์šฉ๋ ฅ ํ…Œ์ŠคํŠธ",
"์—ฐ์Šต๋ฌธ์ œ 3: ์‹ฌํ™” ๋ฌธ์ œ"
]
# ์ž๊ฐ€์ง„๋‹จ ์ฒดํฌ๋ฆฌ์ŠคํŠธ
checklist = [
"โ–ก ํ•ต์‹ฌ ๊ฐœ๋…์„ ์ดํ•ดํ–ˆ๋‚˜์š”?",
"โ–ก ๋ฌธ์ œ ํ•ด๊ฒฐ ๊ณผ์ •์„ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋‚˜์š”?",
"โ–ก ๋น„์Šทํ•œ ์œ ํ˜•์˜ ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ๋‚˜์š”?",
"โ–ก ์‹ค์ƒํ™œ ์ ์šฉ ์‚ฌ๋ก€๋ฅผ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‚˜์š”?"
]
problem = problems.get(education_level, {}).get(math_field, {}).get(specific_topic, [{}])[0].get(difficulty, "๋ฌธ์ œ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
explanation = create_explanation()
visualization = create_visualization()
return problem, explanation, visualization, "\n".join(practice_problems), "\n".join(checklist)
def create_interface():
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
education_level = gr.Dropdown(
choices=["์ค‘ํ•™๊ต", "๊ณ ๋“ฑํ•™๊ต", "๋Œ€ํ•™๊ต"],
label="๊ต์œก๊ณผ์ • ๋‹จ๊ณ„",
value="์ค‘ํ•™๊ต"
)
math_field = gr.Dropdown(
choices=["๋Œ€์ˆ˜", "๊ธฐํ•˜", "ํ•ด์„ํ•™"],
label="์ˆ˜ํ•™ ์˜์—ญ",
value="๋Œ€์ˆ˜"
)
specific_topic = gr.Dropdown(
choices=["์ผ์ฐจ๋ฐฉ์ •์‹", "์‚ผ๊ฐํ˜•"],
label="์„ธ๋ถ€ ์ฃผ์ œ",
value="์ผ์ฐจ๋ฐฉ์ •์‹"
)
difficulty = gr.Dropdown(
choices=["basic", "medium", "advanced"],
label="๋‚œ์ด๋„",
value="basic"
)
generate_btn = gr.Button("๋ฌธ์ œ ์ƒ์„ฑ")
with gr.Column():
problem_output = gr.Textbox(label="์ƒ์„ฑ๋œ ๋ฌธ์ œ")
explanation_output = gr.Textbox(label="์ƒ์„ธ ํ’€์ด")
visualization_output = gr.Image(label="์‹œ๊ฐํ™”")
practice_output = gr.Textbox(label="์—ฐ์Šต๋ฌธ์ œ")
checklist_output = gr.Textbox(label="์ž๊ฐ€์ง„๋‹จ ์ฒดํฌ๋ฆฌ์ŠคํŠธ")
generate_btn.click(
generate_math_content,
inputs=[education_level, math_field, specific_topic, difficulty],
outputs=[problem_output, explanation_output, visualization_output, practice_output, checklist_output]
)
return demo
demo = create_interface()
if __name__ == '__main__':
demo = create_interface() # demo ๋ณ€์ˆ˜ ์ •์˜
demo.launch() # ์‹คํ–‰