Download test_api.py from fokan/pdf-4: direct link, hf CLI and curl.
- Browser
- Download file 4.96 kB
-
https://huggingface.co/spaces/fokan/pdf-4/resolve/main/test_api.py
- Command line
-
hf download hf://spaces/fokan/pdf-4/test_api.py
-
curl -L -o test_api.py https://huggingface.co/spaces/fokan/pdf-4/resolve/main/test_api.py
4.96 kB
| #!/usr/bin/env python3 | |
| """ | |
| Test script for the Enhanced DOCX to PDF Converter API | |
| """ | |
| import requests | |
| import base64 | |
| import json | |
| import os | |
| # API endpoint | |
| BASE_URL = "http://localhost:8000" | |
| def test_health(): | |
| """Test health endpoint""" | |
| print("Testing health endpoint...") | |
| try: | |
| response = requests.get(f"{BASE_URL}/health") | |
| if response.status_code == 200: | |
| print("✓ Health check passed") | |
| print(f" Version: {response.json().get('version')}") | |
| else: | |
| print("✗ Health check failed") | |
| except Exception as e: | |
| print(f"✗ Health check error: {e}") | |
| def test_convert_file(docx_path): | |
| """Test file conversion""" | |
| print(f"\nTesting file conversion with {docx_path}...") | |
| if not os.path.exists(docx_path): | |
| print(f"✗ File {docx_path} not found") | |
| return | |
| try: | |
| with open(docx_path, 'rb') as f: | |
| files = {'file': (os.path.basename(docx_path), f, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')} | |
| response = requests.post(f"{BASE_URL}/convert", files=files) | |
| if response.status_code == 200: | |
| result = response.json() | |
| if result.get('success'): | |
| print("✓ File conversion successful") | |
| print(f" PDF URL: {result.get('pdf_url')}") | |
| else: | |
| print(f"✗ Conversion failed: {result.get('error')}") | |
| else: | |
| print(f"✗ Conversion failed with status {response.status_code}") | |
| print(response.text) | |
| except Exception as e: | |
| print(f"✗ Conversion error: {e}") | |
| def test_convert_base64(docx_path): | |
| """Test base64 conversion""" | |
| print(f"\nTesting base64 conversion with {docx_path}...") | |
| if not os.path.exists(docx_path): | |
| print(f"✗ File {docx_path} not found") | |
| return | |
| try: | |
| with open(docx_path, 'rb') as f: | |
| file_content = base64.b64encode(f.read()).decode('utf-8') | |
| data = { | |
| 'file_content': file_content, | |
| 'filename': os.path.basename(docx_path) | |
| } | |
| response = requests.post(f"{BASE_URL}/convert", data=data) | |
| if response.status_code == 200: | |
| result = response.json() | |
| if result.get('success'): | |
| print("✓ Base64 conversion successful") | |
| print(f" PDF URL: {result.get('pdf_url')}") | |
| else: | |
| print(f"✗ Conversion failed: {result.get('error')}") | |
| else: | |
| print(f"✗ Conversion failed with status {response.status_code}") | |
| print(response.text) | |
| except Exception as e: | |
| print(f"✗ Conversion error: {e}") | |
| def test_batch_convert(docx_paths): | |
| """Test batch conversion""" | |
| print(f"\nTesting batch conversion with {len(docx_paths)} files...") | |
| files_data = [] | |
| for path in docx_paths: | |
| if not os.path.exists(path): | |
| print(f"✗ File {path} not found") | |
| continue | |
| try: | |
| with open(path, 'rb') as f: | |
| file_content = base64.b64encode(f.read()).decode('utf-8') | |
| files_data.append({ | |
| 'file_content': file_content, | |
| 'filename': os.path.basename(path) | |
| }) | |
| except Exception as e: | |
| print(f"✗ Error reading {path}: {e}") | |
| if not files_data: | |
| print("✗ No valid files to convert") | |
| return | |
| try: | |
| payload = {'files': files_data} | |
| response = requests.post(f"{BASE_URL}/convert/batch", json=payload) | |
| if response.status_code == 200: | |
| results = response.json() | |
| success_count = sum(1 for r in results if r.get('success')) | |
| print(f"✓ Batch conversion completed: {success_count}/{len(results)} successful") | |
| for i, result in enumerate(results): | |
| if result.get('success'): | |
| print(f" File {i+1}: Success - {result.get('pdf_url')}") | |
| else: | |
| print(f" File {i+1}: Failed - {result.get('error')}") | |
| else: | |
| print(f"✗ Batch conversion failed with status {response.status_code}") | |
| print(response.text) | |
| except Exception as e: | |
| print(f"✗ Batch conversion error: {e}") | |
| if __name__ == "__main__": | |
| print("Enhanced DOCX to PDF Converter API Test Script") | |
| print("=" * 50) | |
| # Test health endpoint | |
| test_health() | |
| # Test with template.docx if available | |
| template_path = "template.docx" | |
| if os.path.exists(template_path): | |
| test_convert_file(template_path) | |
| test_convert_base64(template_path) | |
| test_batch_convert([template_path, template_path]) # Test with same file twice | |
| else: | |
| print(f"\nNote: {template_path} not found, skipping file tests") | |
| print("\nTest script completed.") |