""" Test script for the deployed Hugging Face API """ import requests import base64 import numpy as np import cv2 from PIL import Image import io def create_test_image(): """Create a test image for API testing""" # Create a simple test image img = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8) # Convert to PIL Image pil_img = Image.fromarray(img) # Convert to base64 buffer = io.BytesIO() pil_img.save(buffer, format='JPEG') img_str = base64.b64encode(buffer.getvalue()).decode() return img_str def test_api_similarity(): """Test the similarity API endpoint""" url = "https://pavaniyerra-hackthon4.hf.space/predict_similarity/" print("Testing Hugging Face API...") print("=" * 40) try: # Create two test images img1_b64 = create_test_image() img2_b64 = create_test_image() # Prepare the request data - API expects file1 and file2 data = { "file1": img1_b64, "file2": img2_b64 } print("Sending request to API...") response = requests.post(url, json=data, timeout=30) if response.status_code == 200: result = response.json() print("SUCCESS: API Response received successfully!") print(f"Similarity Score: {result}") # Interpret the similarity score if isinstance(result, (int, float)): if result > 0.8: print("Result: Very High Similarity (likely same person)") elif result > 0.6: print("Result: High Similarity (possibly same person)") elif result > 0.4: print("Result: Moderate Similarity (uncertain)") elif result > 0.2: print("Result: Low Similarity (likely different persons)") else: print("Result: Very Low Similarity (definitely different persons)") else: print(f"Unexpected response format: {result}") else: print(f"ERROR: API Error: {response.status_code}") print(f"Response: {response.text}") except requests.exceptions.RequestException as e: print(f"ERROR: Network Error: {e}") except Exception as e: print(f"ERROR: Error: {e}") def test_api_classification(): """Test the classification API endpoint (if available)""" # Try different possible endpoints possible_urls = [ "https://pavaniyerra-hackthon4.hf.space/predict_class/", "https://pavaniyerra-hackthon4.hf.space/classify/", "https://pavaniyerra-hackthon4.hf.space/predict/" ] print("\nTesting Classification API...") print("=" * 40) for url in possible_urls: try: # Create a test image img_b64 = create_test_image() # Prepare the request data - try different parameter names data = { "file": img_b64 } print(f"Trying endpoint: {url}") response = requests.post(url, json=data, timeout=30) if response.status_code == 200: result = response.json() print("SUCCESS: Classification API Response received successfully!") print(f"Predicted Class: {result}") return else: print(f"ERROR: {response.status_code} - {response.text[:100]}...") except requests.exceptions.RequestException as e: print(f"ERROR: Network Error for {url}: {e}") except Exception as e: print(f"ERROR: Error for {url}: {e}") print("No working classification endpoint found.") if __name__ == "__main__": print("Hugging Face API Test") print("=" * 50) print(f"API URL: https://pavaniyerra-hackthon4.hf.space/predict_similarity/") print() # Test similarity API test_api_similarity() # Test classification API (if available) test_api_classification() print("\n" + "=" * 50) print("API Testing Complete!") print("\nNote: This test uses random images.") print("For real testing, use actual face images.")