Spaces:
Sleeping
Sleeping
| """ | |
| Fixed test script for the deployed Hugging Face API | |
| """ | |
| import requests | |
| import base64 | |
| import numpy as np | |
| 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_with_form_data(): | |
| """Test the API using form data (multipart/form-data)""" | |
| url = "https://pavaniyerra-hackthon4.hf.space/predict_similarity/" | |
| print("Testing API with form data...") | |
| print("=" * 40) | |
| try: | |
| # Create test images | |
| img1_b64 = create_test_image() | |
| img2_b64 = create_test_image() | |
| # Convert base64 to bytes | |
| img1_bytes = base64.b64decode(img1_b64) | |
| img2_bytes = base64.b64decode(img2_b64) | |
| # Prepare files for form data | |
| files = { | |
| 'file1': ('image1.jpg', img1_bytes, 'image/jpeg'), | |
| 'file2': ('image2.jpg', img2_bytes, 'image/jpeg') | |
| } | |
| print("Sending request with form data...") | |
| response = requests.post(url, files=files, timeout=30) | |
| print(f"Status Code: {response.status_code}") | |
| print(f"Response Headers: {dict(response.headers)}") | |
| print(f"Response Text: {response.text}") | |
| if response.status_code == 200: | |
| try: | |
| result = response.json() | |
| print("SUCCESS: API Response received successfully!") | |
| print(f"Similarity Score: {result}") | |
| except: | |
| print(f"Response (not JSON): {response.text}") | |
| else: | |
| print(f"ERROR: API Error: {response.status_code}") | |
| except Exception as e: | |
| print(f"ERROR: {e}") | |
| def test_api_with_json(): | |
| """Test the API using JSON data""" | |
| url = "https://pavaniyerra-hackthon4.hf.space/predict_similarity/" | |
| print("\nTesting API with JSON data...") | |
| print("=" * 40) | |
| try: | |
| # Create test images | |
| img1_b64 = create_test_image() | |
| img2_b64 = create_test_image() | |
| # Try different JSON formats | |
| json_formats = [ | |
| {"file1": img1_b64, "file2": img2_b64}, | |
| {"img1": img1_b64, "img2": img2_b64}, | |
| {"image1": img1_b64, "image2": img2_b64}, | |
| {"files": [img1_b64, img2_b64]}, | |
| {"data": {"file1": img1_b64, "file2": img2_b64}} | |
| ] | |
| for i, data in enumerate(json_formats): | |
| print(f"\nTrying JSON format {i+1}: {list(data.keys())}") | |
| response = requests.post(url, json=data, timeout=30) | |
| print(f"Status: {response.status_code}") | |
| if response.status_code == 200: | |
| print("SUCCESS!") | |
| print(f"Response: {response.text}") | |
| break | |
| else: | |
| print(f"Error: {response.text[:200]}...") | |
| except Exception as e: | |
| print(f"ERROR: {e}") | |
| def test_api_info(): | |
| """Get information about the API""" | |
| base_url = "https://pavaniyerra-hackthon4.hf.space" | |
| print("\nGetting API information...") | |
| print("=" * 40) | |
| try: | |
| # Try to get API info | |
| response = requests.get(base_url, timeout=30) | |
| print(f"Base URL Status: {response.status_code}") | |
| # Try common endpoints | |
| endpoints = ["/", "/docs", "/openapi.json", "/info", "/health"] | |
| for endpoint in endpoints: | |
| try: | |
| url = base_url + endpoint | |
| response = requests.get(url, timeout=10) | |
| print(f"{endpoint}: {response.status_code}") | |
| if response.status_code == 200 and len(response.text) < 500: | |
| print(f" Content: {response.text[:100]}...") | |
| except: | |
| print(f"{endpoint}: Error") | |
| except Exception as e: | |
| print(f"ERROR: {e}") | |
| if __name__ == "__main__": | |
| print("Hugging Face API Test - Fixed Version") | |
| print("=" * 50) | |
| print(f"API URL: https://pavaniyerra-hackthon4.hf.space/predict_similarity/") | |
| print() | |
| # Test different approaches | |
| test_api_with_form_data() | |
| test_api_with_json() | |
| test_api_info() | |
| print("\n" + "=" * 50) | |
| print("API Testing Complete!") | |