import os import sys import json import httpx import logging sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from extraction.ocr_service import OCRService logging.basicConfig(level=logging.INFO, format="%(levelname)s:%(name)s:%(message)s") def main(): print("=== STARTING OCR SERVICE TEST ===") if not os.path.exists("ingestion_output.json"): print("File ingestion_output.json not found! Vui lòng chạy lại ingestion/sync.py") return with open("ingestion_output.json", "r", encoding="utf-8") as f: items = json.load(f) # Tìm đúng file SCAN_PDF để test target_name = "Bien-ban-ban-giao-scan.pdf" target_item = next((item for item in items if item.get("name") == target_name), None) if not target_item: print(f"Không tìm thấy file {target_name} trong ingestion_output.json") return download_url = target_item.get("download_url") if not download_url: print(f"File {target_name} không có download_url. Vui lòng xoá delta_state.json và chạy lại ingestion/sync.py") return print(f"\n1. Đang tải file: {target_name} trực tiếp từ SharePoint...") try: # Thay vì dùng downloadUrl dễ bị 401 Unauthorized do cơ chế tempauth, # ta sẽ dùng endpoint /content qua Graph API bằng token xịn của hệ thống. from ingestion.graph_client import GraphClient graph = GraphClient() drive_id = target_item.get("drive_id") item_id = target_item.get("item_id") url = f"https://graph.microsoft.com/v1.0/drives/{drive_id}/items/{item_id}/content" pdf_bytes = graph._download_file(url) print(f" => Tải thành công {len(pdf_bytes)} bytes.") except Exception as e: print(f"Tải file thất bại: {e}") return print("\n2. Khởi tạo PaddleOCR & VietOCR và bắt đầu nhận diện...") print(" (LƯU Ý: Lần chạy đầu tiên sẽ khá lâu do hệ thống phải tải model AI về máy)") ocr_service = OCRService() # Đẩy byte stream vào mổ xẻ results = ocr_service.process_pdf_bytes(pdf_bytes) print(f"\n3. Quá trình OCR hoàn tất! Tổng số trang đã dịch: {len(results)}") for result in results: print(f"\n==================== TRANG {result.page} ====================") print(f"\n--- [1] KẾT QUẢ TỪ PADDLEOCR MẶC ĐỊNH (Độ tự tin: {result.paddle_confidence}) ---") print(result.paddle_text) print(f"\n--- [2] KẾT QUẢ TỪ VIETOCR (Độ tự tin: {result.confidence}) ---") print(result.text) print("====================================================\n") if __name__ == "__main__": main()