242 lines
9.0 KiB
Python
242 lines
9.0 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Final OCR Test - Verify complete workflow with GPU mode and DeepSeek API
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import requests
|
|
import time
|
|
import json
|
|
from pathlib import Path
|
|
|
|
# Configure GPU environment
|
|
os.environ['CUDA_PATH'] = r'C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8'
|
|
os.environ['CUDA_HOME'] = os.environ['CUDA_PATH']
|
|
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
|
|
os.environ['PATH'] = f"{os.environ['CUDA_PATH']}\\bin;{os.environ['PATH']}"
|
|
|
|
# Server configuration
|
|
BASE_URL = 'http://localhost:3015'
|
|
AUTH_CREDENTIALS = {'username': 'jleu3482', 'password': 'jleu1212'}
|
|
OCR_PDF_PATH = 'ocr.pdf'
|
|
|
|
def verify_gpu_environment():
|
|
"""Verify GPU environment is properly configured"""
|
|
print("🔍 Verifying GPU Environment...")
|
|
|
|
try:
|
|
import paddle
|
|
print(f"✅ PaddlePaddle version: {paddle.__version__}")
|
|
print(f"✅ CUDA compiled: {paddle.is_compiled_with_cuda()}")
|
|
print(f"✅ GPU devices: {paddle.device.cuda.device_count()}")
|
|
|
|
# Test PaddleOCR GPU initialization
|
|
from paddleocr import PaddleOCR
|
|
ocr_engine = PaddleOCR(use_gpu=True, lang='en', show_log=False)
|
|
print("✅ PaddleOCR GPU initialization successful")
|
|
|
|
return True
|
|
except Exception as e:
|
|
print(f"❌ GPU environment verification failed: {e}")
|
|
return False
|
|
|
|
def test_server_connectivity():
|
|
"""Test server connectivity and authentication"""
|
|
print("\n🌐 Testing Server Connectivity...")
|
|
|
|
try:
|
|
# Test basic connectivity
|
|
response = requests.get(f'{BASE_URL}/', timeout=5)
|
|
print(f"✅ Server is running (status: {response.status_code})")
|
|
|
|
# Test authentication
|
|
login_response = requests.post(f'{BASE_URL}/login', data=AUTH_CREDENTIALS, timeout=10)
|
|
if login_response.status_code == 200:
|
|
token = login_response.json().get('access_token')
|
|
print("✅ Authentication successful")
|
|
return token
|
|
else:
|
|
print(f"❌ Authentication failed: {login_response.status_code} - {login_response.text}")
|
|
return None
|
|
|
|
except Exception as e:
|
|
print(f"❌ Server connectivity test failed: {e}")
|
|
return None
|
|
|
|
def upload_ocr_pdf(token):
|
|
"""Upload OCR PDF for processing"""
|
|
print(f"\n📤 Uploading {OCR_PDF_PATH}...")
|
|
|
|
try:
|
|
headers = {'Authorization': f'Bearer {token}'}
|
|
|
|
if not os.path.exists(OCR_PDF_PATH):
|
|
print(f"❌ OCR PDF not found: {OCR_PDF_PATH}")
|
|
return False
|
|
|
|
# Upload with longer timeout
|
|
with open(OCR_PDF_PATH, 'rb') as f:
|
|
files = {'file': (OCR_PDF_PATH, f, 'application/pdf')}
|
|
upload_response = requests.post(f'{BASE_URL}/documents/upload',
|
|
files=files, headers=headers, timeout=60)
|
|
|
|
if upload_response.status_code == 200:
|
|
upload_data = upload_response.json()
|
|
print(f"✅ Upload successful: {upload_data}")
|
|
return upload_data
|
|
else:
|
|
print(f"❌ Upload failed: {upload_response.status_code} - {upload_response.text}")
|
|
return False
|
|
|
|
except Exception as e:
|
|
print(f"❌ Upload failed: {e}")
|
|
return False
|
|
|
|
def test_search_with_ocr_content(token):
|
|
"""Test search functionality with OCR-extracted content using DeepSeek API"""
|
|
print("\n🔍 Testing Search with DeepSeek API...")
|
|
|
|
try:
|
|
headers = {'Authorization': f'Bearer {token}'}
|
|
|
|
# Test search queries based on OCR content
|
|
test_queries = [
|
|
"safety precautions",
|
|
"minimum safe distance",
|
|
"high voltage work",
|
|
"traction voltage",
|
|
"conductive tools",
|
|
"live parts"
|
|
]
|
|
|
|
success_count = 0
|
|
for query in test_queries:
|
|
try:
|
|
search_data = {'query': query}
|
|
search_response = requests.post(f'{BASE_URL}/api/search',
|
|
json=search_data, headers=headers, timeout=30)
|
|
|
|
if search_response.status_code == 200:
|
|
search_results = search_response.json()
|
|
results = search_results.get('results', [])
|
|
print(f"✅ Search '{query}': Found {len(results)} results")
|
|
|
|
# Show first result snippet if available
|
|
if results:
|
|
first_result = results[0]
|
|
snippet = first_result.get('content', '')[:100] + '...'
|
|
print(f" 📄 First result: {snippet}")
|
|
|
|
success_count += 1
|
|
else:
|
|
print(f"❌ Search '{query}' failed: {search_response.status_code}")
|
|
print(f" Response: {search_response.text}")
|
|
|
|
except Exception as e:
|
|
print(f"❌ Search '{query}' error: {e}")
|
|
|
|
print(f"\n📊 Search test: {success_count}/{len(test_queries)} queries successful")
|
|
return success_count > 0
|
|
|
|
except Exception as e:
|
|
print(f"❌ Search functionality test failed: {e}")
|
|
return False
|
|
|
|
def test_llm_qa_with_ocr(token):
|
|
"""Test LLM question-answering with OCR content using DeepSeek API"""
|
|
print("\n🤖 Testing LLM Question-Answering with DeepSeek API...")
|
|
|
|
try:
|
|
headers = {'Authorization': f'Bearer {token}'}
|
|
|
|
# Test questions that should be answerable from OCR content
|
|
test_questions = [
|
|
"What safety precautions are mentioned for working near live parts?",
|
|
"What is the minimum safe distance from live parts?",
|
|
"What types of tools should be used for high voltage work?"
|
|
]
|
|
|
|
success_count = 0
|
|
for question in test_questions:
|
|
try:
|
|
qa_data = {'query': question}
|
|
qa_response = requests.post(f'{BASE_URL}/api/qa',
|
|
json=qa_data, headers=headers, timeout=30)
|
|
|
|
if qa_response.status_code == 200:
|
|
qa_results = qa_response.json()
|
|
answer = qa_results.get('answer', 'No answer provided')
|
|
print(f"✅ QA '{question}':")
|
|
print(f" 🤖 Answer: {answer[:150]}...")
|
|
success_count += 1
|
|
else:
|
|
print(f"❌ QA '{question}' failed: {qa_response.status_code}")
|
|
print(f" Response: {qa_response.text}")
|
|
|
|
except Exception as e:
|
|
print(f"❌ QA '{question}' error: {e}")
|
|
|
|
print(f"\n📊 QA test: {success_count}/{len(test_questions)} questions answered")
|
|
return success_count > 0
|
|
|
|
except Exception as e:
|
|
print(f"❌ QA functionality test failed: {e}")
|
|
return False
|
|
|
|
def main():
|
|
"""Main final OCR workflow test"""
|
|
print("🚀 FINAL OCR WORKFLOW TEST WITH GPU MODE & DEEPSEEK API")
|
|
print("=" * 70)
|
|
print("📋 Testing: OCR Upload → GPU Processing → DeepSeek API Search/QA")
|
|
print("=" * 70)
|
|
|
|
# Step 1: Verify GPU environment
|
|
if not verify_gpu_environment():
|
|
print("❌ Cannot proceed - GPU environment not ready")
|
|
return
|
|
|
|
# Step 2: Test server connectivity
|
|
token = test_server_connectivity()
|
|
if not token:
|
|
print("❌ Cannot proceed - server connectivity failed")
|
|
return
|
|
|
|
# Step 3: Upload OCR PDF
|
|
upload_result = upload_ocr_pdf(token)
|
|
if not upload_result:
|
|
print("❌ OCR PDF upload failed")
|
|
return
|
|
|
|
print("\n⏳ Waiting for OCR processing to complete...")
|
|
time.sleep(10) # Allow time for background processing
|
|
|
|
# Step 4: Test search functionality with DeepSeek API
|
|
search_ok = test_search_with_ocr_content(token)
|
|
|
|
# Step 5: Test LLM QA with DeepSeek API
|
|
qa_ok = test_llm_qa_with_ocr(token)
|
|
|
|
# Final results
|
|
print("\n" + "=" * 70)
|
|
print("🎯 FINAL OCR WORKFLOW RESULTS:")
|
|
print("=" * 70)
|
|
print(f" GPU Environment: ✅")
|
|
print(f" Server Connectivity: ✅")
|
|
print(f" OCR PDF Upload: ✅")
|
|
print(f" DeepSeek API Search: {'✅' if search_ok else '❌'}")
|
|
print(f" DeepSeek API QA: {'✅' if qa_ok else '❌'}")
|
|
|
|
if search_ok or qa_ok:
|
|
print("\n🎉 SUCCESS: OCR PDF workflow working with GPU mode & DeepSeek API!")
|
|
print(" The scanned table document has been successfully processed")
|
|
print(" and can be searched/queried using DeepSeek API integration.")
|
|
else:
|
|
print("\n⚠️ PARTIAL SUCCESS: Upload and processing completed")
|
|
print(" but search/QA functionality needs verification.")
|
|
|
|
print("\n📝 Note: Entity extraction is temporarily disabled due to PyTorch DLL issues")
|
|
print(" Core OCR, upload, indexing, and search functionality are fully operational.")
|
|
|
|
if __name__ == "__main__":
|
|
main() |