import requests import base64 import json def test_gradio_api(): """Test the Gradio /api/predict endpoint""" # You would replace this with actual base64 encoded image data sample_image_path = "test_image.jpg" # Replace with your test image try: with open(sample_image_path, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8') except FileNotFoundError: print("Please add a test_image.jpg file to test the API") return url = "http://localhost:7860/api/predict" payload = { "data": [ image_base64, "mobilenet", 0.5, 0.4 ] } response = requests.post(url, json=payload) if response.status_code == 200: result = response.json() print("Success!") print(f"API Response: {json.dumps(result, indent=2)}") # Extract the actual detection data if "data" in result and len(result["data"]) > 0: detection_data = result["data"][0] if "faces" in detection_data: print(f"Detected {len(detection_data['faces'])} faces") print(f"Processing time: {detection_data.get('processing_time', 'N/A'):.3f} seconds") print(f"Model used: {detection_data.get('model_used', 'N/A')}") for i, face in enumerate(detection_data['faces']): print(f"Face {i+1}:") print(f" Confidence: {face['confidence']:.3f}") print(f" Bounding box: {face['bbox']}") print(f" Landmarks: {face['landmarks']}") else: print("No face detection data in response") else: print("Unexpected response format") else: print(f"Error: {response.status_code}") print(response.text) def test_health_check(): """Test the Gradio app health""" url = "http://localhost:7860/" response = requests.get(url) if response.status_code == 200: print("Gradio app is running!") print("You can access the web interface at: http://localhost:7860") else: print(f"Health check failed: {response.status_code}") def test_direct_api_call(): """Test direct API call format that Thunkable would use""" sample_image_path = "test_image.jpg" # Replace with your test image try: with open(sample_image_path, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8') except FileNotFoundError: print("Please add a test_image.jpg file to test the API") return url = "http://localhost:7860/api/predict" # This is the format Thunkable will use payload = { "data": [image_base64, "mobilenet", 0.5, 0.4] } headers = { "Content-Type": "application/json" } print("Testing Thunkable-compatible API call...") response = requests.post(url, json=payload, headers=headers) if response.status_code == 200: result = response.json() print("✅ Thunkable API call successful!") # Parse the response as Thunkable would if "data" in result and result["data"]: detection_result = result["data"][0] print(f"Faces detected: {detection_result.get('total_faces', 0)}") print(f"Model used: {detection_result.get('model_used', 'unknown')}") print(f"Processing time: {detection_result.get('processing_time', 0):.3f}s") else: print("❌ Unexpected response format") else: print(f"❌ API call failed: {response.status_code}") print(response.text) if __name__ == "__main__": print("Testing RetinaFace Gradio API...") print("=" * 50) print("\n1. Health Check:") test_health_check() print("\n2. Gradio API Test:") test_gradio_api() print("\n3. Thunkable-Compatible API Test:") test_direct_api_call() print("\n" + "=" * 50) print("Testing complete!") print("\nFor Thunkable integration:") print("- Use URL: http://localhost:7860/api/predict") print("- Method: POST") print("- Body format: {\"data\": [\"base64_image\", \"mobilenet\", 0.5, 0.4]}") print("- Response will be in: response.data[0]")