Spaces:
Sleeping
Sleeping
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() | |