| import gradio as gr |
| import logging |
| import os |
| from paddleocr import PaddleOCR |
| from PIL import Image |
| import numpy as np |
|
|
| |
| os.environ["CPP_MIN_LOG_LEVEL"] = "3" |
| logging.getLogger("ppocr").setLevel(logging.WARNING) |
|
|
| print("Đang khởi tạo PaddleOCR (Chinese Model)...") |
|
|
| |
| |
| |
| |
|
|
| try: |
| |
| ocr = PaddleOCR( |
| use_textline_orientation=True, |
| lang='ch' |
| ) |
| except TypeError as e: |
| |
| print(f"Cảnh báo khởi tạo: {e}. Chuyển sang chế độ tối giản.") |
| ocr = PaddleOCR(lang='ch') |
| except ValueError as e: |
| |
| print(f"Lỗi tham số: {e}. Đang thử lại không tham số phụ...") |
| ocr = PaddleOCR(lang='ch') |
|
|
| print("Model đã sẵn sàng hoạt động!") |
|
|
| def predict(image): |
| if image is None: |
| return "请上传图片 / Vui lòng tải ảnh lên." |
| |
| try: |
| |
| if isinstance(image, Image.Image): |
| image = np.array(image) |
| |
| |
| result = ocr.ocr(image, cls=True) |
| |
| txts = [] |
| if result and result[0]: |
| for line in result[0]: |
| text = line[1][0] |
| confidence = line[1][1] |
| |
| if confidence > 0.5: |
| txts.append(text) |
| return "\n".join(txts) |
| else: |
| return "未发现文字 / Không tìm thấy văn bản." |
| |
| except Exception as e: |
| return f"Error / Lỗi hệ thống: {str(e)}" |
|
|
| |
| iface = gr.Interface( |
| fn=predict, |
| inputs=gr.Image(type="pil", label="上传图片 / Upload Image"), |
| outputs=gr.Textbox(label="结果 / Result", lines=15, show_copy_button=True), |
| title="PaddleOCR Chinese - CPU Fixed", |
| description="Phiên bản đã sửa lỗi tham số 'show_log'. Chạy ổn định trên Hugging Face CPU.", |
| examples=[] |
| ) |
|
|
| if __name__ == "__main__": |
| iface.launch(server_name="0.0.0.0", server_port=7860) |