File size: 4,360 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
"""
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.")