kataria_opticals_api / test_api.py
codernotme's picture
commit
a5a6a2e verified
import requests
import time
import sys
import os
API_URL = "http://localhost:8000"
IMAGE_PATH = "/home/codernotme/.gemini/antigravity/brain/fd9634fe-740d-41a0-9d95-4bd5df08a975/test_face_1770615920629.png" # Updated path
def wait_for_server():
print("Waiting for server...")
for i in range(30):
try:
response = requests.get(f"{API_URL}/health")
if response.status_code == 200:
print("Server is ready!")
return True
except requests.exceptions.ConnectionError:
pass
time.sleep(2)
print(".", end="", flush=True)
print("\nServer timed out.")
return False
def test_recommendation():
if not os.path.exists(IMAGE_PATH):
print(f"Test image not found at {IMAGE_PATH}")
return
print(f"Testing recommendation with {IMAGE_PATH}...")
try:
with open(IMAGE_PATH, "rb") as f:
files = {"file": f}
response = requests.post(f"{API_URL}/recommend", files=files)
if response.status_code == 200:
print("Success!")
print(response.json())
else:
print(f"Failed: {response.status_code}")
print(response.text)
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
if wait_for_server():
test_recommendation()
else:
sys.exit(1)