kakatt / test_api.py
codernotme's picture
Upload 6 files
e2d9248 verified
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)