78 lines
2.4 KiB
Python
78 lines
2.4 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Example client script to test the TexTeller server API.
|
|
"""
|
|
import requests
|
|
import base64
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
def test_base64_request(image_path: str, server_url: str = "http://localhost:8001/predict"):
|
|
"""Test the server with a base64-encoded image."""
|
|
# Read and encode the image
|
|
with open(image_path, "rb") as f:
|
|
image_data = f.read()
|
|
image_base64 = base64.b64encode(image_data).decode()
|
|
|
|
# Send request
|
|
response = requests.post(server_url, json={"image_base64": image_base64}, headers={"Content-Type": "application/json"})
|
|
|
|
# Print result
|
|
if response.status_code == 200:
|
|
result = response.json()
|
|
print(f"✓ Success!")
|
|
print(f"Result: {result.get('result', 'N/A')}")
|
|
return result
|
|
else:
|
|
print(f"✗ Error: {response.status_code}")
|
|
print(f"Response: {response.text}")
|
|
return None
|
|
|
|
|
|
def test_url_request(image_url: str, server_url: str = "http://localhost:8001/predict"):
|
|
"""Test the server with an image URL."""
|
|
# Send request
|
|
response = requests.post(server_url, json={"image_url": image_url}, headers={"Content-Type": "application/json"})
|
|
|
|
# Print result
|
|
if response.status_code == 200:
|
|
result = response.json()
|
|
print(f"✓ Success!")
|
|
print(f"Result: {result.get('result', 'N/A')}")
|
|
return result
|
|
else:
|
|
print(f"✗ Error: {response.status_code}")
|
|
print(f"Response: {response.text}")
|
|
return None
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print("=" * 50)
|
|
print("TexTeller Server API Test")
|
|
print("=" * 50)
|
|
|
|
# Test with local image if provided
|
|
if len(sys.argv) > 1:
|
|
image_path = sys.argv[1]
|
|
if Path(image_path).exists():
|
|
print(f"\nTest 1: Base64 request with local image")
|
|
print(f"Image: {image_path}")
|
|
test_base64_request(image_path)
|
|
else:
|
|
print(f"Error: Image file not found: {image_path}")
|
|
|
|
# Test with URL if provided
|
|
if len(sys.argv) > 2:
|
|
image_url = sys.argv[2]
|
|
print(f"\nTest 2: URL request")
|
|
print(f"URL: {image_url}")
|
|
test_url_request(image_url)
|
|
|
|
if len(sys.argv) == 1:
|
|
print("\nUsage:")
|
|
print(f" python {sys.argv[0]} <image_path> [image_url]")
|
|
print("\nExamples:")
|
|
print(f" python {sys.argv[0]} equation.png")
|
|
print(f" python {sys.argv[0]} equation.png https://example.com/formula.png")
|