asdf / app.py
tkdehf2's picture
Rename social exam to app.py
11f7f57 verified
raw
history blame
3.45 kB
import gradio as gr
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
import torch
from PIL import Image
import numpy as np
# OCR ๋ชจ๋ธ ๋ฐ ํ”„๋กœ์„ธ์„œ ์ดˆ๊ธฐํ™”
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
# Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ
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()