Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,67 +2,86 @@ import gradio as gr
|
|
2 |
import pytesseract
|
3 |
from PIL import Image
|
4 |
|
5 |
-
# Tesseract κ²½λ‘ μ€μ
|
6 |
-
|
7 |
-
# λ‘컬 νκ²½μμ μ€ν μ, μλ κ²½λ‘λ₯Ό μμ μ Tesseract μ€μΉ κ²½λ‘λ‘ λ³κ²½νμΈμ.
|
8 |
-
pytesseract.pytesseract.tesseract_cmd = "/usr/bin/tesseract"
|
9 |
|
10 |
-
#
|
11 |
-
def
|
12 |
-
"""
|
13 |
-
μ
λ‘λλ μκΈμ¨ μ΄λ―Έμ§λ₯Ό OCRλ‘ ν
μ€νΈλ‘ λ³ννκ³ ,
|
14 |
-
μ±μ·¨κΈ°μ€μ λ°λΌ νκ° λ° νΌλλ°±μ μ 곡νλ ν¨μ.
|
15 |
-
"""
|
16 |
try:
|
17 |
# OCRλ‘ μ΄λ―Έμ§μμ ν
μ€νΈ μΆμΆ
|
18 |
extracted_text = pytesseract.image_to_string(image, lang="kor")
|
19 |
|
20 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
score = 0
|
22 |
feedback = []
|
23 |
-
|
24 |
-
|
25 |
-
if "νΌν© κ³μ°" in extracted_text:
|
26 |
feedback.append("νΌν© κ³μ° νμ΄κ° ν¬ν¨λμμ΅λλ€.")
|
27 |
score += 30
|
28 |
else:
|
29 |
feedback.append("νΌν© κ³μ° νμ΄κ° λλ½λμμ΅λλ€.")
|
30 |
|
31 |
-
if "μ½μμ λ°°μ" in
|
32 |
feedback.append("μ½μμ λ°°μ κ΄λ ¨ νμ΄κ° ν¬ν¨λμμ΅λλ€.")
|
33 |
score += 30
|
34 |
else:
|
35 |
feedback.append("μ½μμ λ°°μ κ΄λ ¨ νμ΄κ° λλ½λμμ΅λλ€.")
|
36 |
|
37 |
-
if "λΆμμ λ§μ
κ³Ό λΊμ
" in
|
38 |
feedback.append("λΆμ λλ μμ μ°μ°μ΄ ν¬ν¨λμμ΅λλ€.")
|
39 |
score += 40
|
40 |
else:
|
41 |
feedback.append("λΆμ λλ μμ μ°μ°μ΄ λλ½λμμ΅λλ€.")
|
42 |
|
43 |
-
# κ²°κ³Ό λ°ν
|
44 |
-
return
|
45 |
-
|
|
|
46 |
except Exception as e:
|
47 |
-
|
48 |
-
return "", 0, f"μ€λ₯ λ°μ: {str(e)}"
|
49 |
|
50 |
# Gradio μΈν°νμ΄μ€ ꡬμ±
|
51 |
with gr.Blocks() as demo:
|
52 |
-
gr.Markdown("#
|
53 |
|
54 |
-
with gr.
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
# μ ν리μΌμ΄μ
μ€ν
|
68 |
demo.launch()
|
|
|
2 |
import pytesseract
|
3 |
from PIL import Image
|
4 |
|
5 |
+
# Tesseract OCR κ²½λ‘ μ€μ
|
6 |
+
pytesseract.pytesseract.tesseract_cmd = "/usr/bin/tesseract" # Tesseract μ€μΉ κ²½λ‘
|
|
|
|
|
7 |
|
8 |
+
# 1λ¨κ³: μκΈμ¨ μ΄λ―Έμ§μμ ν
μ€νΈ μΆμΆ ν νμΌλ‘ μ μ₯
|
9 |
+
def extract_text(image):
|
|
|
|
|
|
|
|
|
10 |
try:
|
11 |
# OCRλ‘ μ΄λ―Έμ§μμ ν
μ€νΈ μΆμΆ
|
12 |
extracted_text = pytesseract.image_to_string(image, lang="kor")
|
13 |
|
14 |
+
# ν
μ€νΈλ₯Ό νμΌλ‘ μ μ₯
|
15 |
+
with open("output.txt", "w", encoding="utf-8") as file:
|
16 |
+
file.write(extracted_text)
|
17 |
+
|
18 |
+
return "ν
μ€νΈκ° μ±κ³΅μ μΌλ‘ μΆμΆλμ΄ 'output.txt'μ μ μ₯λμμ΅λλ€.", extracted_text
|
19 |
+
except Exception as e:
|
20 |
+
return f"μ€λ₯ λ°μ: {str(e)}", ""
|
21 |
+
|
22 |
+
# 2λ¨κ³: μ μ₯λ ν
μ€νΈ νμΌμ μ½κ³ νκ° μν
|
23 |
+
def evaluate_text():
|
24 |
+
try:
|
25 |
+
# ν
μ€νΈ νμΌ μ½κΈ°
|
26 |
+
with open("output.txt", "r", encoding="utf-8") as file:
|
27 |
+
text_content = file.read()
|
28 |
+
|
29 |
+
# κ°λ¨ν νκ° λ
Όλ¦¬ (μ: νΉμ ν€μλ ν¬ν¨ μ¬λΆ)
|
30 |
score = 0
|
31 |
feedback = []
|
32 |
+
|
33 |
+
if "νΌν© κ³μ°" in text_content:
|
|
|
34 |
feedback.append("νΌν© κ³μ° νμ΄κ° ν¬ν¨λμμ΅λλ€.")
|
35 |
score += 30
|
36 |
else:
|
37 |
feedback.append("νΌν© κ³μ° νμ΄κ° λλ½λμμ΅λλ€.")
|
38 |
|
39 |
+
if "μ½μμ λ°°μ" in text_content:
|
40 |
feedback.append("μ½μμ λ°°μ κ΄λ ¨ νμ΄κ° ν¬ν¨λμμ΅λλ€.")
|
41 |
score += 30
|
42 |
else:
|
43 |
feedback.append("μ½μμ λ°°μ κ΄λ ¨ νμ΄κ° λλ½λμμ΅λλ€.")
|
44 |
|
45 |
+
if "λΆμμ λ§μ
κ³Ό λΊμ
" in text_content or "μμμ κ³±μ
κ³Ό λλμ
" in text_content:
|
46 |
feedback.append("λΆμ λλ μμ μ°μ°μ΄ ν¬ν¨λμμ΅λλ€.")
|
47 |
score += 40
|
48 |
else:
|
49 |
feedback.append("λΆμ λλ μμ μ°μ°μ΄ λλ½λμμ΅λλ€.")
|
50 |
|
51 |
+
# κ²°κ³Ό λ°ν
|
52 |
+
return f"μ μ: {score}\nνΌλλ°±:\n" + "\n".join(feedback)
|
53 |
+
except FileNotFoundError:
|
54 |
+
return "ν
μ€νΈ νμΌμ΄ μ‘΄μ¬νμ§ μμ΅λλ€. λ¨Όμ μκΈμ¨λ₯Ό μΈμνμ¬ ν
μ€νΈλ₯Ό μ μ₯νμΈμ."
|
55 |
except Exception as e:
|
56 |
+
return f"μ€λ₯ λ°μ: {str(e)}"
|
|
|
57 |
|
58 |
# Gradio μΈν°νμ΄μ€ ꡬμ±
|
59 |
with gr.Blocks() as demo:
|
60 |
+
gr.Markdown("# μκΈμ¨ μν νμ΄ νκ° μμ€ν
")
|
61 |
|
62 |
+
with gr.Tab("1λ¨κ³: μκΈμ¨ μΈμ"):
|
63 |
+
with gr.Row():
|
64 |
+
image_input = gr.Image(label="μκΈμ¨ μ΄λ―Έμ§ μ
λ‘λ", type="pil")
|
65 |
+
output_message = gr.Textbox(label="κ²°κ³Ό λ©μμ§")
|
66 |
+
extracted_text = gr.Textbox(label="μΆμΆλ ν
μ€νΈ")
|
67 |
+
|
68 |
+
extract_button = gr.Button("ν
μ€νΈ μΆμΆ λ° μ μ₯")
|
69 |
+
extract_button.click(
|
70 |
+
extract_text,
|
71 |
+
inputs=image_input,
|
72 |
+
outputs=[output_message, extracted_text]
|
73 |
+
)
|
74 |
+
|
75 |
+
with gr.Tab("2λ¨κ³: ν
μ€νΈ νκ°"):
|
76 |
+
with gr.Row():
|
77 |
+
evaluation_result = gr.Textbox(label="νκ° κ²°κ³Ό")
|
78 |
+
|
79 |
+
evaluate_button = gr.Button("νκ° μ€ν")
|
80 |
+
evaluate_button.click(
|
81 |
+
evaluate_text,
|
82 |
+
inputs=None,
|
83 |
+
outputs=evaluation_result
|
84 |
+
)
|
85 |
|
86 |
# μ ν리μΌμ΄μ
μ€ν
|
87 |
demo.launch()
|