Test the prediction endpoint with an image
(base_url, image_path=None)
| 33 | return False |
| 34 | |
| 35 | def test_prediction(base_url, image_path=None): |
| 36 | """Test the prediction endpoint with an image""" |
| 37 | if not image_path: |
| 38 | print("\nSkipping prediction test (no image provided)") |
| 39 | return None |
| 40 | |
| 41 | print(f"\nTesting prediction endpoint with image: {image_path}") |
| 42 | try: |
| 43 | # Load and encode the image |
| 44 | with open(image_path, "rb") as img_file: |
| 45 | img_data = img_file.read() |
| 46 | |
| 47 | # First try with base64 |
| 48 | print("Testing base64 prediction...") |
| 49 | encoded_img = base64.b64encode(img_data).decode('utf-8') |
| 50 | response = requests.post( |
| 51 | f"{base_url}/predict", |
| 52 | json={"image": encoded_img, "prompt": "What can you see in this image?"}, |
| 53 | timeout=60 |
| 54 | ) |
| 55 | |
| 56 | if response.status_code == 200: |
| 57 | print("Base64 prediction succeeded!") |
| 58 | result = response.json() |
| 59 | print(f"Response text: {result.get('text_response', '')[:100]}...") |
| 60 | print(f"Normalized actions: {result.get('normalized_actions', [])}") |
| 61 | print(f"Delta values: {result.get('delta_values', [])}") |
| 62 | else: |
| 63 | print(f"Base64 prediction failed! Status: {response.status_code}") |
| 64 | print(f"Response: {response.text}") |
| 65 | |
| 66 | # Now try with file upload |
| 67 | print("\nTesting file upload prediction...") |
| 68 | files = {"file": open(image_path, "rb")} |
| 69 | data = {"prompt": "What can you see in this image?"} |
| 70 | response = requests.post( |
| 71 | f"{base_url}/predict_from_file", |
| 72 | files=files, |
| 73 | data=data, |
| 74 | timeout=60 |
| 75 | ) |
| 76 | |
| 77 | if response.status_code == 200: |
| 78 | print("File upload prediction succeeded!") |
| 79 | result = response.json() |
| 80 | print(f"Response text: {result.get('text_response', '')[:100]}...") |
| 81 | else: |
| 82 | print(f"File upload prediction failed! Status: {response.status_code}") |
| 83 | print(f"Response: {response.text}") |
| 84 | |
| 85 | except Exception as e: |
| 86 | print(f"Error testing prediction: {str(e)}") |
| 87 | return None |
| 88 | |
| 89 | def wait_for_service(base_url, max_retries=10, retry_delay=5): |
| 90 | """Wait for the service to be available""" |