Spaces:
Sleeping
Sleeping
File size: 2,897 Bytes
b96d38b | 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 | import requests
import base64
import json
from pathlib import Path
class LabReportAPIClient:
"""Client for testing the Lab Report Analysis API"""
def __init__(self, base_url: str = "http://localhost:8000"):
self.base_url = base_url.rstrip('/')
def health_check(self):
"""Check if the API is running"""
try:
response = requests.get(f"{self.base_url}/health")
return response.json()
except requests.exceptions.RequestException as e:
return {"error": f"Failed to connect: {str(e)}"}
def analyze_image_file(self, image_path: str):
"""Analyze a lab report image file"""
try:
with open(image_path, 'rb') as f:
files = {'file': (Path(image_path).name, f, 'image/jpeg')}
response = requests.post(f"{self.base_url}/analyze", files=files)
if response.status_code == 200:
return response.json()
else:
return {
"error": True,
"status_code": response.status_code,
"message": response.text
}
except Exception as e:
return {"error": f"Failed to analyze image: {str(e)}"}
def analyze_base64_image(self, image_path: str):
"""Analyze a lab report using base64 encoding"""
try:
with open(image_path, 'rb') as f:
image_b64 = base64.b64encode(f.read()).decode('utf-8')
data = {"image": image_b64}
response = requests.post(
f"{self.base_url}/analyze-base64",
json=data,
headers={'Content-Type': 'application/json'}
)
if response.status_code == 200:
return response.json()
else:
return {
"error": True,
"status_code": response.status_code,
"message": response.text
}
except Exception as e:
return {"error": f"Failed to analyze base64 image: {str(e)}"}
def main():
"""Test the API client"""
client = LabReportAPIClient()
# Health check
print("🏥 Testing Lab Report Analysis API")
print("=" * 50)
health = client.health_check()
print(f"Health Check: {health}")
print()
# You can test with an actual image file
# Uncomment and modify the path below to test with your lab report image
# image_path = "your_lab_report_image.jpg" # Replace with actual path
# print(f"Analyzing image: {image_path}")
# result = client.analyze_image_file(image_path)
# print(json.dumps(result, indent=2))
if __name__ == "__main__":
main() |