File size: 1,064 Bytes
e2d9248
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import sys
import os

def test_api(image_path):
    url = "http://localhost:7860/predict"
    if not os.path.exists(image_path):
        print(f"Error: Image not found at {image_path}")
        return

    print(f"Testing API with image: {image_path}")
    try:
        with open(image_path, "rb") as f:
            files = {"file": ("image.jpg", f, "image/jpeg")}
            response = requests.post(url, files=files)
        
        if response.status_code == 200:
            print("Success!")
            print(response.json())
        else:
            print(f"Failed details: {response.text}")
            print(f"Status Code: {response.status_code}")
    except requests.exceptions.ConnectionError:
        print("Error: Could not connect to API. Is it running on port 7860?")

if __name__ == "__main__":
    if len(sys.argv) > 1:
        image_path = sys.argv[1]
    else:
        # Default to a known image if available, otherwise User provides one
        image_path = "../dataset/test/Heart/Heart(0).jpg"
    
    test_api(image_path)