math_assessment / app.py
tkdehf2's picture
Update app.py
4822af3 verified
import gradio as gr
import pytesseract
from PIL import Image
# Tesseract OCR 경둜 μ„€μ •
pytesseract.pytesseract.tesseract_cmd = "/usr/bin/tesseract" # Tesseract μ„€μΉ˜ 경둜
# 1단계: 손글씨 μ΄λ―Έμ§€μ—μ„œ ν…μŠ€νŠΈ μΆ”μΆœ ν›„ 파일둜 μ €μž₯
def extract_text(image):
try:
# OCR둜 μ΄λ―Έμ§€μ—μ„œ ν…μŠ€νŠΈ μΆ”μΆœ
extracted_text = pytesseract.image_to_string(image, lang="kor")
# ν…μŠ€νŠΈλ₯Ό 파일둜 μ €μž₯
with open("output.txt", "w", encoding="utf-8") as file:
file.write(extracted_text)
return "ν…μŠ€νŠΈκ°€ μ„±κ³΅μ μœΌλ‘œ μΆ”μΆœλ˜μ–΄ 'output.txt'에 μ €μž₯λ˜μ—ˆμŠ΅λ‹ˆλ‹€.", extracted_text
except Exception as e:
return f"였λ₯˜ λ°œμƒ: {str(e)}", ""
# 2단계: μ €μž₯된 ν…μŠ€νŠΈ νŒŒμΌμ„ 읽고 평가 μˆ˜ν–‰
def evaluate_text():
try:
# ν…μŠ€νŠΈ 파일 읽기
with open("output.txt", "r", encoding="utf-8") as file:
text_content = file.read()
# κ°„λ‹¨ν•œ 평가 논리 (예: νŠΉμ • ν‚€μ›Œλ“œ 포함 μ—¬λΆ€)
score = 0
feedback = []
if "ν˜Όν•© 계산" in text_content:
feedback.append("ν˜Όν•© 계산 풀이가 ν¬ν•¨λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
score += 30
else:
feedback.append("ν˜Όν•© 계산 풀이가 λˆ„λ½λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
if "μ•½μˆ˜μ™€ 배수" in text_content:
feedback.append("μ•½μˆ˜μ™€ 배수 κ΄€λ ¨ 풀이가 ν¬ν•¨λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
score += 30
else:
feedback.append("μ•½μˆ˜μ™€ 배수 κ΄€λ ¨ 풀이가 λˆ„λ½λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
if "λΆ„μˆ˜μ˜ λ§μ…ˆκ³Ό λΊ„μ…ˆ" in text_content or "μ†Œμˆ˜μ˜ κ³±μ…ˆκ³Ό λ‚˜λˆ—μ…ˆ" in text_content:
feedback.append("λΆ„μˆ˜ λ˜λŠ” μ†Œμˆ˜ 연산이 ν¬ν•¨λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
score += 40
else:
feedback.append("λΆ„μˆ˜ λ˜λŠ” μ†Œμˆ˜ 연산이 λˆ„λ½λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
# κ²°κ³Ό λ°˜ν™˜
return f"점수: {score}\nν”Όλ“œλ°±:\n" + "\n".join(feedback)
except FileNotFoundError:
return "ν…μŠ€νŠΈ 파일이 μ‘΄μž¬ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. λ¨Όμ € 손글씨λ₯Ό μΈμ‹ν•˜μ—¬ ν…μŠ€νŠΈλ₯Ό μ €μž₯ν•˜μ„Έμš”."
except Exception as e:
return f"였λ₯˜ λ°œμƒ: {str(e)}"
# Gradio μΈν„°νŽ˜μ΄μŠ€ ꡬ성
with gr.Blocks() as demo:
gr.Markdown("# 손글씨 μˆ˜ν•™ 풀이 평가 μ‹œμŠ€ν…œ")
with gr.Tab("1단계: 손글씨 인식"):
with gr.Row():
image_input = gr.Image(label="손글씨 이미지 μ—…λ‘œλ“œ", type="pil")
output_message = gr.Textbox(label="κ²°κ³Ό λ©”μ‹œμ§€")
extracted_text = gr.Textbox(label="μΆ”μΆœλœ ν…μŠ€νŠΈ")
extract_button = gr.Button("ν…μŠ€νŠΈ μΆ”μΆœ 및 μ €μž₯")
extract_button.click(
extract_text,
inputs=image_input,
outputs=[output_message, extracted_text]
)
with gr.Tab("2단계: ν…μŠ€νŠΈ 평가"):
with gr.Row():
evaluation_result = gr.Textbox(label="평가 κ²°κ³Ό")
evaluate_button = gr.Button("평가 μ‹€ν–‰")
evaluate_button.click(
evaluate_text,
inputs=None,
outputs=evaluation_result
)
# μ• ν”Œλ¦¬μΌ€μ΄μ…˜ μ‹€ν–‰
demo.launch()