math_assessment / app.py
tkdehf2's picture
Update app.py
c9a2f34 verified
raw
history blame
2.64 kB
import gradio as gr
import pytesseract
from PIL import Image
# Tesseract 경둜 μ„€μ •
# Hugging Face Spacesμ—μ„œλŠ” 기본적으둜 "/usr/bin/tesseract" κ²½λ‘œμ— μ„€μΉ˜λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.
# 둜컬 ν™˜κ²½μ—μ„œ μ‹€ν–‰ μ‹œ, μ•„λž˜ 경둜λ₯Ό μžμ‹ μ˜ Tesseract μ„€μΉ˜ 경둜둜 λ³€κ²½ν•˜μ„Έμš”.
pytesseract.pytesseract.tesseract_cmd = "/usr/bin/tesseract"
# 평가 ν•¨μˆ˜ μ •μ˜
def evaluate_solution(image):
"""
μ—…λ‘œλ“œλœ 손글씨 이미지λ₯Ό OCR둜 ν…μŠ€νŠΈλ‘œ λ³€ν™˜ν•˜κ³ ,
성취기쀀에 따라 평가 및 ν”Όλ“œλ°±μ„ μ œκ³΅ν•˜λŠ” ν•¨μˆ˜.
"""
try:
# OCR둜 μ΄λ―Έμ§€μ—μ„œ ν…μŠ€νŠΈ μΆ”μΆœ
extracted_text = pytesseract.image_to_string(image, lang="kor")
# 초기 μ μˆ˜μ™€ ν”Όλ“œλ°± μ„€μ •
score = 0
feedback = []
# μ˜ˆμ‹œ: 6ν•™λ…„ μˆ˜ν•™ μ„±μ·¨κΈ°μ€€ 기반 평가 (ν˜Όν•© 계산, μ•½μˆ˜μ™€ 배수, λΆ„μˆ˜μ™€ μ†Œμˆ˜ μ—°μ‚°)
if "ν˜Όν•© 계산" in extracted_text:
feedback.append("ν˜Όν•© 계산 풀이가 ν¬ν•¨λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
score += 30
else:
feedback.append("ν˜Όν•© 계산 풀이가 λˆ„λ½λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
if "μ•½μˆ˜μ™€ 배수" in extracted_text:
feedback.append("μ•½μˆ˜μ™€ 배수 κ΄€λ ¨ 풀이가 ν¬ν•¨λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
score += 30
else:
feedback.append("μ•½μˆ˜μ™€ 배수 κ΄€λ ¨ 풀이가 λˆ„λ½λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
if "λΆ„μˆ˜μ˜ λ§μ…ˆκ³Ό λΊ„μ…ˆ" in extracted_text or "μ†Œμˆ˜μ˜ κ³±μ…ˆκ³Ό λ‚˜λˆ—μ…ˆ" in extracted_text:
feedback.append("λΆ„μˆ˜ λ˜λŠ” μ†Œμˆ˜ 연산이 ν¬ν•¨λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
score += 40
else:
feedback.append("λΆ„μˆ˜ λ˜λŠ” μ†Œμˆ˜ 연산이 λˆ„λ½λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
# κ²°κ³Ό λ°˜ν™˜ (ν…μŠ€νŠΈ, 점수, ν”Όλ“œλ°±)
return extracted_text, score, "\n".join(feedback)
except Exception as e:
# 였λ₯˜ λ°œμƒ μ‹œ κΈ°λ³Έκ°’ λ°˜ν™˜
return "", 0, f"였λ₯˜ λ°œμƒ: {str(e)}"
# Gradio μΈν„°νŽ˜μ΄μŠ€ ꡬ성
with gr.Blocks() as demo:
gr.Markdown("# μ΄ˆλ“±ν•™κ΅ 6ν•™λ…„ μˆ˜ν•™ 문제 풀이 평가 μ‹œμŠ€ν…œ")
with gr.Row():
image_input = gr.Image(label="손글씨 이미지 μ—…λ‘œλ“œ", type="pil")
output_text = gr.Textbox(label="μΆ”μΆœλœ ν…μŠ€νŠΈ")
output_score = gr.Number(label="점수")
output_feedback = gr.Textbox(label="ν”Όλ“œλ°±")
submit_button = gr.Button("ν‰κ°€ν•˜κΈ°")
submit_button.click(
evaluate_solution,
inputs=image_input,
outputs=[output_text, output_score, output_feedback]
)
# μ• ν”Œλ¦¬μΌ€μ΄μ…˜ μ‹€ν–‰
demo.launch()