Spaces:
Sleeping
Sleeping
File size: 4,938 Bytes
db10255 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
#!/usr/bin/env python3
"""
Test script for HandyHome OCR API
Tests all endpoints with sample document URLs
"""
import requests
import json
import sys
# Configuration
BASE_URL = "http://localhost:7860" # Change to your Hugging Face Space URL for remote testing
# Sample test URLs (replace with your actual test image URLs)
TEST_URLS = {
'national_id': 'https://example.com/national_id.jpg',
'drivers_license': 'https://example.com/drivers_license.jpg',
'prc': 'https://example.com/prc.jpg',
'umid': 'https://example.com/umid.jpg',
'sss': 'https://example.com/sss.jpg',
'passport': 'https://example.com/passport.jpg',
'postal': 'https://example.com/postal.jpg',
'phic': 'https://example.com/phic.jpg',
'nbi': 'https://example.com/nbi.jpg',
'police': 'https://example.com/police.jpg',
'tesda': 'https://example.com/tesda.jpg',
}
def test_health():
"""Test health endpoint"""
print("\n" + "="*60)
print("Testing Health Endpoint")
print("="*60)
try:
response = requests.get(f"{BASE_URL}/health", timeout=10)
print(f"Status Code: {response.status_code}")
print(f"Response: {json.dumps(response.json(), indent=2)}")
return response.status_code == 200
except Exception as e:
print(f"Error: {str(e)}")
return False
def test_index():
"""Test index/documentation endpoint"""
print("\n" + "="*60)
print("Testing Index/Documentation Endpoint")
print("="*60)
try:
response = requests.get(f"{BASE_URL}/", timeout=10)
print(f"Status Code: {response.status_code}")
data = response.json()
print(f"Service: {data.get('service')}")
print(f"Version: {data.get('version')}")
print(f"Available Endpoints: {len(data.get('endpoints', {}))}")
return response.status_code == 200
except Exception as e:
print(f"Error: {str(e)}")
return False
def test_extraction_endpoint(endpoint, document_type, document_url):
"""Test an OCR extraction endpoint"""
print("\n" + "="*60)
print(f"Testing: {document_type}")
print("="*60)
print(f"Endpoint: {endpoint}")
print(f"Document URL: {document_url}")
try:
response = requests.post(
f"{BASE_URL}{endpoint}",
json={'document_url': document_url},
timeout=300
)
print(f"Status Code: {response.status_code}")
try:
data = response.json()
print(f"Response: {json.dumps(data, indent=2)}")
return response.status_code == 200 and data.get('success', False)
except:
print(f"Response (non-JSON): {response.text[:200]}")
return False
except requests.exceptions.Timeout:
print("Error: Request timed out (>5 minutes)")
return False
except Exception as e:
print(f"Error: {str(e)}")
return False
def main():
"""Run all tests"""
print("\n" + "="*60)
print("HandyHome OCR API Test Suite")
print("="*60)
print(f"Base URL: {BASE_URL}")
results = {}
# Test utility endpoints
results['health'] = test_health()
results['index'] = test_index()
# Test OCR endpoints
endpoints_to_test = [
('/api/extract-national-id', 'National ID', TEST_URLS['national_id']),
('/api/extract-drivers-license', "Driver's License", TEST_URLS['drivers_license']),
('/api/extract-prc', 'PRC ID', TEST_URLS['prc']),
('/api/extract-umid', 'UMID', TEST_URLS['umid']),
('/api/extract-sss', 'SSS ID', TEST_URLS['sss']),
('/api/extract-passport', 'Passport', TEST_URLS['passport']),
('/api/extract-postal', 'Postal ID', TEST_URLS['postal']),
('/api/extract-phic', 'PhilHealth ID', TEST_URLS['phic']),
('/api/extract-nbi', 'NBI Clearance', TEST_URLS['nbi']),
('/api/extract-police-clearance', 'Police Clearance', TEST_URLS['police']),
('/api/extract-tesda', 'TESDA Certificate', TEST_URLS['tesda']),
]
for endpoint, doc_type, url in endpoints_to_test:
key = doc_type.lower().replace(' ', '_').replace("'", '')
results[key] = test_extraction_endpoint(endpoint, doc_type, url)
# Print summary
print("\n" + "="*60)
print("Test Summary")
print("="*60)
passed = sum(1 for v in results.values() if v)
total = len(results)
for test_name, passed_test in results.items():
status = "β PASS" if passed_test else "β FAIL"
print(f"{status} - {test_name}")
print("\n" + "="*60)
print(f"Results: {passed}/{total} tests passed")
print("="*60)
return 0 if passed == total else 1
if __name__ == '__main__':
sys.exit(main())
|