Spaces:
Sleeping
Sleeping
| #!/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()) | |