from gradio_client import Client, handle_file from PIL import Image import base64 import io import os # def test_base64_api(client_url="http://localhost:7860"): # """Test the base64 API endpoint""" # print("๐Ÿงช Testing Base64 API Endpoint...") # try: # # Initialize the client # client = Client(client_url) # # Check if temp_drawing.png exists, if not create a simple test image # if not os.path.exists("temp_drawing.png"): # print("Creating a test image...") # # Create a simple test image (white background with black circle) # img = Image.new('RGB', (400, 400), 'white') # from PIL import ImageDraw # draw = ImageDraw.Draw(img) # draw.ellipse([150, 150, 250, 250], fill='black') # img.save("temp_drawing.png") # # Load the image # image = Image.open("temp_drawing.png") # print(f"โœ“ Loaded image: {image.size}") # # Convert image to base64 # buffered = io.BytesIO() # image.save(buffered, format="PNG") # img_str = base64.b64encode(buffered.getvalue()).decode() # print(f"โœ“ Converted to base64 (length: {len(img_str)})") # # Call the API endpoint with correct parameter names # result = client.predict( # image_data=img_str, # top_k=5, # api_name="/classify_image_api" # ) # print("โœ“ API Response:") # print(f" Success: {result.get('success', False)}") # if result.get('success'): # print(" Predictions:") # for i, pred in enumerate(result.get('predictions', []), 1): # print(f" {i}. {pred['category']} - {pred['confidence']:.3f}") # else: # print(f" Error: {result.get('error', 'Unknown error')}") # except Exception as e: # print(f"โŒ Base64 API test failed: {str(e)}") # def test_main_interface(client_url="http://localhost:7860"): # """Test the main drawing interface""" # print("\n๐Ÿงช Testing Main Drawing Interface...") # try: # # Initialize the client # client = Client(client_url) # # Check if temp_drawing.png exists # if not os.path.exists("temp_drawing.png"): # print("Creating a test image...") # # Create a simple test image (white background with black circle) # img = Image.new('RGB', (400, 400), 'white') # from PIL import ImageDraw # draw = ImageDraw.Draw(img) # draw.ellipse([150, 150, 250, 250], fill='black') # img.save("temp_drawing.png") # # Load and convert image to the format expected by Sketchpad # image = Image.open("temp_drawing.png") # # Create a mock sketchpad data structure # sketchpad_data = { # "background": image, # "layers": [image], # "composite": image # } # # Test the main gradio_classify function # result = client.predict( # canvas=sketchpad_data, # api_name="/gradio_classify" # ) # print("โœ“ Main Interface Response:") # print(result) # except Exception as e: # print(f"โŒ Main interface test failed: {str(e)}") # print("๐Ÿ’ก Try using the web interface directly instead") # def test_huggingface_space(): # """Test the deployed Hugging Face space""" # print("\n๐Ÿงช Testing Hugging Face Space...") # try: # # Test your deployed space # client = Client("souvikg544/quickdraw-classifier") # # Check if temp_drawing.png exists # if not os.path.exists("temp_drawing.png"): # print("Creating a test image...") # img = Image.new('RGB', (400, 400), 'white') # from PIL import ImageDraw # draw = ImageDraw.Draw(img) # draw.ellipse([150, 150, 250, 250], fill='black') # img.save("temp_drawing.png") # # Load and convert image to the format expected by Sketchpad # image = Image.open("temp_drawing.png") # # Create a mock sketchpad data structure # sketchpad_data = { # "background": image, # "layers": [image], # "composite": image # } # # Test with sketchpad format # result = client.predict( # canvas=sketchpad_data, # api_name="/gradio_classify" # ) # print("โœ“ Hugging Face Space Response:") # print(result) # except Exception as e: # print(f"โŒ Hugging Face space test failed: {str(e)}") # def create_test_drawings(): # """Create various test drawings for testing""" # print("\n๐ŸŽจ Creating test drawings...") # from PIL import ImageDraw # # Test drawing 1: Circle (might be classified as sun, moon, etc.) # img1 = Image.new('RGB', (400, 400), 'white') # draw1 = ImageDraw.Draw(img1) # draw1.ellipse([150, 150, 250, 250], fill='black') # img1.save("test_circle.png") # print("โœ“ Created test_circle.png") # # Test drawing 2: Rectangle (might be classified as house, car, etc.) # img2 = Image.new('RGB', (400, 400), 'white') # draw2 = ImageDraw.Draw(img2) # draw2.rectangle([100, 150, 300, 250], fill='black') # img2.save("test_rectangle.png") # print("โœ“ Created test_rectangle.png") # # Test drawing 3: Triangle (might be classified as tree, mountain, etc.) # img3 = Image.new('RGB', (400, 400), 'white') # draw3 = ImageDraw.Draw(img3) # draw3.polygon([(200, 100), (150, 200), (250, 200)], fill='black') # img3.save("test_triangle.png") # print("โœ“ Created test_triangle.png") # def test_multiple_drawings(): # """Test classification with multiple different drawings""" # print("\n๐Ÿงช Testing Multiple Drawings...") # test_files = ["test_circle.png", "test_rectangle.png", "test_triangle.png"] # try: # client = Client("http://localhost:7860") # for test_file in test_files: # if os.path.exists(test_file): # print(f"\n--- Testing {test_file} ---") # # Load image and create sketchpad format # image = Image.open(test_file) # sketchpad_data = { # "background": image, # "layers": [image], # "composite": image # } # result = client.predict( # canvas=sketchpad_data, # api_name="/gradio_classify" # ) # print(result) # else: # print(f"โŒ {test_file} not found") # except Exception as e: # print(f"โŒ Multiple drawings test failed: {str(e)}") # def test_base64_with_file(): # """Test base64 API with actual file conversion""" # print("\n๐Ÿงช Testing Base64 API with File Conversion...") # try: # client = Client("http://localhost:7860") # # Use one of our test files # test_file = "test_circle.png" # if os.path.exists(test_file): # # Load and convert to base64 # with open(test_file, "rb") as f: # img_bytes = f.read() # img_b64 = base64.b64encode(img_bytes).decode() # print(f"โœ“ Converted {test_file} to base64") # # Test the base64 API with correct parameter names # result = client.predict( # image_data=img_b64, # top_k=5, # api_name="/classify_image_api" # ) # print("โœ“ Base64 API Response:") # print(result) # else: # print("โŒ No test file found for base64 testing") # except Exception as e: # print(f"โŒ Base64 file test failed: {str(e)}") # if __name__ == "__main__": # print("๐Ÿš€ QuickDraw Classifier API Test Suite") # print("=" * 50) # # Create test drawings first # create_test_drawings() # # Test local server (make sure your app is running on localhost:7860) # print("\n๐Ÿ“ Testing Local Server...") # test_base64_api("http://localhost:7860") # test_main_interface("http://localhost:7860") # test_multiple_drawings() # # Test Hugging Face deployment # test_huggingface_space() # # Test base64 API with file conversion # test_base64_with_file() # print("\nโœ… Test suite completed!") # print("\n๐Ÿ’ก Tips:") # print("- Make sure your local server is running: python app.py") # print("- For HF Space testing, ensure your space is public and running") # print("- Check the generated test images: test_circle.png, test_rectangle.png, test_triangle.png") from gradio_client import Client image = Image.open("temp_drawing.png") buffered = io.BytesIO() image.save(buffered, format="PNG") img_str = base64.b64encode(buffered.getvalue()).decode() # client = Client("souvikg544/quickdraw-classifier") client = Client("https://6820c804655322f295.gradio.live/") result = client.predict( image_data=img_str, top_k=5, word="broom", api_name="/classify_image_api" ) print(result)