|
import gradio as gr |
|
from transformers import TrOCRProcessor, VisionEncoderDecoderModel |
|
import torch |
|
from PIL import Image |
|
import numpy as np |
|
|
|
|
|
processor = TrOCRProcessor.from_pretrained('microsoft/trocr-base-handwritten') |
|
model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-base-handwritten') |
|
|
|
|
|
answer_key = { |
|
"1": { |
|
"answer": "๋ฏผ์ฃผ์ฃผ์", |
|
"explanation": "๋ฏผ์ฃผ์ฃผ์๋ ๊ตญ๋ฏผ์ด ์ฃผ์ธ์ด ๋์ด ๋๋ผ์ ์ค์ํ ์ผ์ ๊ฒฐ์ ํ๋ ์ ๋์
๋๋ค. ์ฐ๋ฆฌ๋๋ผ๋ ๋ฏผ์ฃผ์ฃผ์ ๊ตญ๊ฐ๋ก, ๊ตญ๋ฏผ๋ค์ด ํฌํ๋ฅผ ํตํด ๋ํ์๋ฅผ ์ ์ถํ๊ณ ์ค์ํ ๊ฒฐ์ ์ ์ฐธ์ฌํฉ๋๋ค." |
|
}, |
|
"2": { |
|
"answer": "์ผ๊ถ๋ถ๋ฆฝ", |
|
"explanation": "์ผ๊ถ๋ถ๋ฆฝ์ ์
๋ฒ๋ถ, ํ์ ๋ถ, ์ฌ๋ฒ๋ถ๋ก ๊ถ๋ ฅ์ ๋๋์ด ์๋ก ๊ฒฌ์ ์ ๊ท ํ์ ์ด๋ฃจ๊ฒ ํ๋ ์ ๋์
๋๋ค. ์ด๋ฅผ ํตํด ํ ์ชฝ์ ๊ถ๋ ฅ์ด ์ง์ค๋๋ ๊ฒ์ ๋ง์ ์ ์์ต๋๋ค." |
|
}, |
|
"3": { |
|
"answer": "์ง๋ฐฉ์์น์ ๋", |
|
"explanation": "์ง๋ฐฉ์์น์ ๋๋ ์ง์ญ์ ์ผ์ ๊ทธ ์ง์ญ ์ฃผ๋ฏผ๋ค์ด ์ง์ ๊ฒฐ์ ํ๊ณ ์ฒ๋ฆฌํ๋ ์ ๋์
๋๋ค. ์ฃผ๋ฏผ๋ค์ด ์ง์ ์ง๋ฐฉ์์น๋จ์ฒด์ฅ๊ณผ ์ง๋ฐฉ์ํ ์์์ ์ ์ถํฉ๋๋ค." |
|
} |
|
} |
|
|
|
def preprocess_image(image): |
|
"""์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ ํจ์""" |
|
if isinstance(image, np.ndarray): |
|
image = Image.fromarray(image) |
|
return image |
|
|
|
def recognize_text(image): |
|
"""์๊ธ์จ ์ธ์ ํจ์""" |
|
image = preprocess_image(image) |
|
pixel_values = processor(image, return_tensors="pt").pixel_values |
|
|
|
with torch.no_grad(): |
|
generated_ids = model.generate(pixel_values) |
|
|
|
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] |
|
return generated_text |
|
|
|
def grade_answer(question_number, student_answer): |
|
"""๋ต์ ์ฑ์ ํจ์""" |
|
correct_answer = answer_key[question_number]["answer"] |
|
explanation = answer_key[question_number]["explanation"] |
|
|
|
|
|
is_correct = student_answer.replace(" ", "").lower() == correct_answer.replace(" ", "").lower() |
|
|
|
return { |
|
"์ ๋ต ์ฌ๋ถ": "์ ๋ต" if is_correct else "์ค๋ต", |
|
"์ ๋ต": correct_answer, |
|
"ํด์ค": explanation |
|
} |
|
|
|
def process_submission(image, question_number): |
|
"""์ ์ฒด ์ฒ๋ฆฌ ํจ์""" |
|
if not image or not question_number: |
|
return "์ด๋ฏธ์ง์ ๋ฌธ์ ๋ฒํธ๋ฅผ ๋ชจ๋ ์
๋ ฅํด์ฃผ์ธ์." |
|
|
|
|
|
recognized_text = recognize_text(image) |
|
|
|
|
|
result = grade_answer(question_number, recognized_text) |
|
|
|
|
|
output = f""" |
|
์ธ์๋ ๋ต์: {recognized_text} |
|
์ฑ์ ๊ฒฐ๊ณผ: {result['์ ๋ต ์ฌ๋ถ']} |
|
์ ๋ต: {result['์ ๋ต']} |
|
|
|
[ํด์ค] |
|
{result['ํด์ค']} |
|
""" |
|
|
|
return output |
|
|
|
|
|
iface = gr.Interface( |
|
fn=process_submission, |
|
inputs=[ |
|
gr.Image(label="๋ต์ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ์ธ์", type="numpy"), |
|
gr.Dropdown(choices=["1", "2", "3"], label="๋ฌธ์ ๋ฒํธ๋ฅผ ์ ํํ์ธ์") |
|
], |
|
outputs=gr.Textbox(label="์ฑ์ ๊ฒฐ๊ณผ"), |
|
title="์ด๋ฑํ๊ต ์ฌํ ์ํ์ง ์ฑ์ ํ๋ก๊ทธ๋จ", |
|
description="์๊ธ์จ๋ก ์์ฑ๋ ์ฌํ ์ํ ๋ต์์ ์ฑ์ ํ๊ณ ํด์ค์ ์ ๊ณตํ๋ ํ๋ก๊ทธ๋จ์
๋๋ค.", |
|
) |
|
|
|
if __name__ == "__main__": |
|
iface.launch() |