tkdehf2 commited on
Commit
4822af3
β€’
1 Parent(s): c9a2f34

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -34
app.py CHANGED
@@ -2,67 +2,86 @@ import gradio as gr
2
  import pytesseract
3
  from PIL import Image
4
 
5
- # Tesseract 경둜 μ„€μ •
6
- # Hugging Face Spacesμ—μ„œλŠ” 기본적으둜 "/usr/bin/tesseract" κ²½λ‘œμ— μ„€μΉ˜λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.
7
- # 둜컬 ν™˜κ²½μ—μ„œ μ‹€ν–‰ μ‹œ, μ•„λž˜ 경둜λ₯Ό μžμ‹ μ˜ Tesseract μ„€μΉ˜ 경둜둜 λ³€κ²½ν•˜μ„Έμš”.
8
- pytesseract.pytesseract.tesseract_cmd = "/usr/bin/tesseract"
9
 
10
- # 평가 ν•¨μˆ˜ μ •μ˜
11
- def evaluate_solution(image):
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
- # μ˜ˆμ‹œ: 6ν•™λ…„ μˆ˜ν•™ μ„±μ·¨κΈ°μ€€ 기반 평가 (ν˜Όν•© 계산, μ•½μˆ˜μ™€ 배수, λΆ„μˆ˜μ™€ μ†Œμˆ˜ μ—°μ‚°)
25
- if "ν˜Όν•© 계산" in extracted_text:
26
  feedback.append("ν˜Όν•© 계산 풀이가 ν¬ν•¨λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
27
  score += 30
28
  else:
29
  feedback.append("ν˜Όν•© 계산 풀이가 λˆ„λ½λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
30
 
31
- if "μ•½μˆ˜μ™€ 배수" in extracted_text:
32
  feedback.append("μ•½μˆ˜μ™€ 배수 κ΄€λ ¨ 풀이가 ν¬ν•¨λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
33
  score += 30
34
  else:
35
  feedback.append("μ•½μˆ˜μ™€ 배수 κ΄€λ ¨ 풀이가 λˆ„λ½λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
36
 
37
- if "λΆ„μˆ˜μ˜ λ§μ…ˆκ³Ό λΊ„μ…ˆ" in extracted_text or "μ†Œμˆ˜μ˜ κ³±μ…ˆκ³Ό λ‚˜λˆ—μ…ˆ" in extracted_text:
38
  feedback.append("λΆ„μˆ˜ λ˜λŠ” μ†Œμˆ˜ 연산이 ν¬ν•¨λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
39
  score += 40
40
  else:
41
  feedback.append("λΆ„μˆ˜ λ˜λŠ” μ†Œμˆ˜ 연산이 λˆ„λ½λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
42
 
43
- # κ²°κ³Ό λ°˜ν™˜ (ν…μŠ€νŠΈ, 점수, ν”Όλ“œλ°±)
44
- return extracted_text, score, "\n".join(feedback)
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("# μ΄ˆλ“±ν•™κ΅ 6ν•™λ…„ μˆ˜ν•™ 문제 풀이 평가 μ‹œμŠ€ν…œ")
53
 
54
- with gr.Row():
55
- image_input = gr.Image(label="손글씨 이미지 μ—…λ‘œλ“œ", type="pil")
56
- output_text = gr.Textbox(label="μΆ”μΆœλœ ν…μŠ€νŠΈ")
57
- output_score = gr.Number(label="점수")
58
- output_feedback = gr.Textbox(label="ν”Όλ“œλ°±")
59
-
60
- submit_button = gr.Button("ν‰κ°€ν•˜κΈ°")
61
- submit_button.click(
62
- evaluate_solution,
63
- inputs=image_input,
64
- outputs=[output_text, output_score, output_feedback]
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()