Spaces:
Sleeping
Sleeping
File size: 4,540 Bytes
2ca4976 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
"""
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!")
|