ranbac commited on
Commit
56d51db
·
verified ·
1 Parent(s): bbf5cce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -22
app.py CHANGED
@@ -1,55 +1,72 @@
1
  import gradio as gr
 
 
2
  from paddleocr import PaddleOCR
3
  from PIL import Image
4
  import numpy as np
5
- import logging
6
 
7
- # Tắt log debug của Paddle để gọn console
 
8
  logging.getLogger("ppocr").setLevel(logging.WARNING)
9
 
10
- print("Đang tải model PaddleOCR...")
 
 
 
 
 
11
 
12
- # CẤU HÌNH AN TOÀN NHẤT CHO CPU:
13
- # 1. Bỏ use_gpu=False (Paddle tự detect CPU)
14
- # 2. Thay use_angle_cls bằng tham số mới nếu cần, nhưng ở bản 2.7.3 thì use_angle_cls vẫn chạy tốt.
15
- # Tuy nhiên, để tránh lỗi "Unknown argument", ta khởi tạo đơn giản nhất.
16
  try:
17
- # Thử khởi tạo theo cách chuẩn
18
- ocr = PaddleOCR(use_angle_cls=True, lang='ch', show_log=False)
19
- except Exception as e:
20
- print(f"Lỗi khởi tạo lần 1: {e}")
21
- # Fallback nếu phiên bản thay đổi
22
- ocr = PaddleOCR(use_angle_cls=True, lang='ch', use_gpu=False, show_log=False)
 
 
 
 
 
 
 
23
 
24
- print("Đã tải model thành công!")
25
 
26
  def predict(image):
27
  if image is None:
28
- return "Vui lòng tải ảnh lên."
29
 
30
  try:
 
31
  if isinstance(image, Image.Image):
32
  image = np.array(image)
33
 
 
34
  result = ocr.ocr(image, cls=True)
35
 
36
  txts = []
37
  if result and result[0]:
38
  for line in result[0]:
39
  text = line[1][0]
40
- txts.append(text)
 
 
 
41
  return "\n".join(txts)
42
  else:
43
- return "Không tìm thấy văn bản nào."
 
44
  except Exception as e:
45
- return f"Đã xảy ra lỗi khi nhận dạng: {str(e)}"
46
 
 
47
  iface = gr.Interface(
48
  fn=predict,
49
- inputs=gr.Image(type="pil", label="Tải ảnh lên"),
50
- outputs=gr.Textbox(label="Kết quả nhận dạng", lines=10),
51
- title="PaddleOCR Tiếng Việt - CPU Mode",
52
- description="Chạy trên Docker (PaddleOCR v2.7.3 stable)",
53
  examples=[]
54
  )
55
 
 
1
  import gradio as gr
2
+ import logging
3
+ import os
4
  from paddleocr import PaddleOCR
5
  from PIL import Image
6
  import numpy as np
 
7
 
8
+ # 1. Cấu hình tắt log bằng code hệ thống (Thay vì truyền tham số show_log)
9
+ os.environ["CPP_MIN_LOG_LEVEL"] = "3"
10
  logging.getLogger("ppocr").setLevel(logging.WARNING)
11
 
12
+ print("Đang khởi tạo PaddleOCR (Chinese Model)...")
13
+
14
+ # 2. KHỞI TẠO MODEL (Đã sửa lỗi)
15
+ # - Bỏ show_log=False (Gây lỗi crash)
16
+ # - Bỏ use_gpu=False (Gây lỗi crash ở bản mới, Paddle tự nhận diện CPU)
17
+ # - Đổi use_angle_cls -> use_textline_orientation (Theo chuẩn mới)
18
 
 
 
 
 
19
  try:
20
+ # Cách khởi tạo chuẩn cho PaddleOCR v2.9+
21
+ ocr = PaddleOCR(
22
+ use_textline_orientation=True,
23
+ lang='ch'
24
+ )
25
+ except TypeError as e:
26
+ # Trường hợp dự phòng nếu thư viện thay đổi API bất ngờ
27
+ print(f"Cảnh báo khởi tạo: {e}. Chuyển sang chế độ tối giản.")
28
+ ocr = PaddleOCR(lang='ch')
29
+ except ValueError as e:
30
+ # Xử lý lỗi tham số lạ
31
+ print(f"Lỗi tham số: {e}. Đang thử lại không tham số phụ...")
32
+ ocr = PaddleOCR(lang='ch')
33
 
34
+ print("Model đã sẵn sàng hoạt động!")
35
 
36
  def predict(image):
37
  if image is None:
38
+ return "请上传图片 / Vui lòng tải ảnh lên."
39
 
40
  try:
41
+ # Chuyển đổi ảnh PIL sang Numpy array
42
  if isinstance(image, Image.Image):
43
  image = np.array(image)
44
 
45
+ # Thực hiện nhận dạng
46
  result = ocr.ocr(image, cls=True)
47
 
48
  txts = []
49
  if result and result[0]:
50
  for line in result[0]:
51
  text = line[1][0]
52
+ confidence = line[1][1]
53
+ # Lọc kết quả có độ tin cậy > 50%
54
+ if confidence > 0.5:
55
+ txts.append(text)
56
  return "\n".join(txts)
57
  else:
58
+ return "未发现文字 / Không tìm thấy văn bản."
59
+
60
  except Exception as e:
61
+ return f"Error / Lỗi hệ thống: {str(e)}"
62
 
63
+ # Giao diện Gradio
64
  iface = gr.Interface(
65
  fn=predict,
66
+ inputs=gr.Image(type="pil", label="上传图片 / Upload Image"),
67
+ outputs=gr.Textbox(label="结果 / Result", lines=15, show_copy_button=True),
68
+ title="PaddleOCR Chinese - CPU Fixed",
69
+ description="Phiên bản đã sửa lỗi tham số 'show_log'. Chạy ổn định trên Hugging Face CPU.",
70
  examples=[]
71
  )
72