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()