dots-ocr-idcard / scripts /quick_test.py
tommulder's picture
feat(api): fast FastAPI app + model loader refactor; add mock mode for tests\n\n- Add pyproject + setuptools config and console entrypoint\n- Implement enhanced field extraction + MRZ heuristics\n- Add response builder with compatibility for legacy MRZ fields\n- New preprocessing pipeline for PDFs/images\n- HF Spaces GPU: cache ENV, optional flash-attn, configurable base image\n- Add Make targets for Spaces GPU and local CPU\n- Add httpx for TestClient; tests pass in mock mode\n- Remove embedded model files and legacy app/modules
211e423
raw
history blame
2.74 kB
#!/usr/bin/env python3
"""Quick API Test Script
A simple script to quickly test the deployed Dots.OCR API endpoint.
"""
import requests
import json
import sys
from pathlib import Path
def test_api(base_url="http://localhost:7860"):
"""Quick test of the API endpoint."""
print(f"πŸ” Testing API at {base_url}")
# Health check
try:
health_response = requests.get(f"{base_url}/health", timeout=10)
health_response.raise_for_status()
health_data = health_response.json()
print(f"βœ… Health check passed: {health_data}")
except Exception as e:
print(f"❌ Health check failed: {e}")
return False
# Test with front image
front_image = Path(__file__).parent / "tom_id_card_front.jpg"
if not front_image.exists():
print(f"❌ Test image not found: {front_image}")
return False
print(f"πŸ“Έ Testing with {front_image.name}")
try:
with open(front_image, 'rb') as f:
files = {'file': f}
response = requests.post(
f"{base_url}/v1/id/ocr",
files=files,
timeout=30
)
response.raise_for_status()
result = response.json()
print(f"βœ… OCR test passed")
print(f" Request ID: {result.get('request_id')}")
print(f" Media type: {result.get('media_type')}")
print(f" Processing time: {result.get('processing_time'):.2f}s")
print(f" Detections: {len(result.get('detections', []))}")
# Show extracted fields
for i, detection in enumerate(result.get('detections', [])):
fields = detection.get('extracted_fields', {})
field_count = len([f for f in fields.values() if f is not None])
print(f" Page {i+1}: {field_count} fields extracted")
# Show some key fields
key_fields = ['document_number', 'surname', 'given_names', 'nationality']
for field in key_fields:
if field in fields and fields[field] is not None:
value = fields[field].get('value', 'N/A') if isinstance(fields[field], dict) else str(fields[field])
confidence = fields[field].get('confidence', 'N/A') if isinstance(fields[field], dict) else 'N/A'
print(f" {field}: {value} (confidence: {confidence})")
return True
except Exception as e:
print(f"❌ OCR test failed: {e}")
return False
if __name__ == "__main__":
base_url = sys.argv[1] if len(sys.argv) > 1 else "http://localhost:7860"
success = test_api(base_url)
sys.exit(0 if success else 1)